diff --git a/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp b/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp index 1db440120aa3a2..323cf1f2d5ecbd 100644 --- a/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp @@ -351,6 +351,9 @@ class SimplifyBooleanExprCheck::Visitor : public RecursiveASTVisitor { } bool VisitIfStmt(IfStmt *If) { + // Skip any if's that have a condition var or an init statement. + if (If->hasInitStorage() || If->hasVarStorage()) + return true; /* * if (true) ThenStmt(); -> ThenStmt(); * if (false) ThenStmt(); -> ; @@ -461,14 +464,17 @@ class SimplifyBooleanExprCheck::Visitor : public RecursiveASTVisitor { * if (Cond) return false; return true; -> return !Cond; */ auto *If = cast(*First); - ExprAndBool ThenReturnBool = - checkSingleStatement(If->getThen(), parseReturnLiteralBool); - if (ThenReturnBool && ThenReturnBool.Bool != TrailingReturnBool.Bool) { - if (Check->ChainedConditionalReturn || - (!PrevIf && If->getElse() == nullptr)) { - Check->replaceCompoundReturnWithCondition( - Context, cast(*Second), TrailingReturnBool.Bool, If, - ThenReturnBool.Item); + if (!If->hasInitStorage() && !If->hasVarStorage()) { + ExprAndBool ThenReturnBool = + checkSingleStatement(If->getThen(), parseReturnLiteralBool); + if (ThenReturnBool && + ThenReturnBool.Bool != TrailingReturnBool.Bool) { + if (Check->ChainedConditionalReturn || + (!PrevIf && If->getElse() == nullptr)) { + Check->replaceCompoundReturnWithCondition( + Context, cast(*Second), TrailingReturnBool.Bool, + If, ThenReturnBool.Item); + } } } } else if (isa(*First)) { @@ -481,7 +487,8 @@ class SimplifyBooleanExprCheck::Visitor : public RecursiveASTVisitor { : isa(*First) ? cast(*First)->getSubStmt() : cast(*First)->getSubStmt(); auto *SubIf = dyn_cast(SubStmt); - if (SubIf && !SubIf->getElse()) { + if (SubIf && !SubIf->getElse() && !SubIf->hasInitStorage() && + !SubIf->hasVarStorage()) { ExprAndBool ThenReturnBool = checkSingleStatement(SubIf->getThen(), parseReturnLiteralBool); if (ThenReturnBool && diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability-simplify-bool-expr-cxx17.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability-simplify-bool-expr-cxx17.cpp new file mode 100644 index 00000000000000..310afe04672aeb --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/readability-simplify-bool-expr-cxx17.cpp @@ -0,0 +1,19 @@ +// RUN: clang-tidy %s -checks='-*,readability-simplify-boolean-expr' -- -std=c++17 | count 0 +struct RAII {}; +bool foo(bool Cond) { + bool Result; + + if (RAII Object; Cond) + Result = true; + else + Result = false; + + if (bool X = Cond; X) + Result = true; + else + Result = false; + + if (bool X = Cond; X) + return true; + return false; +} diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability-simplify-bool-expr.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability-simplify-bool-expr.cpp index acf0cfe301279d..c14438aa938019 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability-simplify-bool-expr.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability-simplify-bool-expr.cpp @@ -478,6 +478,14 @@ void lambda_conditional_return_statements() { // CHECK-FIXES-NEXT: {{^}} };{{$}} } +bool condition_variable_return_stmt(int i) { + // Unchanged: condition variable. + if (bool Res = i == 0) + return true; + else + return false; +} + void simple_conditional_assignment_statements(int i) { bool b; if (i > 10) @@ -594,6 +602,13 @@ void complex_conditional_assignment_statements(int i) { h = true; } else h = false; + + // Unchanged: condition variable. + bool k; + if (bool Res = j > 10) + k = true; + else + k = false; } // Unchanged: chained return statements, but ChainedConditionalReturn not set.