[clang-tools-extra] fc354d3 - [clang-tidy] Skip parentheses in `readability-make-member-function-const`

Evgeny Shulgin via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 21 04:18:22 PDT 2022


Author: Evgeny Shulgin
Date: 2022-03-21T14:18:17+03:00
New Revision: fc354d375232c22bbc13b80e4e339d8925ae2c70

URL: https://github.com/llvm/llvm-project/commit/fc354d375232c22bbc13b80e4e339d8925ae2c70
DIFF: https://github.com/llvm/llvm-project/commit/fc354d375232c22bbc13b80e4e339d8925ae2c70.diff

LOG: [clang-tidy] Skip parentheses in `readability-make-member-function-const`

The checker should ignore parentheses when looking whether the
function should be marked as `const`.

Fixes https://github.com/llvm/llvm-project/issues/52838

Reviewed By: mgehre-amd, njames93

Differential Revision: https://reviews.llvm.org/D122075

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/readability/MakeMemberFunctionConstCheck.cpp
    clang-tools-extra/test/clang-tidy/checkers/readability-make-member-function-const.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/readability/MakeMemberFunctionConstCheck.cpp b/clang-tools-extra/clang-tidy/readability/MakeMemberFunctionConstCheck.cpp
index 3e5b71fdd56b6..15ab394eeee11 100644
--- a/clang-tools-extra/clang-tidy/readability/MakeMemberFunctionConstCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/MakeMemberFunctionConstCheck.cpp
@@ -66,6 +66,13 @@ class FindUsageOfThis : public RecursiveASTVisitor<FindUsageOfThis> {
     return Parents.begin()->get<T>();
   }
 
+  const Expr *getParentExprIgnoreParens(const Expr *E) {
+    const Expr *Parent = getParent<Expr>(E);
+    while (isa_and_nonnull<ParenExpr>(Parent))
+      Parent = getParent<Expr>(Parent);
+    return Parent;
+  }
+
   bool VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *) {
     // An UnresolvedMemberExpr might resolve to a non-const non-static
     // member function.
@@ -140,7 +147,7 @@ class FindUsageOfThis : public RecursiveASTVisitor<FindUsageOfThis> {
       return true;
     }
 
-    const auto *Parent = getParent<Expr>(Member);
+    const auto *Parent = getParentExprIgnoreParens(Member);
 
     if (const auto *Cast = dyn_cast_or_null<ImplicitCastExpr>(Parent)) {
       // A read access to a member is safe when the member either
@@ -167,12 +174,12 @@ class FindUsageOfThis : public RecursiveASTVisitor<FindUsageOfThis> {
   bool VisitCXXThisExpr(const CXXThisExpr *E) {
     Usage = Const;
 
-    const auto *Parent = getParent<Expr>(E);
+    const auto *Parent = getParentExprIgnoreParens(E);
 
     // Look through deref of this.
     if (const auto *UnOp = dyn_cast_or_null<UnaryOperator>(Parent)) {
       if (UnOp->getOpcode() == UO_Deref) {
-        Parent = getParent<Expr>(UnOp);
+        Parent = getParentExprIgnoreParens(UnOp);
       }
     }
 

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/readability-make-member-function-const.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability-make-member-function-const.cpp
index 3591c169e332f..d454ad20fdd1b 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability-make-member-function-const.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability-make-member-function-const.cpp
@@ -40,6 +40,12 @@ struct A {
     return ConstM;
   }
 
+  int read_fields_in_parentheses() {
+    // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: method 'read_fields_in_parentheses' can be made const
+    // CHECK-FIXES: {{^}}  int read_fields_in_parentheses() const {
+    return (this)->M + (((((Struct.M))))) + ((this->ConstM));
+  }
+
   void call_const_member() {
     // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: method 'call_const_member' can be made const
     // CHECK-FIXES: {{^}}  void call_const_member() const {


        


More information about the cfe-commits mailing list