Skip to content

Conversation

Changqing-JING
Copy link
Contributor

void* ignore_const_return(){
  void* const p = nullptr;
  return p;
}

misc-const-correctness give fake positive when a pointer is not modified but returned as non-const type

test.cpp:7:3: warning: pointee of variable 'p' of type 'void *' can be declared 'const' [misc-const-correctness]
    7 |   void * p = nullptr;

Copy link

github-actions bot commented Oct 6, 2025

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang-tools-extra clang-tidy clang:analysis labels Oct 6, 2025
@llvmbot
Copy link
Member

llvmbot commented Oct 6, 2025

@llvm/pr-subscribers-clang
@llvm/pr-subscribers-clang-tools-extra

@llvm/pr-subscribers-clang-tidy

Author: Changqing Jing (Changqing-JING)

Changes
void* ignore_const_return(){
  void* const p = nullptr;
  return p;
}

misc-const-correctness give fake positive when a pointer is not modified but returned as non-const type

test.cpp:7:3: warning: pointee of variable 'p' of type 'void *' can be declared 'const' [misc-const-correctness]
    7 |   void * p = nullptr;

Full diff: https://github.com/llvm/llvm-project/pull/162067.diff

2 Files Affected:

  • (modified) clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-pointers.cpp (+30)
  • (modified) clang/lib/Analysis/ExprMutationAnalyzer.cpp (+7-1)
diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-pointers.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-pointers.cpp
index 2ef47266b02b0..6a4cbfec614aa 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-pointers.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-pointers.cpp
@@ -48,3 +48,33 @@ void ignore_const_alias() {
   p_local0 = &a[1];
 }
 
+void* ignore_const_return(){
+  void* const p = nullptr;
+  return p;
+}
+
+void const* const_return(){
+  void * p = nullptr;
+  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: pointee of variable 'p' of type 'void *' can be declared 'const'
+  // CHECK-FIXES: void  const* p
+  return p;
+}
+
+template<typename T>
+T* ignore_const_return_template(){
+  T* const p = nullptr;
+  return p;
+}
+
+template<typename T>
+T const* const_return_template(){
+  T * p = nullptr;
+  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: pointee of variable 'p' of type 'int *' can be declared 'const'
+  // CHECK-FIXES: T  const* p
+  return p;
+}
+
+void instantiate_return_templates() {
+  ignore_const_return_template<int>();
+  const_return_template<int>();
+}
diff --git a/clang/lib/Analysis/ExprMutationAnalyzer.cpp b/clang/lib/Analysis/ExprMutationAnalyzer.cpp
index 3fcd3481c2d6b..c051212ff1e57 100644
--- a/clang/lib/Analysis/ExprMutationAnalyzer.cpp
+++ b/clang/lib/Analysis/ExprMutationAnalyzer.cpp
@@ -787,10 +787,16 @@ ExprMutationAnalyzer::Analyzer::findPointeeToNonConst(const Expr *Exp) {
   // FIXME: false positive if the pointee does not change in lambda
   const auto CaptureNoConst = lambdaExpr(hasCaptureInit(Exp));
 
+  // Return statement in function with non-const pointer return type
+  const auto ReturnAsNonConst = returnStmt(
+      hasDescendant(equalsNode(Exp)), 
+      hasAncestor(functionDecl(returns(NonConstPointerOrDependentType))));
+
   const auto Matches =
       match(stmt(anyOf(forEachDescendant(
                            stmt(anyOf(AssignToNonConst, PassAsNonConstArg,
-                                      CastToNonConst, CaptureNoConst))
+                                      CastToNonConst, CaptureNoConst,
+                                      ReturnAsNonConst))
                                .bind("stmt")),
                        forEachDescendant(InitToNonConst))),
             Stm, Context);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:analysis clang Clang issues not falling into any other category clang-tidy clang-tools-extra

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants