-
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
Changes from 5 commits
ad8fc03
863331f
4799052
92db784
42aab95
e210b4f
401490d
4ecee58
b03d891
edb5401
347cfaf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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) { | ||
Finder->addMatcher( | ||
parenExpr(subExpr(anyOf(parenExpr(), integerLiteral(), floatLiteral(), | ||
characterLiteral(), cxxBoolLiteral(), | ||
stringLiteral(), 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 |
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; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we set I wonder, could we make the default to 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 commentThe 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 commentThe 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 commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||||
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 |
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 the code, one often forgets to remove the corresponding parentheses. | ||
HerrCai0907 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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 | ||
HerrCai0907 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
affect the semantic. | ||
HerrCai0907 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
.. code-block:: c++ | ||
|
||
int a = (1 * 2) + 3; // no warning |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// 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"; | ||
} | ||
|
||
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
|
||
} |
Uh oh!
There was an error while loading. Please reload this page.