-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[clang-tidy] Add new check 'bugprone-inconsistent-ifelse-braces' #162361
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
base: main
Are you sure you want to change the base?
Changes from 6 commits
5f61932
18e6ae8
117eb01
40570f9
6a652cd
540a43c
02aa926
cfb7875
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,89 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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 "InconsistentIfelseBracesCheck.h" | ||
| #include "../utils/BracesAroundStatement.h" | ||
| #include "clang/AST/ASTContext.h" | ||
| #include "clang/ASTMatchers/ASTMatchers.h" | ||
|
|
||
| using namespace clang::ast_matchers; | ||
|
|
||
| namespace clang::tidy::bugprone { | ||
|
|
||
| /// Check that at least one branch of the \p If statement is a \c CompoundStmt. | ||
| static bool shouldHaveBraces(const IfStmt *If) { | ||
| const Stmt *const Then = If->getThen(); | ||
| if (isa<CompoundStmt>(Then)) | ||
| return true; | ||
|
|
||
| if (const Stmt *const Else = If->getElse()) { | ||
| if (const auto *NestedIf = dyn_cast<const IfStmt>(Else)) | ||
| return shouldHaveBraces(NestedIf); | ||
|
|
||
| return isa<CompoundStmt>(Else); | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| void InconsistentIfelseBracesCheck::registerMatchers(MatchFinder *Finder) { | ||
| Finder->addMatcher( | ||
| traverse(TK_IgnoreUnlessSpelledInSource, | ||
| ifStmt(hasElse(anything()), | ||
| unless(isConsteval()), // 'if consteval' always has braces | ||
| unless(hasParent(ifStmt()))) | ||
| .bind("if_stmt")), | ||
localspook marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| this); | ||
| } | ||
|
|
||
| void InconsistentIfelseBracesCheck::check( | ||
| const MatchFinder::MatchResult &Result) { | ||
| const auto *MatchedIf = Result.Nodes.getNodeAs<IfStmt>("if_stmt"); | ||
| if (!shouldHaveBraces(MatchedIf)) | ||
| return; | ||
| checkIfStmt(Result, MatchedIf); | ||
| } | ||
|
|
||
| void InconsistentIfelseBracesCheck::checkIfStmt( | ||
| const ast_matchers::MatchFinder::MatchResult &Result, const IfStmt *If) { | ||
capitan-davide marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const Stmt *Then = If->getThen(); | ||
| if (const auto *NestedIf = dyn_cast<const IfStmt>(Then)) { | ||
| // If the then-branch is a nested IfStmt, first we need to add braces to | ||
| // it, then we need to check the inner IfStmt. | ||
| checkStmt(Result, If->getThen(), If->getRParenLoc(), If->getElseLoc()); | ||
| if (shouldHaveBraces(NestedIf)) | ||
| checkIfStmt(Result, NestedIf); | ||
| } else if (!isa<CompoundStmt>(Then)) { | ||
| checkStmt(Result, If->getThen(), If->getRParenLoc(), If->getElseLoc()); | ||
| } | ||
|
|
||
| if (const Stmt *const Else = If->getElse()) { | ||
| if (const auto *NestedIf = dyn_cast<const IfStmt>(Else)) | ||
| checkIfStmt(Result, NestedIf); | ||
| else if (!isa<CompoundStmt>(Else)) | ||
| checkStmt(Result, If->getElse(), If->getElseLoc()); | ||
| } | ||
| } | ||
|
|
||
| void InconsistentIfelseBracesCheck::checkStmt( | ||
capitan-davide marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const ast_matchers::MatchFinder::MatchResult &Result, const Stmt *S, | ||
| SourceLocation StartLoc, SourceLocation EndLocHint) { | ||
| const SourceManager &SM = *Result.SourceManager; | ||
| const LangOptions &LangOpts = Result.Context->getLangOpts(); | ||
|
|
||
| const utils::BraceInsertionHints Hints = | ||
| utils::getBraceInsertionsHints(S, LangOpts, SM, StartLoc, EndLocHint); | ||
| if (Hints) { | ||
| DiagnosticBuilder Diag = diag(Hints.DiagnosticPos, "<message>"); | ||
|
||
| if (Hints.offersFixIts()) { | ||
| Diag << Hints.openingBraceFixIt() << Hints.closingBraceFixIt(); | ||
| } | ||
capitan-davide marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| } // namespace clang::tidy::bugprone | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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_BUGPRONE_INCONSISTENTIFELSEBRACESCHECK_H | ||
| #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_INCONSISTENTIFELSEBRACESCHECK_H | ||
|
|
||
| #include "../ClangTidyCheck.h" | ||
|
|
||
| namespace clang::tidy::bugprone { | ||
|
|
||
| /// Detects `if`/`else` statements where one branch uses braces and the other | ||
| /// does not. | ||
| /// | ||
| /// For the user-facing documentation see: | ||
| /// https://clang.llvm.org/extra/clang-tidy/checks/bugprone/inconsistent-ifelse-braces.html | ||
| class InconsistentIfelseBracesCheck : public ClangTidyCheck { | ||
| public: | ||
| InconsistentIfelseBracesCheck(StringRef Name, ClangTidyContext *Context) | ||
| : ClangTidyCheck(Name, Context) {} | ||
| void registerMatchers(ast_matchers::MatchFinder *Finder) override; | ||
| void check(const ast_matchers::MatchFinder::MatchResult &Result) override; | ||
|
|
||
| private: | ||
| void checkIfStmt(const ast_matchers::MatchFinder::MatchResult &Result, | ||
| const IfStmt *If); | ||
| void checkStmt(const ast_matchers::MatchFinder::MatchResult &Result, | ||
| const Stmt *S, SourceLocation StartLoc, | ||
| SourceLocation EndLocHint = {}); | ||
| }; | ||
|
|
||
| } // namespace clang::tidy::bugprone | ||
|
|
||
| #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_INCONSISTENTIFELSEBRACESCHECK_H |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| .. title:: clang-tidy - bugprone-inconsistent-ifelse-braces | ||
|
|
||
| bugprone-inconsistent-ifelse-braces | ||
| =================================== | ||
|
|
||
| Detects ``if``/``else`` statements where one branch uses braces and the other | ||
| does not. | ||
|
|
||
| Before: | ||
|
|
||
| .. code-block:: c++ | ||
|
|
||
| if (condition) { | ||
| statement; | ||
| } else | ||
| statement; | ||
|
|
||
| if (condition) | ||
| statement; | ||
|
|
||
| if (condition) | ||
| statement; | ||
| else | ||
| statement; | ||
|
|
||
| After: | ||
|
|
||
| .. code-block:: c++ | ||
|
|
||
| if (condition) { | ||
| statement; | ||
| } else { | ||
| statement; | ||
| } | ||
|
|
||
| if (condition) | ||
| statement; | ||
|
|
||
| if (condition) | ||
| statement; | ||
| else | ||
| statement; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // RUN: %check_clang_tidy -std=c++23-or-later %s bugprone-inconsistent-ifelse-braces %t | ||
|
|
||
| bool cond(const char *) { return false; } | ||
| void do_something(const char *) {} | ||
|
|
||
| // Positive tests. | ||
| void f() { | ||
| if consteval { | ||
| if (cond("if1")) | ||
| do_something("if-single-line"); | ||
| else { | ||
| } | ||
| // CHECK-MESSAGES: :[[@LINE-4]]:21: warning: <message> [bugprone-inconsistent-ifelse-braces] | ||
| // CHECK-FIXES: if (cond("if1")) { | ||
| // CHECK-FIXES: } else { | ||
| } | ||
|
|
||
| if consteval { | ||
| if (cond("if2")) | ||
| do_something("if-single-line"); | ||
| else { | ||
| } | ||
| // CHECK-MESSAGES: :[[@LINE-4]]:21: warning: <message> [bugprone-inconsistent-ifelse-braces] | ||
| // CHECK-FIXES: if (cond("if2")) { | ||
| // CHECK-FIXES: } else { | ||
| } else { | ||
| if (cond("if2.1")) { | ||
| } else | ||
| do_something("else-single-line"); | ||
| // CHECK-MESSAGES: :[[@LINE-2]]:11: warning: <message> [bugprone-inconsistent-ifelse-braces] | ||
| // CHECK-FIXES: } else { | ||
| // CHECK-FIXES: } | ||
| } | ||
| } | ||
|
|
||
| // Negative tests. | ||
| void g() { | ||
| if consteval { | ||
| if (cond("if0")) { | ||
| do_something("if-single-line"); | ||
| } else if (cond("if0")) { | ||
| do_something("elseif-single-line"); | ||
| } else { | ||
| do_something("else-single-line"); | ||
| } | ||
| } else { | ||
| if (cond("if0.1")) | ||
| do_something("if-single-line"); | ||
| else if (cond("if0.1")) | ||
| do_something("elseif-single-line"); | ||
| else | ||
| do_something("else-single-line"); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| // RUN: %check_clang_tidy -std=c++17-or-later %s bugprone-inconsistent-ifelse-braces %t | ||
|
|
||
| constexpr bool cond(const char *) { return false; } | ||
| constexpr void do_something(const char *) {} | ||
|
|
||
| // Positive tests. | ||
| void f() { | ||
| if constexpr (cond("if0") /*comment*/) do_something("if-same-line"); | ||
| else { | ||
| } | ||
| // CHECK-MESSAGES: :[[@LINE-3]]:41: warning: <message> [bugprone-inconsistent-ifelse-braces] | ||
| // CHECK-FIXES: if constexpr (cond("if0") /*comment*/) { do_something("if-same-line"); | ||
| // CHECK-FIXES: } else { | ||
|
|
||
| if constexpr (cond("if0.1") /*comment*/) { | ||
| } else do_something("else-same-line"); | ||
| // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: <message> [bugprone-inconsistent-ifelse-braces] | ||
| // CHECK-FIXES: } else { do_something("else-same-line"); | ||
| // CHECK-FIXES: } | ||
|
|
||
| if constexpr (cond("if1")) | ||
| do_something("if-single-line"); | ||
| else { | ||
| } | ||
| // CHECK-MESSAGES: :[[@LINE-4]]:29: warning: <message> [bugprone-inconsistent-ifelse-braces] | ||
| // CHECK-FIXES: if constexpr (cond("if1")) { | ||
| // CHECK-FIXES: } else { | ||
|
|
||
| if constexpr (cond("if1.1")) { | ||
| } else | ||
| do_something("else-single-line"); | ||
| // CHECK-MESSAGES: :[[@LINE-2]]:9: warning: <message> [bugprone-inconsistent-ifelse-braces] | ||
| // CHECK-FIXES: } else { | ||
| // CHECK-FIXES: } | ||
|
|
||
| if constexpr (cond("if2") /*comment*/) | ||
| // some comment | ||
| do_something("if-multi-line"); | ||
| else { | ||
| } | ||
| // CHECK-MESSAGES: :[[@LINE-5]]:41: warning: <message> [bugprone-inconsistent-ifelse-braces] | ||
| // CHECK-FIXES: if constexpr (cond("if2") /*comment*/) { | ||
| // CHECK-FIXES: } else { | ||
|
|
||
| if constexpr (cond("if2.1") /*comment*/) { | ||
| } else | ||
| // some comment | ||
| do_something("else-multi-line"); | ||
| // CHECK-MESSAGES: :[[@LINE-3]]:9: warning: <message> [bugprone-inconsistent-ifelse-braces] | ||
| // CHECK-FIXES: } else { | ||
| // CHECK-FIXES: } | ||
|
|
||
| if constexpr (cond("if3")) do_something("elseif-same-line"); | ||
| else if constexpr (cond("if3")) { | ||
| } else { | ||
| } | ||
| // CHECK-MESSAGES: :[[@LINE-4]]:29: warning: <message> [bugprone-inconsistent-ifelse-braces] | ||
| // CHECK-FIXES: if constexpr (cond("if3")) { do_something("elseif-same-line"); | ||
| // CHECK-FIXES: } else if constexpr (cond("if3")) { | ||
|
|
||
| if constexpr (cond("if3.1")) { | ||
| } else if constexpr (cond("if3.1")) do_something("elseif-same-line"); | ||
| else { | ||
| } | ||
| // CHECK-MESSAGES: :[[@LINE-3]]:38: warning: <message> [bugprone-inconsistent-ifelse-braces] | ||
| // CHECK-FIXES: } else if constexpr (cond("if3.1")) { do_something("elseif-same-line"); | ||
| // CHECK-FIXES: } else { | ||
|
|
||
| if constexpr (cond("if3.2")) { | ||
| } else if constexpr (cond("if3.2")) { | ||
| } else do_something("else-same-line"); | ||
| // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: <message> [bugprone-inconsistent-ifelse-braces] | ||
| // CHECK-FIXES: } else { do_something("else-same-line"); | ||
| // CHECK-FIXES: } | ||
|
|
||
| if constexpr (cond("if4-outer")) | ||
| if constexpr (cond("if4-inner")) | ||
| do_something("if-single-line"); | ||
| else { | ||
| } | ||
| else { | ||
| } | ||
| // CHECK-MESSAGES: :[[@LINE-7]]:35: warning: <message> [bugprone-inconsistent-ifelse-braces] | ||
| // CHECK-MESSAGES: :[[@LINE-7]]:37: warning: <message> [bugprone-inconsistent-ifelse-braces] | ||
| // CHECK-FIXES: if constexpr (cond("if4-outer")) { | ||
| // CHECK-FIXES: if constexpr (cond("if4-inner")) { | ||
| // CHECK-FIXES: } else { | ||
| // CHECK-FIXES: } else { | ||
|
|
||
| if constexpr (cond("if5")) | ||
| do_something("if-single-line"); | ||
| else if constexpr (cond("if5")) { | ||
| } | ||
| // CHECK-MESSAGES: :[[@LINE-4]]:29: warning: <message> [bugprone-inconsistent-ifelse-braces] | ||
| // CHECK-FIXES: if constexpr (cond("if5")) { | ||
| // CHECK-FIXES: } else if constexpr (cond("if5")) { | ||
|
|
||
| if constexpr (cond("if5.1")) { | ||
| } else if constexpr (cond("if5.1")) | ||
| do_something("elseif-single-line"); | ||
| // CHECK-MESSAGES: :[[@LINE-2]]:38: warning: <message> [bugprone-inconsistent-ifelse-braces] | ||
| // CHECK-FIXES: } else if constexpr (cond("if5.1")) { | ||
| // CHECK-FIXES: } | ||
| } | ||
|
|
||
| // Negative tests. | ||
| void g() { | ||
| if constexpr (cond("if0")) { | ||
| do_something("if-single-line"); | ||
| } else if constexpr (cond("if0")) { | ||
| do_something("elseif-single-line"); | ||
| } else { | ||
| do_something("else-single-line"); | ||
| } | ||
|
|
||
| if constexpr (cond("if1")) | ||
| do_something("if-single-line"); | ||
| else if constexpr (cond("if1")) | ||
| do_something("elseif-single-line"); | ||
| else | ||
| do_something("else-single-line"); | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.