diff --git a/clang-tools-extra/clang-tidy/readability/CMakeLists.txt b/clang-tools-extra/clang-tidy/readability/CMakeLists.txt index 4b4c49d3b17d1..c2191ebd46a05 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..07eceb79e4c95 --- /dev/null +++ b/clang-tools-extra/clang-tidy/readability/InconsistentIfElseBracesCheck.cpp @@ -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::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(Then)) + return true; + + if (const Stmt *const Else = If->getElse()) { + if (const auto *NestedIf = dyn_cast(Else)) + return shouldHaveBraces(NestedIf); + + return isa(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("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(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(Then)) { + emitDiagnostic(Result, If->getThen(), If->getRParenLoc(), If->getElseLoc()); + } + + if (const Stmt *const Else = If->getElse()) { + if (const auto *NestedIf = dyn_cast(Else)) + checkIfStmt(Result, NestedIf); + else if (!isa(Else)) + emitDiagnostic(Result, If->getElse(), If->getElseLoc()); + } +} + +void InconsistentIfElseBracesCheck::emitDiagnostic( + const MatchFinder::MatchResult &Result, const Stmt *S, + SourceLocation StartLoc, SourceLocation EndLocHint) { + const SourceManager &SM = *Result.SourceManager; + const LangOptions &LangOpts = Result.Context->getLangOpts(); + + if (!StartLoc.isMacroID()) { + const utils::BraceInsertionHints Hints = + utils::getBraceInsertionsHints(S, LangOpts, SM, StartLoc, EndLocHint); + assert(Hints && Hints.offersFixIts() && "Expected hints or fix-its"); + diag(Hints.DiagnosticPos, "") + << Hints.openingBraceFixIt() << Hints.closingBraceFixIt(); + } else { + diag(StartLoc, "") << StartLoc.isMacroID(); + } +} + +} // 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 + +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 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 12f8cdb289dd2..dd34924011247 100644 --- a/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp @@ -30,6 +30,7 @@ #include "IdentifierNamingCheck.h" #include "ImplicitBoolConversionCheck.h" #include "InconsistentDeclarationParameterNameCheck.h" +#include "InconsistentIfElseBracesCheck.h" #include "IsolateDeclarationCheck.h" #include "MagicNumbersCheck.h" #include "MakeMemberFunctionConstCheck.h" @@ -110,6 +111,8 @@ class ReadabilityModule : public ClangTidyModule { "readability-identifier-naming"); CheckFactories.registerCheck( "readability-implicit-bool-conversion"); + CheckFactories.registerCheck( + "readability-inconsistent-ifelse-braces"); CheckFactories.registerCheck( "readability-math-missing-parentheses"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 7cdff86beeec6..4ebb0838426ff 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -157,6 +157,12 @@ Improvements to clang-tidy New checks ^^^^^^^^^^ +- New :doc:`readability-inconsistent-ifelse-braces + ` check. + + Detects ``if``/``else`` statements where one branch uses braces and the other + does not. + - New :doc:`bugprone-invalid-enum-default-initialization ` check. diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst index c490d2ece2e0a..434939bc9cdaf 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -386,6 +386,7 @@ Clang-Tidy Checks :doc:`readability-identifier-naming `, "Yes" :doc:`readability-implicit-bool-conversion `, "Yes" :doc:`readability-inconsistent-declaration-parameter-name `, "Yes" + :doc:`readability-inconsistent-ifelse-braces `, "Yes" :doc:`readability-isolate-declaration `, "Yes" :doc:`readability-magic-numbers `, :doc:`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..4b236f8684b3b --- /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..9facf21bfb351 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/inconsistent-ifelse-braces-attributes.cpp @@ -0,0 +1,32 @@ +// RUN: %check_clang_tidy -std=c++20-or-later %s readability-braces-around-statements %t + +void do_something(const char *) {} + +// Positive tests. +void f(bool b) { + if (b) [[likely]] do_something("if-same-line"); + else { + } + // CHECK-MESSAGES: :[[@LINE-3]]:20: warning: [readability-inconsistent-ifelse-braces] +} + +// Negative tests. +void g(bool b) { + if (b) { + return; + } + + if (b) [[likely]] { + return; + } + + if (b) [[unlikely]] { + return; + } + + if (b) [[likely]] + return; + + if (b) [[unlikely]] + 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..47dcc1fc12992 --- /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: [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: [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: [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..9601bd2a9a6b7 --- /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: [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: [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: [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: [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: [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: [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: [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: [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: [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: [readability-inconsistent-ifelse-braces] + // CHECK-MESSAGES: :[[@LINE-7]]:37: warning: [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: [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: [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..e6c27ffec629f --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/inconsistent-ifelse-braces.cpp @@ -0,0 +1,122 @@ +// RUN: %check_clang_tidy -std=c++98-or-later %s readability-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: [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: [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: [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: [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: [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: [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: [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: [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: [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: [readability-inconsistent-ifelse-braces] + // CHECK-MESSAGES: :[[@LINE-7]]:27: warning: [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: [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: [readability-inconsistent-ifelse-braces] + // CHECK-FIXES: } else if (cond("if5.1")) { + // CHECK-FIXES: } +} + +// 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"); +}