[clang-tidy] Fix false positive in misc-const-correctness (#170065)

Closes https://github.com/llvm/llvm-project/issues/131599 and
https://github.com/llvm/llvm-project/issues/170033
This commit is contained in:
mitchell
2025-12-02 09:16:17 +08:00
committed by GitHub
parent 91e8780424
commit 326ee7af41
4 changed files with 39 additions and 1 deletions

View File

@@ -447,7 +447,8 @@ Changes in existing checks
positives when pointers is transferred to non-const references
and avoid false positives of function pointer and fix false
positives on return of non-const pointer and fix false positives on
pointer-to-member operator.
pointer-to-member operator and avoid false positives when the address
of a variable is taken to be passed to a function.
- Improved :doc:`misc-coroutine-hostile-raii
<clang-tidy/checks/misc/coroutine-hostile-raii>` check by adding the option

View File

@@ -73,3 +73,18 @@ void ignoreNonConstRefOps() {
int* p2 {nullptr};
int*& r2 = (int*&)p2;
}
void pointer_to_pointer_param(int**);
void pass_address_to_pointer_to_pointer() {
int i = 0;
int* ip = &i;
// CHECK-NOT: warning
pointer_to_pointer_param(&ip);
}
void void_pointer_to_pointer_param(void**);
void pass_address_to_void_pointer_to_pointer() {
void* ptr = nullptr;
// CHECK-NOT: warning
void_pointer_to_pointer_param(&ptr);
}

View File

@@ -135,6 +135,11 @@ class ExprPointeeResolve {
if (const auto *PE = dyn_cast<ParenExpr>(E))
return resolveExpr(PE->getSubExpr());
if (const auto *UO = dyn_cast<UnaryOperator>(E)) {
if (UO->getOpcode() == UO_AddrOf)
return resolveExpr(UO->getSubExpr());
}
if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E)) {
// only implicit cast needs to be treated as resolvable.
// explicit cast will be checked in `findPointeeToNonConst`

View File

@@ -2091,4 +2091,21 @@ TEST(ExprMutationAnalyzerTest, PointeeMutatedByPointerToMemberOperator) {
EXPECT_TRUE(isPointeeMutated(Results, AST.get()));
}
TEST(ExprMutationAnalyzerTest, PointeeMutatedByPassAsPointerToPointer) {
{
const std::string Code = "void f(int**); void g() { int* ip; f(&ip); }";
auto AST = buildASTFromCode(Code);
auto Results =
match(withEnclosingCompound(declRefTo("ip")), AST->getASTContext());
EXPECT_TRUE(isPointeeMutated(Results, AST.get()));
}
{
const std::string Code = "void f(void**); void g() { void* ip; f(&ip); }";
auto AST = buildASTFromCode(Code);
auto Results =
match(withEnclosingCompound(declRefTo("ip")), AST->getASTContext());
EXPECT_TRUE(isPointeeMutated(Results, AST.get()));
}
}
} // namespace clang