================ @@ -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")), ---------------- capitan-davide wrote:
There is a test for this [here](https://github.com/llvm/llvm-project/pull/162361/files/40570f986a7bec192db25c1001294ca8f93d4c75#diff-44527a85c21f5dc54a69873c1f9f83cdae752651ea62ae152809059d935110a1R77-R83). Basically, the matcher will match the outer `IfStmt` and if the then-branch is another `IfStmt` is handled recursively by [`InconsistentIfelseBracesCheck::checkIfStmt`](https://github.com/llvm/llvm-project/pull/162361/files#diff-1c1e936a6e93dcab49626522a8166d7bfee991f7410e755cecbb60243aae9fa9R60) https://github.com/llvm/llvm-project/pull/162361 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
