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