[clang-tidy] Avoid extra parentheses around MemberExpr
authorDanny Mösch <danny.moesch@icloud.com>
Tue, 12 Jul 2022 21:33:01 +0000 (23:33 +0200)
committerDanny Mösch <danny.moesch@icloud.com>
Tue, 26 Jul 2022 20:36:00 +0000 (22:36 +0200)
Fixes https://github.com/llvm/llvm-project/issues/55025.

Reviewed By: aaron.ballman

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

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

index d9851a8..e22bad7 100644 (file)
@@ -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())
index 75c7f44..08eacd3 100644 (file)
@@ -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.
 
index c3d6aa7..7f75564 100644 (file)
@@ -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();{{$}}
+}