Skip to content

Commit eb72006

Browse files
[clang-tidy] Add some more tests
1 parent dfce0a0 commit eb72006

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
#include "InconsistentIfElseBracesCheck.h"
1010
#include "../utils/BracesAroundStatement.h"
1111
#include "clang/AST/ASTContext.h"
12+
#include "clang/AST/Stmt.h"
1213
#include "clang/ASTMatchers/ASTMatchers.h"
14+
#include "clang/Basic/SourceLocation.h"
15+
#include "clang/Lex/Lexer.h"
1316

1417
using namespace clang::ast_matchers;
1518

@@ -55,17 +58,19 @@ void InconsistentIfElseBracesCheck::checkIfStmt(
5558
// If the then-branch is a nested IfStmt, first we need to add braces to
5659
// it, then we need to check the inner IfStmt.
5760
emitDiagnostic(Result, If->getThen(), If->getRParenLoc(), If->getElseLoc());
61+
5862
if (shouldHaveBraces(NestedIf))
5963
checkIfStmt(Result, NestedIf);
6064
} else if (!isa<CompoundStmt>(Then)) {
61-
emitDiagnostic(Result, If->getThen(), If->getRParenLoc(), If->getElseLoc());
65+
emitDiagnostic(Result, Then, If->getRParenLoc(), If->getElseLoc());
6266
}
6367

6468
if (const Stmt *const Else = If->getElse()) {
6569
if (const auto *NestedIf = dyn_cast<const IfStmt>(Else))
6670
checkIfStmt(Result, NestedIf);
67-
else if (!isa<CompoundStmt>(Else))
71+
else if (!isa<CompoundStmt>(Else)) {
6872
emitDiagnostic(Result, If->getElse(), If->getElseLoc());
73+
}
6974
}
7075
}
7176

@@ -74,7 +79,6 @@ void InconsistentIfElseBracesCheck::emitDiagnostic(
7479
SourceLocation StartLoc, SourceLocation EndLocHint) {
7580
const SourceManager &SM = *Result.SourceManager;
7681
const LangOptions &LangOpts = Result.Context->getLangOpts();
77-
7882
if (!StartLoc.isMacroID()) {
7983
const utils::BraceInsertionHints Hints =
8084
utils::getBraceInsertionsHints(S, LangOpts, SM, StartLoc, EndLocHint);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// RUN: %check_clang_tidy -std=c++20-or-later %s readability-inconsistent-ifelse-braces %t
2+
3+
// Positive tests.
4+
void f(bool b) {
5+
if (b) [[likely]] return;
6+
else {
7+
}
8+
// CHECK-MESSAGES: :[[@LINE-3]]:9: warning: <message> [readability-inconsistent-ifelse-braces]
9+
// CHECK-FIXES: if (b) { {{[[][[]}}likely{{[]][]]}} return;
10+
// CHECK-FIXES: } else {
11+
12+
if (b) {
13+
} else [[unlikely]]
14+
return;
15+
// CHECK-MESSAGES: :[[@LINE-2]]:9: warning: <message> [readability-inconsistent-ifelse-braces]
16+
// CHECK-FIXES: } else { {{[[][[]}}unlikely{{[]][]]}}
17+
}
18+
19+
// Negative tests.
20+
void g(bool b) {
21+
if (b) {
22+
return;
23+
}
24+
25+
if (b) { [[likely]]
26+
return;
27+
}
28+
29+
if (b) { [[unlikely]]
30+
return;
31+
} else { [[likely]]
32+
return;
33+
}
34+
35+
if (b) [[likely]]
36+
return;
37+
38+
if (b) [[unlikely]]
39+
return;
40+
else [[likely]]
41+
return;
42+
}

clang-tools-extra/test/clang-tidy/checkers/readability/inconsistent-ifelse-braces.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// RUN: %check_clang_tidy -std=c++98-or-later %s readability-inconsistent-ifelse-braces %t
22

3+
#define MACRO_COND(x) cond(x)
4+
#define MACRO_FUN (void)0
5+
36
bool cond(const char *) { return false; }
47
void do_something(const char *) {}
58

@@ -101,6 +104,18 @@ void f() {
101104
// CHECK-MESSAGES: :[[@LINE-2]]:28: warning: <message> [readability-inconsistent-ifelse-braces]
102105
// CHECK-FIXES: } else if (cond("if5.1")) {
103106
// CHECK-FIXES: }
107+
108+
if (MACRO_COND("if6")) MACRO_FUN;
109+
else {
110+
}
111+
// CHECK-MESSAGES: :[[@LINE-3]]:20: warning: <message> [readability-inconsistent-ifelse-braces]
112+
// CHECK-FIXES: if (MACRO_COND("if6")) { MACRO_FUN;
113+
// CHECK-FIXES: } else {
114+
115+
if (MACRO_COND("if6")) {
116+
} else MACRO_FUN;
117+
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: <message> [readability-inconsistent-ifelse-braces]
118+
// CHECK-FIXES: } else { MACRO_FUN;
104119
}
105120

106121
// Negative tests.

0 commit comments

Comments
 (0)