From 35660247dd9ce850dccd60cc55428a510dbdd1c8 Mon Sep 17 00:00:00 2001 From: Nathan James Date: Wed, 18 May 2022 20:47:36 +0100 Subject: [PATCH] [clang-tidy] Fix readability-simplify-boolean-expr when Ifs have an init statement or condition variable Fixes https://github.com/llvm/llvm-project/issues/55553. Reviewed By: LegalizeAdulthood Differential Revision: https://reviews.llvm.org/D125874 --- .../readability/SimplifyBooleanExprCheck.cpp | 25 ++++++++++++++-------- .../readability-simplify-bool-expr-cxx17.cpp | 19 ++++++++++++++++ .../checkers/readability-simplify-bool-expr.cpp | 15 +++++++++++++ 3 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/readability-simplify-bool-expr-cxx17.cpp diff --git a/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp b/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp index 1db4401..323cf1f 100644 --- a/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp @@ -351,6 +351,9 @@ public: } 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 @@ public: * 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 @@ public: : 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 0000000..310afe0 --- /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 acf0cfe..c14438a 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. -- 2.7.4