https://github.com/capitan-davide updated https://github.com/llvm/llvm-project/pull/162361
>From 5f61932af5a90b52b5939406a929734dd6353316 Mon Sep 17 00:00:00 2001 From: Davide Cunial <[email protected]> Date: Fri, 24 Oct 2025 08:18:09 +0200 Subject: [PATCH 1/3] [clang-tidy] Add new check 'bugprone-inconsistent-ifelse-braces' --- .../bugprone/BugproneTidyModule.cpp | 3 + .../clang-tidy/bugprone/CMakeLists.txt | 1 + .../InconsistentIfelseBracesCheck.cpp | 95 +++++++++++++++ .../bugprone/InconsistentIfelseBracesCheck.h | 41 +++++++ clang-tools-extra/docs/ReleaseNotes.rst | 5 + .../bugprone/inconsistent-ifelse-braces.rst | 6 + .../docs/clang-tidy/checks/list.rst | 3 +- ...nconsistent-ifelse-braces-consteval-if.cpp | 28 +++++ .../bugprone/inconsistent-ifelse-braces.cpp | 108 ++++++++++++++++++ 9 files changed, 289 insertions(+), 1 deletion(-) create mode 100644 clang-tools-extra/clang-tidy/bugprone/InconsistentIfelseBracesCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/bugprone/InconsistentIfelseBracesCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/bugprone/inconsistent-ifelse-braces.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/bugprone/inconsistent-ifelse-braces-consteval-if.cpp create mode 100644 clang-tools-extra/test/clang-tidy/checkers/bugprone/inconsistent-ifelse-braces.cpp diff --git a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp index fe261e729539c..e1e42d22c520e 100644 --- a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp @@ -33,6 +33,7 @@ #include "ImplicitWideningOfMultiplicationResultCheck.h" #include "InaccurateEraseCheck.h" #include "IncDecInConditionsCheck.h" +#include "InconsistentIfelseBracesCheck.h" #include "IncorrectEnableIfCheck.h" #include "IncorrectEnableSharedFromThisCheck.h" #include "IncorrectRoundingsCheck.h" @@ -150,6 +151,8 @@ class BugproneModule : public ClangTidyModule { "bugprone-implicit-widening-of-multiplication-result"); CheckFactories.registerCheck<InaccurateEraseCheck>( "bugprone-inaccurate-erase"); + CheckFactories.registerCheck<InconsistentIfelseBracesCheck>( + "bugprone-inconsistent-ifelse-braces"); CheckFactories.registerCheck<IncorrectEnableIfCheck>( "bugprone-incorrect-enable-if"); CheckFactories.registerCheck<IncorrectEnableSharedFromThisCheck>( diff --git a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt index 46bc8efd44bc5..d19fd5017d2e0 100644 --- a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt @@ -28,6 +28,7 @@ add_clang_library(clangTidyBugproneModule STATIC ForwardingReferenceOverloadCheck.cpp ImplicitWideningOfMultiplicationResultCheck.cpp InaccurateEraseCheck.cpp + InconsistentIfelseBracesCheck.cpp IncorrectEnableIfCheck.cpp IncorrectEnableSharedFromThisCheck.cpp InvalidEnumDefaultInitializationCheck.cpp diff --git a/clang-tools-extra/clang-tidy/bugprone/InconsistentIfelseBracesCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/InconsistentIfelseBracesCheck.cpp new file mode 100644 index 0000000000000..ab3da9f26b390 --- /dev/null +++ b/clang-tools-extra/clang-tidy/bugprone/InconsistentIfelseBracesCheck.cpp @@ -0,0 +1,95 @@ + +//===----------------------------------------------------------------------===// +// +// 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")), + this); +} + +void InconsistentIfelseBracesCheck::check( + const MatchFinder::MatchResult &Result) { + const auto *MatchedIf = Result.Nodes.getNodeAs<IfStmt>("if_stmt"); + if (!shouldHaveBraces(MatchedIf)) + return; + + // TODO: Test behavior with macros. This might be the reason why + // readability-braces-around-statements defines a findRParenLoc() function + // rather than using IfStmt/WhileStmt::getRParenLoc(). + checkIfStmt(Result, MatchedIf); +} + +void InconsistentIfelseBracesCheck::checkIfStmt( + const ast_matchers::MatchFinder::MatchResult &Result, const IfStmt *If) { + 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)) + return checkIfStmt(Result, NestedIf); + } + + 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)) + return checkIfStmt(Result, NestedIf); + + if (!isa<CompoundStmt>(Else)) + checkStmt(Result, If->getElse(), If->getElseLoc()); + } +} + +void InconsistentIfelseBracesCheck::checkStmt( + 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(); + } + } +} +} // namespace clang::tidy::bugprone diff --git a/clang-tools-extra/clang-tidy/bugprone/InconsistentIfelseBracesCheck.h b/clang-tools-extra/clang-tidy/bugprone/InconsistentIfelseBracesCheck.h new file mode 100644 index 0000000000000..b841579223be5 --- /dev/null +++ b/clang-tools-extra/clang-tidy/bugprone/InconsistentIfelseBracesCheck.h @@ -0,0 +1,41 @@ + +//===----------------------------------------------------------------------===// +// +// 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 { + +/// FIXME: Write a short description. +/// +/// For the user-facing documentation see: +/// http://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; + bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { + return LangOpts.CPlusPlus; + } + +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 = SourceLocation()); +}; + +} // namespace clang::tidy::bugprone + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_INCONSISTENTIFELSEBRACESCHECK_H diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 7cdff86beeec6..6ed9b93e8c226 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -157,6 +157,11 @@ Improvements to clang-tidy New checks ^^^^^^^^^^ +- New :doc:`bugprone-inconsistent-ifelse-braces + <clang-tidy/checks/bugprone/inconsistent-ifelse-braces>` check. + + FIXME: Write a short description. + - New :doc:`bugprone-invalid-enum-default-initialization <clang-tidy/checks/bugprone/invalid-enum-default-initialization>` check. diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/inconsistent-ifelse-braces.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/inconsistent-ifelse-braces.rst new file mode 100644 index 0000000000000..9592f39d6a13d --- /dev/null +++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/inconsistent-ifelse-braces.rst @@ -0,0 +1,6 @@ +.. title:: clang-tidy - bugprone-inconsistent-ifelse-braces + +bugprone-inconsistent-ifelse-braces +=================================== + +FIXME: Describe what patterns does the check detect and why. Give examples. diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst index c490d2ece2e0a..9438030c64828 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -101,6 +101,7 @@ Clang-Tidy Checks :doc:`bugprone-implicit-widening-of-multiplication-result <bugprone/implicit-widening-of-multiplication-result>`, "Yes" :doc:`bugprone-inaccurate-erase <bugprone/inaccurate-erase>`, "Yes" :doc:`bugprone-inc-dec-in-conditions <bugprone/inc-dec-in-conditions>`, + :doc:`bugprone-inconsistent-ifelse-braces <bugprone/inconsistent-ifelse-braces>`, :doc:`bugprone-incorrect-enable-if <bugprone/incorrect-enable-if>`, "Yes" :doc:`bugprone-incorrect-enable-shared-from-this <bugprone/incorrect-enable-shared-from-this>`, "Yes" :doc:`bugprone-incorrect-roundings <bugprone/incorrect-roundings>`, @@ -256,7 +257,7 @@ Clang-Tidy Checks :doc:`llvm-prefer-static-over-anonymous-namespace <llvm/prefer-static-over-anonymous-namespace>`, :doc:`llvm-twine-local <llvm/twine-local>`, "Yes" :doc:`llvm-use-new-mlir-op-builder <llvm/use-new-mlir-op-builder>`, "Yes" - :doc:`llvm-use-ranges <llvm/use-ranges>`, "Yes" + :doc:`llvm-use-ranges <llvm/use-ranges>`, :doc:`llvmlibc-callee-namespace <llvmlibc/callee-namespace>`, :doc:`llvmlibc-implementation-in-namespace <llvmlibc/implementation-in-namespace>`, :doc:`llvmlibc-inline-function-decl <llvmlibc/inline-function-decl>`, "Yes" diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/inconsistent-ifelse-braces-consteval-if.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/inconsistent-ifelse-braces-consteval-if.cpp new file mode 100644 index 0000000000000..c950045dab69f --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/inconsistent-ifelse-braces-consteval-if.cpp @@ -0,0 +1,28 @@ +// 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-consteval-single-line"); + else { + } + // CHECK-MESSAGES: :[[@LINE-4]]:21: warning: <message> [bugprone-inconsistent-ifelse-braces] + // CHECK-FIXES: if (cond("if1")) { + // CHECK-FIXES: } else { + } else { + if (cond("if1.1")) { + } else + do_something("if-consteval-single-line"); + // CHECK-MESSAGES: :[[@LINE-2]]:11: warning: <message> [bugprone-inconsistent-ifelse-braces] + // CHECK-FIXES: } else { + // CHECK-FIXES: } + } +} + +// Negative tests. +void g() { +} diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/inconsistent-ifelse-braces.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/inconsistent-ifelse-braces.cpp new file mode 100644 index 0000000000000..de5c79f3f8961 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/inconsistent-ifelse-braces.cpp @@ -0,0 +1,108 @@ +// RUN: %check_clang_tidy %s bugprone-inconsistent-ifelse-braces %t + +bool cond(const char *) { return false; } +void do_something(const char *) {} + +// Positive tests. +void f() { + if (cond("if0") /*comment*/) do_something("if-same-line"); + else { + } + // CHECK-MESSAGES: :[[@LINE-3]]:31: warning: <message> [bugprone-inconsistent-ifelse-braces] + // CHECK-FIXES: if (cond("if0") /*comment*/) { do_something("if-same-line"); + // CHECK-FIXES: } else { + + if (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 (cond("if1")) + do_something("if-single-line"); + else { + } + // CHECK-MESSAGES: :[[@LINE-4]]:19: warning: <message> [bugprone-inconsistent-ifelse-braces] + // CHECK-FIXES: if (cond("if1")) { + // CHECK-FIXES: } else { + + if (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 (cond("if2") /*comment*/) + // some comment + do_something("if-multi-line"); + else { + } + // CHECK-MESSAGES: :[[@LINE-5]]:31: warning: <message> [bugprone-inconsistent-ifelse-braces] + // CHECK-FIXES: if (cond("if2") /*comment*/) { + // CHECK-FIXES: } else { + + if (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 (cond("if3")) do_something("elseif-same-line"); + else if (cond("if3")) { + } else { + } + // CHECK-MESSAGES: :[[@LINE-4]]:19: warning: <message> [bugprone-inconsistent-ifelse-braces] + // CHECK-FIXES: if (cond("if3")) { do_something("elseif-same-line"); + // CHECK-FIXES: } else if (cond("if3")) { + + if (cond("if3.1")) { + } else if (cond("if3.1")) do_something("elseif-same-line"); + else { + } + // CHECK-MESSAGES: :[[@LINE-3]]:28: warning: <message> [bugprone-inconsistent-ifelse-braces] + // CHECK-FIXES: } else if (cond("if3.1")) { do_something("elseif-same-line"); + // CHECK-FIXES: } else { + + if (cond("if3.2")) { + } else if (cond("if3.2")) { + } else do_something("elseif-same-line"); + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: <message> [bugprone-inconsistent-ifelse-braces] + // CHECK-FIXES: } else { do_something("elseif-same-line"); + // CHECK-FIXES: } + + if (cond("if4-outer")) + if (cond("if4-inner")) + do_something("nested-if-single-line"); + else { + } + else { + } + // CHECK-MESSAGES: :[[@LINE-7]]:25: warning: <message> [bugprone-inconsistent-ifelse-braces] + // CHECK-MESSAGES: :[[@LINE-7]]:27: warning: <message> [bugprone-inconsistent-ifelse-braces] + // CHECK-FIXES: if (cond("if4-outer")) { + // CHECK-FIXES: if (cond("if4-inner")) { + // CHECK-FIXES: } else { + // CHECK-FIXES: } else { + + if (cond("if5")) + do_something("noelse-single-line"); + else if (cond("if5")) { + } + // CHECK-MESSAGES: :[[@LINE-4]]:19: warning: <message> [bugprone-inconsistent-ifelse-braces] + // CHECK-FIXES: if (cond("if5")) { + // CHECK-FIXES: } else if (cond("if5")) { + + if (cond("if5.1")) { + } else if (cond("if5.1")) + do_something("noelse-single-line"); + // CHECK-MESSAGES: :[[@LINE-2]]:28: warning: <message> [bugprone-inconsistent-ifelse-braces] + // CHECK-FIXES: } else if (cond("if5.1")) { + // CHECK-FIXES: } +} + +// Negative tests. +void g() { +} >From 18e6ae847c45e851c886fb7ddbd820f578ede385 Mon Sep 17 00:00:00 2001 From: Davide Cunial <[email protected]> Date: Sat, 25 Oct 2025 08:02:52 +0200 Subject: [PATCH 2/3] [clang-tidy] Add some tests to 'bugprone-inconsistent-ifelse-braces' --- ...nconsistent-ifelse-braces-consteval-if.cpp | 33 +++++++++++++++++-- .../bugprone/inconsistent-ifelse-braces.cpp | 27 +++++++++++---- 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/inconsistent-ifelse-braces-consteval-if.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/inconsistent-ifelse-braces-consteval-if.cpp index c950045dab69f..3eb51c39e2921 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/inconsistent-ifelse-braces-consteval-if.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/inconsistent-ifelse-braces-consteval-if.cpp @@ -1,22 +1,33 @@ // 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-consteval-single-line"); + 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("if1.1")) { + if (cond("if2.1")) { } else - do_something("if-consteval-single-line"); + do_something("else-single-line"); // CHECK-MESSAGES: :[[@LINE-2]]:11: warning: <message> [bugprone-inconsistent-ifelse-braces] // CHECK-FIXES: } else { // CHECK-FIXES: } @@ -25,4 +36,20 @@ void f() { // 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"); + } } diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/inconsistent-ifelse-braces.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/inconsistent-ifelse-braces.cpp index de5c79f3f8961..42a06f475356c 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/inconsistent-ifelse-braces.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/inconsistent-ifelse-braces.cpp @@ -1,6 +1,7 @@ // RUN: %check_clang_tidy %s bugprone-inconsistent-ifelse-braces %t bool cond(const char *) { return false; } + void do_something(const char *) {} // Positive tests. @@ -68,14 +69,14 @@ void f() { if (cond("if3.2")) { } else if (cond("if3.2")) { - } else do_something("elseif-same-line"); + } else do_something("else-same-line"); // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: <message> [bugprone-inconsistent-ifelse-braces] - // CHECK-FIXES: } else { do_something("elseif-same-line"); + // CHECK-FIXES: } else { do_something("else-same-line"); // CHECK-FIXES: } if (cond("if4-outer")) if (cond("if4-inner")) - do_something("nested-if-single-line"); + do_something("if-single-line"); else { } else { @@ -88,7 +89,7 @@ void f() { // CHECK-FIXES: } else { if (cond("if5")) - do_something("noelse-single-line"); + do_something("if-single-line"); else if (cond("if5")) { } // CHECK-MESSAGES: :[[@LINE-4]]:19: warning: <message> [bugprone-inconsistent-ifelse-braces] @@ -97,7 +98,7 @@ void f() { if (cond("if5.1")) { } else if (cond("if5.1")) - do_something("noelse-single-line"); + do_something("elseif-single-line"); // CHECK-MESSAGES: :[[@LINE-2]]:28: warning: <message> [bugprone-inconsistent-ifelse-braces] // CHECK-FIXES: } else if (cond("if5.1")) { // CHECK-FIXES: } @@ -105,4 +106,18 @@ void f() { // Negative tests. void g() { -} + if (cond("if0")) { + do_something("if-single-line"); + } else if (cond("if0")) { + do_something("elseif-single-line"); + } else { + do_something("else-single-line"); + } + + if (cond("if1")) + do_something("if-single-line"); + else if (cond("if1")) + do_something("elseif-single-line"); + else + do_something("else-single-line"); +} \ No newline at end of file >From e54909da1f054c3c377269045a1008693daacd58 Mon Sep 17 00:00:00 2001 From: Davide Cunial <[email protected]> Date: Sat, 25 Oct 2025 08:40:34 +0200 Subject: [PATCH 3/3] [clang-tidy] Update documentation for 'bugprone-inconsistent-ifelse-braces' --- .../InconsistentIfelseBracesCheck.cpp | 14 +++---- .../bugprone/InconsistentIfelseBracesCheck.h | 4 +- clang-tools-extra/docs/ReleaseNotes.rst | 3 +- .../bugprone/inconsistent-ifelse-braces.rst | 38 ++++++++++++++++++- .../docs/clang-tidy/checks/list.rst | 2 +- 5 files changed, 48 insertions(+), 13 deletions(-) diff --git a/clang-tools-extra/clang-tidy/bugprone/InconsistentIfelseBracesCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/InconsistentIfelseBracesCheck.cpp index ab3da9f26b390..015334d2753c9 100644 --- a/clang-tools-extra/clang-tidy/bugprone/InconsistentIfelseBracesCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/InconsistentIfelseBracesCheck.cpp @@ -1,4 +1,3 @@ - //===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. @@ -62,17 +61,15 @@ void InconsistentIfelseBracesCheck::checkIfStmt( // it, then we need to check the inner IfStmt. checkStmt(Result, If->getThen(), If->getRParenLoc(), If->getElseLoc()); if (shouldHaveBraces(NestedIf)) - return checkIfStmt(Result, NestedIf); - } - - if (!isa<CompoundStmt>(Then)) + 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)) - return checkIfStmt(Result, NestedIf); - - if (!isa<CompoundStmt>(Else)) + checkIfStmt(Result, NestedIf); + else if (!isa<CompoundStmt>(Else)) checkStmt(Result, If->getElse(), If->getElseLoc()); } } @@ -92,4 +89,5 @@ void InconsistentIfelseBracesCheck::checkStmt( } } } + } // namespace clang::tidy::bugprone diff --git a/clang-tools-extra/clang-tidy/bugprone/InconsistentIfelseBracesCheck.h b/clang-tools-extra/clang-tidy/bugprone/InconsistentIfelseBracesCheck.h index b841579223be5..e3ce405282ecd 100644 --- a/clang-tools-extra/clang-tidy/bugprone/InconsistentIfelseBracesCheck.h +++ b/clang-tools-extra/clang-tidy/bugprone/InconsistentIfelseBracesCheck.h @@ -1,4 +1,3 @@ - //===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. @@ -14,7 +13,8 @@ namespace clang::tidy::bugprone { -/// FIXME: Write a short description. +/// Detects ``if``/``else`` statements where one branch uses braces and the +/// other does not. /// /// For the user-facing documentation see: /// http://clang.llvm.org/extra/clang-tidy/checks/bugprone/inconsistent-ifelse-braces.html diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 6ed9b93e8c226..28bebceef1006 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -160,7 +160,8 @@ New checks - New :doc:`bugprone-inconsistent-ifelse-braces <clang-tidy/checks/bugprone/inconsistent-ifelse-braces>` check. - FIXME: Write a short description. + Detects ``if``/``else`` statements where one branch uses braces and the other + does not. - New :doc:`bugprone-invalid-enum-default-initialization <clang-tidy/checks/bugprone/invalid-enum-default-initialization>` check. diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/inconsistent-ifelse-braces.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/inconsistent-ifelse-braces.rst index 9592f39d6a13d..44f2d64452393 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/inconsistent-ifelse-braces.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/inconsistent-ifelse-braces.rst @@ -3,4 +3,40 @@ bugprone-inconsistent-ifelse-braces =================================== -FIXME: Describe what patterns does the check detect and why. Give examples. +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; diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst index 9438030c64828..4c5b70f6c47e5 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -101,7 +101,7 @@ Clang-Tidy Checks :doc:`bugprone-implicit-widening-of-multiplication-result <bugprone/implicit-widening-of-multiplication-result>`, "Yes" :doc:`bugprone-inaccurate-erase <bugprone/inaccurate-erase>`, "Yes" :doc:`bugprone-inc-dec-in-conditions <bugprone/inc-dec-in-conditions>`, - :doc:`bugprone-inconsistent-ifelse-braces <bugprone/inconsistent-ifelse-braces>`, + :doc:`bugprone-inconsistent-ifelse-braces <bugprone/inconsistent-ifelse-braces>`, "Yes" :doc:`bugprone-incorrect-enable-if <bugprone/incorrect-enable-if>`, "Yes" :doc:`bugprone-incorrect-enable-shared-from-this <bugprone/incorrect-enable-shared-from-this>`, "Yes" :doc:`bugprone-incorrect-roundings <bugprone/incorrect-roundings>`, _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
