Author: Yanzuo Liu
Date: 2026-07-11T16:51:06+08:00
New Revision: cce2b5acd7b0307c7985d58e7348a3e06b928eb4

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

LOG: [clang-tidy] Support parentheses around subscript operators in 
readability-redundant-parentheses (#208759)

Subscript operators have the same operator procedure as function calls.

Treat overloaded `()` as built-in operators as a drive-by. I missed this
case when reviewing #192254.

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
    clang-tools-extra/docs/ReleaseNotes.rst
    
clang-tools-extra/test/clang-tidy/checkers/readability/redundant-parentheses.cpp

Removed: 
    


################################################################################
diff  --git 
a/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
index 639e183f434b2..a0ae94891f552 100644
--- a/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
@@ -73,7 +73,10 @@ void RedundantParenthesesCheck::registerMatchers(MatchFinder 
*Finder) {
                     parenExpr(), ConstantExpr,
                     declRefExpr(to(namedDecl(unless(
                         matchers::matchesAnyListedRegexName(AllowedDecls))))),
-                    memberExpr(), callExpr(unless(cxxOperatorCallExpr())))),
+                    memberExpr(),
+                    callExpr(unless(cxxOperatorCallExpr(
+                        unless(hasAnyOperatorName("()", "[]"))))),
+                    arraySubscriptExpr())),
                 unless(anyOf(isInMacro(),
                              // sizeof(...) is common used.
                              hasParent(unaryExprOrTypeTraitExpr()))))

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 3891dbd21024a..f43b90f5a2386 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -895,6 +895,8 @@ Changes in existing checks
   - Fixed a bug where clients that apply fix-its without 
:program:`clang-tidy`'s
     cleanup could produce invalid code by joining adjacent tokens.
 
+  - Added support for parentheses around subscript operators (``(E1[E2])`` -> 
``E1[E2]``).
+
 - Improved :doc:`readability-redundant-preprocessor
   <clang-tidy/checks/readability/redundant-preprocessor>` check by fixing a
   false positive for nested ``#if`` directives using 
diff erent builtin

diff  --git 
a/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-parentheses.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-parentheses.cpp
index 83c6f94d1a09f..bd6799e512087 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-parentheses.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-parentheses.cpp
@@ -97,6 +97,9 @@ struct Foo
     Y y{};
     return y;
   }
+
+  bool operator()(int);
+  bool operator[](int);
 };
 
 void memberExpr() {
@@ -122,6 +125,24 @@ void memberExpr() {
   }
 }
 
+void call(bool (&func)(), Foo f) {
+  if ((func())) {}
+  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant parentheses around 
expression [readability-redundant-parentheses]
+  // CHECK-FIXES:    if (func()) {}
+  if ((f(1))) {}
+  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant parentheses around 
expression [readability-redundant-parentheses]
+  // CHECK-FIXES:    if (f(1)) {}
+}
+
+void subscript(bool *arr, Foo f) {
+  if ((arr[1])) {}
+  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant parentheses around 
expression [readability-redundant-parentheses]
+  // CHECK-FIXES:    if (arr[1]) {}
+  if ((f[1])) {}
+  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant parentheses around 
expression [readability-redundant-parentheses]
+  // CHECK-FIXES:    if (f[1]) {}
+}
+
 enum class FoldLevel {
   None = 0x0,
   HeaderFlag = 0x2000,


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to