Skip to content

Commit 28a213b

Browse files
[clang-tidy] Update documentation for
'bugprone-inconsistent-ifelse-braces'
1 parent 18e6ae8 commit 28a213b

File tree

6 files changed

+49
-18
lines changed

6 files changed

+49
-18
lines changed

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
//===----------------------------------------------------------------------===//
32
//
43
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
@@ -47,10 +46,6 @@ void InconsistentIfelseBracesCheck::check(
4746
const auto *MatchedIf = Result.Nodes.getNodeAs<IfStmt>("if_stmt");
4847
if (!shouldHaveBraces(MatchedIf))
4948
return;
50-
51-
// TODO: Test behavior with macros. This might be the reason why
52-
// readability-braces-around-statements defines a findRParenLoc() function
53-
// rather than using IfStmt/WhileStmt::getRParenLoc().
5449
checkIfStmt(Result, MatchedIf);
5550
}
5651

@@ -62,17 +57,15 @@ void InconsistentIfelseBracesCheck::checkIfStmt(
6257
// it, then we need to check the inner IfStmt.
6358
checkStmt(Result, If->getThen(), If->getRParenLoc(), If->getElseLoc());
6459
if (shouldHaveBraces(NestedIf))
65-
return checkIfStmt(Result, NestedIf);
66-
}
67-
68-
if (!isa<CompoundStmt>(Then))
60+
checkIfStmt(Result, NestedIf);
61+
} else if (!isa<CompoundStmt>(Then)) {
6962
checkStmt(Result, If->getThen(), If->getRParenLoc(), If->getElseLoc());
63+
}
7064

7165
if (const Stmt *const Else = If->getElse()) {
7266
if (const auto *NestedIf = dyn_cast<const IfStmt>(Else))
73-
return checkIfStmt(Result, NestedIf);
74-
75-
if (!isa<CompoundStmt>(Else))
67+
checkIfStmt(Result, NestedIf);
68+
else if (!isa<CompoundStmt>(Else))
7669
checkStmt(Result, If->getElse(), If->getElseLoc());
7770
}
7871
}
@@ -92,4 +85,5 @@ void InconsistentIfelseBracesCheck::checkStmt(
9285
}
9386
}
9487
}
88+
9589
} // namespace clang::tidy::bugprone

clang-tools-extra/clang-tidy/bugprone/InconsistentIfelseBracesCheck.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
//===----------------------------------------------------------------------===//
32
//
43
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
@@ -14,7 +13,8 @@
1413

1514
namespace clang::tidy::bugprone {
1615

17-
/// FIXME: Write a short description.
16+
/// Detects `if`/`else` statements where one branch uses braces and the other
17+
/// does not.
1818
///
1919
/// For the user-facing documentation see:
2020
/// http://clang.llvm.org/extra/clang-tidy/checks/bugprone/inconsistent-ifelse-braces.html

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ New checks
160160
- New :doc:`bugprone-inconsistent-ifelse-braces
161161
<clang-tidy/checks/bugprone/inconsistent-ifelse-braces>` check.
162162

163-
FIXME: Write a short description.
163+
Detects ``if``/``else`` statements where one branch uses braces and the other
164+
does not.
164165

165166
- New :doc:`bugprone-invalid-enum-default-initialization
166167
<clang-tidy/checks/bugprone/invalid-enum-default-initialization>` check.

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,40 @@
33
bugprone-inconsistent-ifelse-braces
44
===================================
55

6-
FIXME: Describe what patterns does the check detect and why. Give examples.
6+
Detects ``if``/``else`` statements where one branch uses braces and the other
7+
does not.
8+
9+
Before:
10+
11+
.. code-block:: c++
12+
13+
if (condition) {
14+
statement;
15+
} else
16+
statement;
17+
18+
if (condition)
19+
statement;
20+
21+
if (condition)
22+
statement;
23+
else
24+
statement;
25+
26+
After:
27+
28+
.. code-block:: c++
29+
30+
if (condition) {
31+
statement;
32+
} else {
33+
statement;
34+
}
35+
36+
if (condition)
37+
statement;
38+
39+
if (condition)
40+
statement;
41+
else
42+
statement;

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,7 @@ 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>`,
104+
:doc:`bugprone-inconsistent-ifelse-braces <bugprone/inconsistent-ifelse-braces>`, "Yes"
105105
:doc:`bugprone-incorrect-enable-if <bugprone/incorrect-enable-if>`, "Yes"
106106
:doc:`bugprone-incorrect-enable-shared-from-this <bugprone/incorrect-enable-shared-from-this>`, "Yes"
107107
:doc:`bugprone-incorrect-roundings <bugprone/incorrect-roundings>`,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,4 @@ void g() {
120120
do_something("elseif-single-line");
121121
else
122122
do_something("else-single-line");
123-
}
123+
}

0 commit comments

Comments
 (0)