diff --git a/clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp index 5a705376cd4b0..00793009ed390 100644 --- a/clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/InfiniteLoopCheck.cpp @@ -152,6 +152,13 @@ static std::string getCondVarNames(const Stmt *Cond) { return Result; } +static bool isKnownFalse(const Expr &Cond, const ASTContext &Ctx) { + bool Result = false; + if (Cond.EvaluateAsBooleanCondition(Result, Ctx)) + return !Result; + return false; +} + void InfiniteLoopCheck::registerMatchers(MatchFinder *Finder) { const auto LoopCondition = allOf( hasCondition( @@ -170,6 +177,9 @@ void InfiniteLoopCheck::check(const MatchFinder::MatchResult &Result) { const auto *LoopStmt = Result.Nodes.getNodeAs("loop-stmt"); const auto *Func = Result.Nodes.getNodeAs("func"); + if (isKnownFalse(*Cond, *Result.Context)) + return; + bool ShouldHaveConditionVariables = true; if (const auto *While = dyn_cast(LoopStmt)) { if (const VarDecl *LoopVarDecl = While->getConditionVariable()) { diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone-infinite-loop.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone-infinite-loop.cpp index d89bdadf02125..427b5f0272b94 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone-infinite-loop.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone-infinite-loop.cpp @@ -354,3 +354,12 @@ void lambda_capture() { (*p)++; } while (i < Limit); } + +void evaluatable(bool CondVar) { + for (; false && CondVar;) { + } + while (false && CondVar) { + } + do { + } while (false && CondVar); +}