Skip to content

Commit 7e1efdd

Browse files
[clang-tidy] Add some suggestions from code review
Also, renamed from `bugprone-*` to `readability-*`
1 parent 540a43c commit 7e1efdd

File tree

13 files changed

+353
-71
lines changed

13 files changed

+353
-71
lines changed

clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
#include "ImplicitWideningOfMultiplicationResultCheck.h"
3434
#include "InaccurateEraseCheck.h"
3535
#include "IncDecInConditionsCheck.h"
36-
#include "InconsistentIfelseBracesCheck.h"
3736
#include "IncorrectEnableIfCheck.h"
3837
#include "IncorrectEnableSharedFromThisCheck.h"
3938
#include "IncorrectRoundingsCheck.h"
@@ -151,8 +150,6 @@ class BugproneModule : public ClangTidyModule {
151150
"bugprone-implicit-widening-of-multiplication-result");
152151
CheckFactories.registerCheck<InaccurateEraseCheck>(
153152
"bugprone-inaccurate-erase");
154-
CheckFactories.registerCheck<InconsistentIfelseBracesCheck>(
155-
"bugprone-inconsistent-ifelse-braces");
156153
CheckFactories.registerCheck<IncorrectEnableIfCheck>(
157154
"bugprone-incorrect-enable-if");
158155
CheckFactories.registerCheck<IncorrectEnableSharedFromThisCheck>(

clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ add_clang_library(clangTidyBugproneModule STATIC
2828
ForwardingReferenceOverloadCheck.cpp
2929
ImplicitWideningOfMultiplicationResultCheck.cpp
3030
InaccurateEraseCheck.cpp
31-
InconsistentIfelseBracesCheck.cpp
3231
IncorrectEnableIfCheck.cpp
3332
IncorrectEnableSharedFromThisCheck.cpp
3433
InvalidEnumDefaultInitializationCheck.cpp

clang-tools-extra/clang-tidy/readability/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ add_clang_library(clangTidyReadabilityModule STATIC
2424
IdentifierLengthCheck.cpp
2525
IdentifierNamingCheck.cpp
2626
ImplicitBoolConversionCheck.cpp
27+
InconsistentIfelseBracesCheck.cpp
2728
RedundantInlineSpecifierCheck.cpp
2829
InconsistentDeclarationParameterNameCheck.cpp
2930
IsolateDeclarationCheck.cpp

clang-tools-extra/clang-tidy/bugprone/InconsistentIfelseBracesCheck.cpp renamed to clang-tools-extra/clang-tidy/readability/InconsistentIfelseBracesCheck.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
using namespace clang::ast_matchers;
1515

16-
namespace clang::tidy::bugprone {
16+
namespace clang::tidy::readability {
1717

1818
/// Check that at least one branch of the \p If statement is a \c CompoundStmt.
1919
static bool shouldHaveBraces(const IfStmt *If) {
@@ -33,11 +33,10 @@ static bool shouldHaveBraces(const IfStmt *If) {
3333

3434
void InconsistentIfelseBracesCheck::registerMatchers(MatchFinder *Finder) {
3535
Finder->addMatcher(
36-
traverse(TK_IgnoreUnlessSpelledInSource,
37-
ifStmt(hasElse(anything()),
38-
unless(isConsteval()), // 'if consteval' always has braces
39-
unless(hasParent(ifStmt())))
40-
.bind("if_stmt")),
36+
ifStmt(hasElse(anything()),
37+
unless(isConsteval()), // 'if consteval' always has braces
38+
unless(hasParent(ifStmt())))
39+
.bind("if_stmt"),
4140
this);
4241
}
4342

@@ -50,40 +49,41 @@ void InconsistentIfelseBracesCheck::check(
5049
}
5150

5251
void InconsistentIfelseBracesCheck::checkIfStmt(
53-
const ast_matchers::MatchFinder::MatchResult &Result, const IfStmt *If) {
52+
const MatchFinder::MatchResult &Result, const IfStmt *If) {
5453
const Stmt *Then = If->getThen();
5554
if (const auto *NestedIf = dyn_cast<const IfStmt>(Then)) {
5655
// If the then-branch is a nested IfStmt, first we need to add braces to
5756
// it, then we need to check the inner IfStmt.
58-
checkStmt(Result, If->getThen(), If->getRParenLoc(), If->getElseLoc());
57+
emitDiagnostic(Result, If->getThen(), If->getRParenLoc(), If->getElseLoc());
5958
if (shouldHaveBraces(NestedIf))
6059
checkIfStmt(Result, NestedIf);
6160
} else if (!isa<CompoundStmt>(Then)) {
62-
checkStmt(Result, If->getThen(), If->getRParenLoc(), If->getElseLoc());
61+
emitDiagnostic(Result, If->getThen(), If->getRParenLoc(), If->getElseLoc());
6362
}
6463

6564
if (const Stmt *const Else = If->getElse()) {
6665
if (const auto *NestedIf = dyn_cast<const IfStmt>(Else))
6766
checkIfStmt(Result, NestedIf);
6867
else if (!isa<CompoundStmt>(Else))
69-
checkStmt(Result, If->getElse(), If->getElseLoc());
68+
emitDiagnostic(Result, If->getElse(), If->getElseLoc());
7069
}
7170
}
7271

73-
void InconsistentIfelseBracesCheck::checkStmt(
74-
const ast_matchers::MatchFinder::MatchResult &Result, const Stmt *S,
72+
void InconsistentIfelseBracesCheck::emitDiagnostic(
73+
const MatchFinder::MatchResult &Result, const Stmt *S,
7574
SourceLocation StartLoc, SourceLocation EndLocHint) {
7675
const SourceManager &SM = *Result.SourceManager;
7776
const LangOptions &LangOpts = Result.Context->getLangOpts();
7877

79-
const utils::BraceInsertionHints Hints =
80-
utils::getBraceInsertionsHints(S, LangOpts, SM, StartLoc, EndLocHint);
81-
if (Hints) {
82-
DiagnosticBuilder Diag = diag(Hints.DiagnosticPos, "<message>");
83-
if (Hints.offersFixIts()) {
84-
Diag << Hints.openingBraceFixIt() << Hints.closingBraceFixIt();
85-
}
78+
if (!StartLoc.isMacroID()) {
79+
const utils::BraceInsertionHints Hints =
80+
utils::getBraceInsertionsHints(S, LangOpts, SM, StartLoc, EndLocHint);
81+
assert(Hints && Hints.offersFixIts() && "Expected hints or fix-its");
82+
diag(Hints.DiagnosticPos, "<message>")
83+
<< Hints.openingBraceFixIt() << Hints.closingBraceFixIt();
84+
} else {
85+
diag(StartLoc, "<message-for-macro-expansions>") << StartLoc.isMacroID();
8686
}
8787
}
8888

89-
} // namespace clang::tidy::bugprone
89+
} // namespace clang::tidy::readability

clang-tools-extra/clang-tidy/bugprone/InconsistentIfelseBracesCheck.h renamed to clang-tools-extra/clang-tidy/readability/InconsistentIfelseBracesCheck.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,38 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_INCONSISTENTIFELSEBRACESCHECK_H
10-
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_INCONSISTENTIFELSEBRACESCHECK_H
9+
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_INCONSISTENTIFELSEBRACESCHECK_H
10+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_INCONSISTENTIFELSEBRACESCHECK_H
1111

1212
#include "../ClangTidyCheck.h"
13+
#include "clang/AST/ASTTypeTraits.h"
14+
#include <optional>
1315

14-
namespace clang::tidy::bugprone {
16+
namespace clang::tidy::readability {
1517

1618
/// Detects `if`/`else` statements where one branch uses braces and the other
1719
/// does not.
1820
///
1921
/// For the user-facing documentation see:
20-
/// https://clang.llvm.org/extra/clang-tidy/checks/bugprone/inconsistent-ifelse-braces.html
22+
/// https://clang.llvm.org/extra/clang-tidy/checks/readability/inconsistent-ifelse-braces.html
2123
class InconsistentIfelseBracesCheck : public ClangTidyCheck {
2224
public:
2325
InconsistentIfelseBracesCheck(StringRef Name, ClangTidyContext *Context)
2426
: ClangTidyCheck(Name, Context) {}
2527
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
2628
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
29+
std::optional<TraversalKind> getCheckTraversalKind() const override {
30+
return TK_IgnoreUnlessSpelledInSource;
31+
}
2732

2833
private:
2934
void checkIfStmt(const ast_matchers::MatchFinder::MatchResult &Result,
3035
const IfStmt *If);
31-
void checkStmt(const ast_matchers::MatchFinder::MatchResult &Result,
32-
const Stmt *S, SourceLocation StartLoc,
33-
SourceLocation EndLocHint = {});
36+
void emitDiagnostic(const ast_matchers::MatchFinder::MatchResult &Result,
37+
const Stmt *S, SourceLocation StartLoc,
38+
SourceLocation EndLocHint = {});
3439
};
3540

36-
} // namespace clang::tidy::bugprone
41+
} // namespace clang::tidy::readability
3742

38-
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_INCONSISTENTIFELSEBRACESCHECK_H
43+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_INCONSISTENTIFELSEBRACESCHECK_H

clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "IdentifierNamingCheck.h"
3131
#include "ImplicitBoolConversionCheck.h"
3232
#include "InconsistentDeclarationParameterNameCheck.h"
33+
#include "InconsistentIfelseBracesCheck.h"
3334
#include "IsolateDeclarationCheck.h"
3435
#include "MagicNumbersCheck.h"
3536
#include "MakeMemberFunctionConstCheck.h"
@@ -110,6 +111,8 @@ class ReadabilityModule : public ClangTidyModule {
110111
"readability-identifier-naming");
111112
CheckFactories.registerCheck<ImplicitBoolConversionCheck>(
112113
"readability-implicit-bool-conversion");
114+
CheckFactories.registerCheck<InconsistentIfelseBracesCheck>(
115+
"readability-inconsistent-ifelse-braces");
113116
CheckFactories.registerCheck<MathMissingParenthesesCheck>(
114117
"readability-math-missing-parentheses");
115118
CheckFactories.registerCheck<RedundantInlineSpecifierCheck>(

0 commit comments

Comments
 (0)