[clang-tools-extra] 7c0420d - [clang-tidy] Add new check 'readability-inconsistent-ifelse-braces' (#162361)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Dec 30 08:26:21 PST 2025
Author: Davide Cunial
Date: 2025-12-30T19:26:17+03:00
New Revision: 7c0420dc8484a0cbad32c085b94ab250d89b0ce4
URL: https://github.com/llvm/llvm-project/commit/7c0420dc8484a0cbad32c085b94ab250d89b0ce4
DIFF: https://github.com/llvm/llvm-project/commit/7c0420dc8484a0cbad32c085b94ab250d89b0ce4.diff
LOG: [clang-tidy] Add new check 'readability-inconsistent-ifelse-braces' (#162361)
Closes https://github.com/llvm/llvm-project/issues/162140
---------
Co-authored-by: Victor Chernyakin <chernyakin.victor.j at outlook.com>
Added:
clang-tools-extra/clang-tidy/readability/InconsistentIfElseBracesCheck.cpp
clang-tools-extra/clang-tidy/readability/InconsistentIfElseBracesCheck.h
clang-tools-extra/docs/clang-tidy/checks/readability/inconsistent-ifelse-braces.rst
clang-tools-extra/test/clang-tidy/checkers/readability/inconsistent-ifelse-braces-attributes.cpp
clang-tools-extra/test/clang-tidy/checkers/readability/inconsistent-ifelse-braces-consteval-if.cpp
clang-tools-extra/test/clang-tidy/checkers/readability/inconsistent-ifelse-braces-constexpr-if.cpp
clang-tools-extra/test/clang-tidy/checkers/readability/inconsistent-ifelse-braces.cpp
Modified:
clang-tools-extra/clang-tidy/readability/CMakeLists.txt
clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/docs/clang-tidy/checks/list.rst
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
index 161a0d96caf41..a55f0ab78f97f 100644
--- a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt
@@ -24,6 +24,7 @@ add_clang_library(clangTidyReadabilityModule STATIC
IdentifierLengthCheck.cpp
IdentifierNamingCheck.cpp
ImplicitBoolConversionCheck.cpp
+ InconsistentIfElseBracesCheck.cpp
RedundantInlineSpecifierCheck.cpp
InconsistentDeclarationParameterNameCheck.cpp
IsolateDeclarationCheck.cpp
diff --git a/clang-tools-extra/clang-tidy/readability/InconsistentIfElseBracesCheck.cpp b/clang-tools-extra/clang-tidy/readability/InconsistentIfElseBracesCheck.cpp
new file mode 100644
index 0000000000000..6cc1b203470f1
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/readability/InconsistentIfElseBracesCheck.cpp
@@ -0,0 +1,91 @@
+//===----------------------------------------------------------------------===//
+//
+// 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/AST/Stmt.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/Basic/SourceLocation.h"
+#include "clang/Lex/Lexer.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+
+/// 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(
+ 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;
+ checkIfStmt(Result, MatchedIf);
+}
+
+void InconsistentIfElseBracesCheck::checkIfStmt(
+ const 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.
+ emitDiagnostic(Result, If->getThen(), If->getRParenLoc(), If->getElseLoc());
+
+ if (shouldHaveBraces(NestedIf))
+ checkIfStmt(Result, NestedIf);
+ } else if (!isa<CompoundStmt>(Then)) {
+ emitDiagnostic(Result, Then, 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))
+ emitDiagnostic(Result, If->getElse(), If->getElseLoc());
+ }
+}
+
+void InconsistentIfElseBracesCheck::emitDiagnostic(
+ const MatchFinder::MatchResult &Result, const Stmt *S,
+ SourceLocation StartLoc, SourceLocation EndLocHint) {
+ if (StartLoc.isMacroID()) {
+ diag(StartLoc, "statement should have braces");
+ return;
+ }
+ const utils::BraceInsertionHints Hints = utils::getBraceInsertionsHints(
+ S, Result.Context->getLangOpts(), *Result.SourceManager, StartLoc,
+ EndLocHint);
+ assert(Hints && Hints.offersFixIts() && "Expected hints or fix-its");
+ diag(Hints.DiagnosticPos, "statement should have braces")
+ << Hints.openingBraceFixIt() << Hints.closingBraceFixIt();
+}
+
+} // namespace clang::tidy::readability
diff --git a/clang-tools-extra/clang-tidy/readability/InconsistentIfElseBracesCheck.h b/clang-tools-extra/clang-tidy/readability/InconsistentIfElseBracesCheck.h
new file mode 100644
index 0000000000000..d45b4743cdf39
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/readability/InconsistentIfElseBracesCheck.h
@@ -0,0 +1,43 @@
+//===----------------------------------------------------------------------===//
+//
+// 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_READABILITY_INCONSISTENTIFELSEBRACESCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_INCONSISTENTIFELSEBRACESCHECK_H
+
+#include "../ClangTidyCheck.h"
+#include "clang/AST/ASTTypeTraits.h"
+#include <optional>
+
+namespace clang::tidy::readability {
+
+/// 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/readability/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;
+ std::optional<TraversalKind> getCheckTraversalKind() const override {
+ return TK_IgnoreUnlessSpelledInSource;
+ }
+
+private:
+ void checkIfStmt(const ast_matchers::MatchFinder::MatchResult &Result,
+ const IfStmt *If);
+ void emitDiagnostic(const ast_matchers::MatchFinder::MatchResult &Result,
+ const Stmt *S, SourceLocation StartLoc,
+ SourceLocation EndLocHint = {});
+};
+
+} // namespace clang::tidy::readability
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_INCONSISTENTIFELSEBRACESCHECK_H
diff --git a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp
index 6126de747782f..d2da125a9d6da 100644
--- a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp
@@ -29,6 +29,7 @@
#include "IdentifierNamingCheck.h"
#include "ImplicitBoolConversionCheck.h"
#include "InconsistentDeclarationParameterNameCheck.h"
+#include "InconsistentIfElseBracesCheck.h"
#include "IsolateDeclarationCheck.h"
#include "MagicNumbersCheck.h"
#include "MakeMemberFunctionConstCheck.h"
@@ -112,6 +113,8 @@ class ReadabilityModule : public ClangTidyModule {
"readability-identifier-naming");
CheckFactories.registerCheck<ImplicitBoolConversionCheck>(
"readability-implicit-bool-conversion");
+ CheckFactories.registerCheck<InconsistentIfElseBracesCheck>(
+ "readability-inconsistent-ifelse-braces");
CheckFactories.registerCheck<MathMissingParenthesesCheck>(
"readability-math-missing-parentheses");
CheckFactories.registerCheck<RedundantInlineSpecifierCheck>(
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 7d878f7d28386..56e2b56249fa4 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -265,6 +265,12 @@ New checks
Finds virtual function overrides with
diff erent visibility than the function
in the base class.
+- New :doc:`readability-inconsistent-ifelse-braces
+ <clang-tidy/checks/readability/inconsistent-ifelse-braces>` check.
+
+ Detects ``if``/``else`` statements where one branch uses braces and the other
+ does not.
+
- New :doc:`readability-redundant-parentheses
<clang-tidy/checks/readability/redundant-parentheses>` check.
diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst
index e5e77b5cc418b..e150c5ff50fda 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/list.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst
@@ -392,6 +392,7 @@ Clang-Tidy Checks
:doc:`readability-identifier-naming <readability/identifier-naming>`, "Yes"
:doc:`readability-implicit-bool-conversion <readability/implicit-bool-conversion>`, "Yes"
:doc:`readability-inconsistent-declaration-parameter-name <readability/inconsistent-declaration-parameter-name>`, "Yes"
+ :doc:`readability-inconsistent-ifelse-braces <readability/inconsistent-ifelse-braces>`, "Yes"
:doc:`readability-isolate-declaration <readability/isolate-declaration>`, "Yes"
:doc:`readability-magic-numbers <readability/magic-numbers>`,
:doc:`readability-make-member-function-const <readability/make-member-function-const>`, "Yes"
diff --git a/clang-tools-extra/docs/clang-tidy/checks/readability/inconsistent-ifelse-braces.rst b/clang-tools-extra/docs/clang-tidy/checks/readability/inconsistent-ifelse-braces.rst
new file mode 100644
index 0000000000000..53a79808b2147
--- /dev/null
+++ b/clang-tools-extra/docs/clang-tidy/checks/readability/inconsistent-ifelse-braces.rst
@@ -0,0 +1,42 @@
+.. title:: clang-tidy - readability-inconsistent-ifelse-braces
+
+readability-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;
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/inconsistent-ifelse-braces-attributes.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/inconsistent-ifelse-braces-attributes.cpp
new file mode 100644
index 0000000000000..8fdb574227028
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/inconsistent-ifelse-braces-attributes.cpp
@@ -0,0 +1,42 @@
+// RUN: %check_clang_tidy -std=c++20-or-later %s readability-inconsistent-ifelse-braces %t
+
+// Positive tests.
+void f(bool b) {
+ if (b) [[likely]] return;
+ else {
+ }
+ // CHECK-MESSAGES: :[[@LINE-3]]:9: warning: statement should have braces [readability-inconsistent-ifelse-braces]
+ // CHECK-FIXES: if (b) { {{[[][[]}}likely{{[]][]]}} return;
+ // CHECK-FIXES: } else {
+
+ if (b) {
+ } else [[unlikely]]
+ return;
+ // CHECK-MESSAGES: :[[@LINE-2]]:9: warning: statement should have braces [readability-inconsistent-ifelse-braces]
+ // CHECK-FIXES: } else { {{[[][[]}}unlikely{{[]][]]}}
+}
+
+// Negative tests.
+void g(bool b) {
+ if (b) {
+ return;
+ }
+
+ if (b) { [[likely]]
+ return;
+ }
+
+ if (b) { [[unlikely]]
+ return;
+ } else { [[likely]]
+ return;
+ }
+
+ if (b) [[likely]]
+ return;
+
+ if (b) [[unlikely]]
+ return;
+ else [[likely]]
+ return;
+}
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/inconsistent-ifelse-braces-consteval-if.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/inconsistent-ifelse-braces-consteval-if.cpp
new file mode 100644
index 0000000000000..2cfbb1bf363b1
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/inconsistent-ifelse-braces-consteval-if.cpp
@@ -0,0 +1,54 @@
+// RUN: %check_clang_tidy -std=c++23-or-later %s readability-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: statement should have braces [readability-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: statement should have braces [readability-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: statement should have braces [readability-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");
+ }
+}
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/inconsistent-ifelse-braces-constexpr-if.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/inconsistent-ifelse-braces-constexpr-if.cpp
new file mode 100644
index 0000000000000..3ad3afd8b8d61
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/inconsistent-ifelse-braces-constexpr-if.cpp
@@ -0,0 +1,122 @@
+// RUN: %check_clang_tidy -std=c++17-or-later %s readability-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: statement should have braces [readability-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: statement should have braces [readability-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: statement should have braces [readability-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: statement should have braces [readability-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: statement should have braces [readability-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: statement should have braces [readability-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: statement should have braces [readability-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: statement should have braces [readability-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: statement should have braces [readability-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: statement should have braces [readability-inconsistent-ifelse-braces]
+ // CHECK-MESSAGES: :[[@LINE-7]]:37: warning: statement should have braces [readability-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: statement should have braces [readability-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: statement should have braces [readability-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");
+}
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/inconsistent-ifelse-braces.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/inconsistent-ifelse-braces.cpp
new file mode 100644
index 0000000000000..3874719512f91
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/inconsistent-ifelse-braces.cpp
@@ -0,0 +1,137 @@
+// RUN: %check_clang_tidy -std=c++98-or-later %s readability-inconsistent-ifelse-braces %t
+
+#define MACRO_COND(x) cond(x)
+#define MACRO_FUN (void)0
+
+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: statement should have braces [readability-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: statement should have braces [readability-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: statement should have braces [readability-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: statement should have braces [readability-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: statement should have braces [readability-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: statement should have braces [readability-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: statement should have braces [readability-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: statement should have braces [readability-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("else-same-line");
+ // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: statement should have braces [readability-inconsistent-ifelse-braces]
+ // CHECK-FIXES: } else { do_something("else-same-line");
+ // CHECK-FIXES: }
+
+ if (cond("if4-outer"))
+ if (cond("if4-inner"))
+ do_something("if-single-line");
+ else {
+ }
+ else {
+ }
+ // CHECK-MESSAGES: :[[@LINE-7]]:25: warning: statement should have braces [readability-inconsistent-ifelse-braces]
+ // CHECK-MESSAGES: :[[@LINE-7]]:27: warning: statement should have braces [readability-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("if-single-line");
+ else if (cond("if5")) {
+ }
+ // CHECK-MESSAGES: :[[@LINE-4]]:19: warning: statement should have braces [readability-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("elseif-single-line");
+ // CHECK-MESSAGES: :[[@LINE-2]]:28: warning: statement should have braces [readability-inconsistent-ifelse-braces]
+ // CHECK-FIXES: } else if (cond("if5.1")) {
+ // CHECK-FIXES: }
+
+ if (MACRO_COND("if6")) MACRO_FUN;
+ else {
+ }
+ // CHECK-MESSAGES: :[[@LINE-3]]:25: warning: statement should have braces [readability-inconsistent-ifelse-braces]
+ // CHECK-FIXES: if (MACRO_COND("if6")) { MACRO_FUN;
+ // CHECK-FIXES: } else {
+
+ if (MACRO_COND("if6")) {
+ } else MACRO_FUN;
+ // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: statement should have braces [readability-inconsistent-ifelse-braces]
+ // CHECK-FIXES: } else { MACRO_FUN;
+}
+
+// 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");
+}
More information about the cfe-commits
mailing list