[clang-tools-extra] 0734c02 - [clang-tidy] Avoid extra parentheses around MemberExpr

Danny Mösch via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 26 13:36:06 PDT 2022


Author: Danny Mösch
Date: 2022-07-26T22:36:00+02:00
New Revision: 0734c02b34e49568d278dce7ca4b818fc465853d

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

LOG: [clang-tidy] Avoid extra parentheses around MemberExpr

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

Reviewed By: aaron.ballman

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

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/readability/ContainerDataPointerCheck.cpp
    clang-tools-extra/docs/ReleaseNotes.rst
    clang-tools-extra/test/clang-tidy/checkers/readability/container-data-pointer.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/readability/ContainerDataPointerCheck.cpp b/clang-tools-extra/clang-tidy/readability/ContainerDataPointerCheck.cpp
index d9851a89ebe1b..e22bad7db3328 100644
--- a/clang-tools-extra/clang-tidy/readability/ContainerDataPointerCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ContainerDataPointerCheck.cpp
@@ -98,7 +98,8 @@ void ContainerDataPointerCheck::check(const MatchFinder::MatchResult &Result) {
       Lexer::getSourceText(CharSourceRange::getTokenRange(SrcRange),
                            *Result.SourceManager, getLangOpts())};
 
-  if (!isa<DeclRefExpr, ArraySubscriptExpr, CXXOperatorCallExpr, CallExpr>(CE))
+  if (!isa<DeclRefExpr, ArraySubscriptExpr, CXXOperatorCallExpr, CallExpr,
+           MemberExpr>(CE))
     ReplacementText = "(" + ReplacementText + ")";
 
   if (CE->getType()->isPointerType())

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 75c7f446103a5..08eacd321d5ea 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -255,6 +255,9 @@ Changes in existing checks
   <clang-tidy/checks/readability/const-return-type>` when a pure virtual function
   overrided has a const return type. Removed the fix for a virtual function.
 
+- Skipped addition of extra parentheses around member accesses (``a.b``) in fix-it for
+  :doc:`readability-container-data-pointer <clang-tidy/checks/readability/container-data-pointer>`.
+
 - Fixed incorrect suggestions for :doc:`readability-container-size-empty
   <clang-tidy/checks/readability/container-size-empty>` when smart pointers are involved.
 

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/readability/container-data-pointer.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/container-data-pointer.cpp
index c3d6aa7cf57b1..7f75564ad9dc9 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/container-data-pointer.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/container-data-pointer.cpp
@@ -144,3 +144,14 @@ int *q(std::vector<int> ***v) {
   // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer]
   // CHECK-FIXES: {{^  }}return (**v)->data();{{$}}
 }
+
+struct VectorHolder {
+  std::vector<int> v;
+};
+
+int *r() {
+  VectorHolder holder;
+  return &holder.v[0];
+  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: 'data' should be used for accessing the data pointer instead of taking the address of the 0-th element [readability-container-data-pointer]
+  // CHECK-FIXES: {{^  }}return holder.v.data();{{$}}
+}


        


More information about the cfe-commits mailing list