Skip to content

Commit b660692

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

File tree

12 files changed

+76
-71
lines changed

12 files changed

+76
-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>(

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ Improvements to clang-tidy
157157
New checks
158158
^^^^^^^^^^
159159

160-
- New :doc:`bugprone-inconsistent-ifelse-braces
161-
<clang-tidy/checks/bugprone/inconsistent-ifelse-braces>` check.
160+
- New :doc:`readability-inconsistent-ifelse-braces
161+
<clang-tidy/checks/readability/inconsistent-ifelse-braces>` check.
162162

163163
Detects ``if``/``else`` statements where one branch uses braces and the other
164164
does not.

clang-tools-extra/docs/clang-tidy/checks/list.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ Clang-Tidy Checks
101101
:doc:`bugprone-implicit-widening-of-multiplication-result <bugprone/implicit-widening-of-multiplication-result>`, "Yes"
102102
:doc:`bugprone-inaccurate-erase <bugprone/inaccurate-erase>`, "Yes"
103103
:doc:`bugprone-inc-dec-in-conditions <bugprone/inc-dec-in-conditions>`,
104-
:doc:`bugprone-inconsistent-ifelse-braces <bugprone/inconsistent-ifelse-braces>`, "Yes"
105104
:doc:`bugprone-incorrect-enable-if <bugprone/incorrect-enable-if>`, "Yes"
106105
:doc:`bugprone-incorrect-enable-shared-from-this <bugprone/incorrect-enable-shared-from-this>`, "Yes"
107106
:doc:`bugprone-incorrect-roundings <bugprone/incorrect-roundings>`,
@@ -387,6 +386,7 @@ Clang-Tidy Checks
387386
:doc:`readability-identifier-naming <readability/identifier-naming>`, "Yes"
388387
:doc:`readability-implicit-bool-conversion <readability/implicit-bool-conversion>`, "Yes"
389388
:doc:`readability-inconsistent-declaration-parameter-name <readability/inconsistent-declaration-parameter-name>`, "Yes"
389+
:doc:`readability-inconsistent-ifelse-braces <readability/inconsistent-ifelse-braces>`, "Yes"
390390
:doc:`readability-isolate-declaration <readability/isolate-declaration>`, "Yes"
391391
:doc:`readability-magic-numbers <readability/magic-numbers>`,
392392
:doc:`readability-make-member-function-const <readability/make-member-function-const>`, "Yes"

clang-tools-extra/docs/clang-tidy/checks/bugprone/inconsistent-ifelse-braces.rst renamed to clang-tools-extra/docs/clang-tidy/checks/readability/inconsistent-ifelse-braces.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
.. title:: clang-tidy - bugprone-inconsistent-ifelse-braces
1+
.. title:: clang-tidy - readability-inconsistent-ifelse-braces
22

3-
bugprone-inconsistent-ifelse-braces
4-
===================================
3+
readability-inconsistent-ifelse-braces
4+
======================================
55

66
Detects ``if``/``else`` statements where one branch uses braces and the other
77
does not.
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %check_clang_tidy -std=c++23-or-later %s bugprone-inconsistent-ifelse-braces %t
1+
// RUN: %check_clang_tidy -std=c++23-or-later %s readability-inconsistent-ifelse-braces %t
22

33
bool cond(const char *) { return false; }
44
void do_something(const char *) {}
@@ -10,7 +10,7 @@ void f() {
1010
do_something("if-single-line");
1111
else {
1212
}
13-
// CHECK-MESSAGES: :[[@LINE-4]]:21: warning: <message> [bugprone-inconsistent-ifelse-braces]
13+
// CHECK-MESSAGES: :[[@LINE-4]]:21: warning: <message> [readability-inconsistent-ifelse-braces]
1414
// CHECK-FIXES: if (cond("if1")) {
1515
// CHECK-FIXES: } else {
1616
}
@@ -20,14 +20,14 @@ void f() {
2020
do_something("if-single-line");
2121
else {
2222
}
23-
// CHECK-MESSAGES: :[[@LINE-4]]:21: warning: <message> [bugprone-inconsistent-ifelse-braces]
23+
// CHECK-MESSAGES: :[[@LINE-4]]:21: warning: <message> [readability-inconsistent-ifelse-braces]
2424
// CHECK-FIXES: if (cond("if2")) {
2525
// CHECK-FIXES: } else {
2626
} else {
2727
if (cond("if2.1")) {
2828
} else
2929
do_something("else-single-line");
30-
// CHECK-MESSAGES: :[[@LINE-2]]:11: warning: <message> [bugprone-inconsistent-ifelse-braces]
30+
// CHECK-MESSAGES: :[[@LINE-2]]:11: warning: <message> [readability-inconsistent-ifelse-braces]
3131
// CHECK-FIXES: } else {
3232
// CHECK-FIXES: }
3333
}

0 commit comments

Comments
 (0)