-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[clang-tidy] add readability-redundant-parentheses #159911
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
HerrCai0907
merged 11 commits into
llvm:main
from
HerrCai0907:RedundantParenthesesCheck
Sep 25, 2025
+192
−0
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ad8fc03
[clang-tidy] add readability-redundant-parentheses
HerrCai0907 863331f
fix reviwe
HerrCai0907 4799052
Merge remote-tracking branch 'origin/main' into RedundantParenthesesC…
HerrCai0907 92db784
Update clang-tools-extra/clang-tidy/readability/RedundantParenthesesC…
HerrCai0907 42aab95
fix review
HerrCai0907 e210b4f
Update clang-tools-extra/docs/clang-tidy/checks/readability/redundant…
HerrCai0907 401490d
Merge remote-tracking branch 'origin/main' into RedundantParenthesesC…
HerrCai0907 4ecee58
Update clang-tools-extra/docs/clang-tidy/checks/readability/redundant…
HerrCai0907 b03d891
Update clang-tools-extra/docs/clang-tidy/checks/readability/redundant…
HerrCai0907 edb5401
fix review
HerrCai0907 347cfaf
Merge branch 'RedundantParenthesesCheck' of github.com:HerrCai0907/ll…
HerrCai0907 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "RedundantParenthesesCheck.h" | ||
#include "clang/AST/Expr.h" | ||
#include "clang/ASTMatchers/ASTMatchFinder.h" | ||
#include "clang/ASTMatchers/ASTMatchers.h" | ||
#include "clang/ASTMatchers/ASTMatchersMacros.h" | ||
#include <cassert> | ||
|
||
using namespace clang::ast_matchers; | ||
|
||
namespace clang::tidy::readability { | ||
|
||
namespace { | ||
|
||
AST_MATCHER_P(ParenExpr, subExpr, ast_matchers::internal::Matcher<Expr>, | ||
InnerMatcher) { | ||
return InnerMatcher.matches(*Node.getSubExpr(), Finder, Builder); | ||
} | ||
|
||
AST_MATCHER(ParenExpr, isInMacro) { | ||
const Expr *E = Node.getSubExpr(); | ||
return Node.getLParen().isMacroID() || Node.getRParen().isMacroID() || | ||
E->getBeginLoc().isMacroID() || E->getEndLoc().isMacroID(); | ||
} | ||
|
||
} // namespace | ||
|
||
void RedundantParenthesesCheck::registerMatchers(MatchFinder *Finder) { | ||
const auto ConstantExpr = | ||
expr(anyOf(integerLiteral(), floatLiteral(), characterLiteral(), | ||
cxxBoolLiteral(), stringLiteral(), cxxNullPtrLiteralExpr())); | ||
Finder->addMatcher( | ||
parenExpr(subExpr(anyOf(parenExpr(), ConstantExpr, declRefExpr())), | ||
unless(anyOf(isInMacro(), | ||
// sizeof(...) is common used. | ||
hasParent(unaryExprOrTypeTraitExpr())))) | ||
.bind("dup"), | ||
this); | ||
} | ||
|
||
void RedundantParenthesesCheck::check(const MatchFinder::MatchResult &Result) { | ||
const auto *PE = Result.Nodes.getNodeAs<ParenExpr>("dup"); | ||
diag(PE->getBeginLoc(), "redundant parentheses around expression") | ||
<< FixItHint::CreateRemoval(PE->getLParen()) | ||
<< FixItHint::CreateRemoval(PE->getRParen()); | ||
} | ||
|
||
} // namespace clang::tidy::readability |
34 changes: 34 additions & 0 deletions
34
clang-tools-extra/clang-tidy/readability/RedundantParenthesesCheck.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_REDUNDANTPARENTHESESCHECK_H | ||
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_REDUNDANTPARENTHESESCHECK_H | ||
|
||
#include "../ClangTidyCheck.h" | ||
#include "clang/Basic/LangOptions.h" | ||
|
||
namespace clang::tidy::readability { | ||
|
||
/// Detect redundant parentheses. | ||
/// | ||
/// For the user-facing documentation see: | ||
/// https://clang.llvm.org/extra/clang-tidy/checks/readability/redundant-parentheses.html | ||
class RedundantParenthesesCheck : public ClangTidyCheck { | ||
public: | ||
RedundantParenthesesCheck(StringRef Name, ClangTidyContext *Context) | ||
: ClangTidyCheck(Name, Context) {} | ||
void registerMatchers(ast_matchers::MatchFinder *Finder) override; | ||
void check(const ast_matchers::MatchFinder::MatchResult &Result) override; | ||
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { | ||
return LangOpts.CPlusPlus | LangOpts.C99; | ||
} | ||
}; | ||
|
||
} // namespace clang::tidy::readability | ||
|
||
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_REDUNDANTPARENTHESESCHECK_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
clang-tools-extra/docs/clang-tidy/checks/readability/redundant-parentheses.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
.. title:: clang-tidy - readability-redundant-parentheses | ||
|
||
readability-redundant-parentheses | ||
================================= | ||
|
||
Detect redundant parentheses. | ||
|
||
When modifying code, one often forgets to remove the corresponding parentheses. | ||
This results in overly lengthy code. When the expression is complex, finding | ||
the matching parentheses becomes particularly difficult. | ||
|
||
Example | ||
------- | ||
|
||
.. code-block:: c++ | ||
|
||
(1); | ||
((a + 2)) * 3; | ||
(a); | ||
("aaa"); | ||
|
||
Currently this check does not take into account the precedence of operations. | ||
Even if the expression within the parentheses has a higher priority than that | ||
outside the parentheses. In other words, removing the parentheses will not | ||
affect the semantics. | ||
|
||
.. code-block:: c++ | ||
|
||
int a = (1 * 2) + 3; // no warning |
64 changes: 64 additions & 0 deletions
64
clang-tools-extra/test/clang-tidy/checkers/readability/redundant-parentheses.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// RUN: %check_clang_tidy %s readability-redundant-parentheses %t | ||
vbvictor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
void parenExpr() { | ||
1 + 1; | ||
(1 + 1); | ||
((1 + 1)); | ||
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: redundant parentheses around expression [readability-redundant-parentheses] | ||
// CHECK-FIXES: (1 + 1); | ||
(((1 + 1))); | ||
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: redundant parentheses around expression [readability-redundant-parentheses] | ||
// CHECK-MESSAGES: :[[@LINE-2]]:4: warning: redundant parentheses around expression [readability-redundant-parentheses] | ||
// CHECK-FIXES: (1 + 1); | ||
((((1 + 1)))); | ||
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: redundant parentheses around expression [readability-redundant-parentheses] | ||
// CHECK-MESSAGES: :[[@LINE-2]]:4: warning: redundant parentheses around expression [readability-redundant-parentheses] | ||
// CHECK-MESSAGES: :[[@LINE-3]]:5: warning: redundant parentheses around expression [readability-redundant-parentheses] | ||
// CHECK-FIXES: (1 + 1); | ||
} | ||
|
||
#define EXP (1 + 1) | ||
#define PAREN(e) (e) | ||
void parenExprWithMacro() { | ||
EXP; // 1 | ||
(EXP); // 2 | ||
((EXP)); // 3 | ||
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: redundant parentheses around expression [readability-redundant-parentheses] | ||
// CHECK-FIXES: (EXP); // 3 | ||
PAREN((1)); | ||
} | ||
|
||
void constant() { | ||
(1); | ||
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: redundant parentheses around expression [readability-redundant-parentheses] | ||
// CHECK-FIXES: 1; | ||
(1.0); | ||
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: redundant parentheses around expression [readability-redundant-parentheses] | ||
// CHECK-FIXES: 1.0; | ||
(true); | ||
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: redundant parentheses around expression [readability-redundant-parentheses] | ||
// CHECK-FIXES: true; | ||
(','); | ||
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: redundant parentheses around expression [readability-redundant-parentheses] | ||
// CHECK-FIXES: ','; | ||
("v4"); | ||
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: redundant parentheses around expression [readability-redundant-parentheses] | ||
// CHECK-FIXES: "v4"; | ||
(nullptr); | ||
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: redundant parentheses around expression [readability-redundant-parentheses] | ||
// CHECK-FIXES: nullptr; | ||
} | ||
|
||
void declRefExpr(int a) { | ||
(a); | ||
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: redundant parentheses around expression [readability-redundant-parentheses] | ||
// CHECK-FIXES: a; | ||
} | ||
|
||
void exceptions() { | ||
sizeof(1); | ||
alignof(2); | ||
HerrCai0907 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
alignof((3)); | ||
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant parentheses around expression [readability-redundant-parentheses] | ||
// CHECK-FIXES: alignof(3); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we set
isLanguageVersionSupported
toCPlusPlus || C99
otherwise the check may run on ObjC, which may have different semantics I guess..I wonder, could we make the default to
CPlusPlus || C99
instead oftrue
here:llvm-project/clang-tools-extra/clang-tidy/ClangTidyCheck.h
Lines 68 to 70 in c12f08f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It needs RFC IMO. I have never written OC so I don't know the ecosystem of OC. Is clang-tidy a common used tools in OC's world?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've never used OC too, so we'd definitely need an RFC for that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the goal is to disable the check for OC, shouldn't we just do
return !LangOpts.ObjC
? It expresses intent more clearly. (Although ifObjC
isn't set for Objective-C++, that won't work.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have further discussion in https://discourse.llvm.org/t/rfc-clang-tidy-which-language-should-be-supported-by-default/88424