Skip to content

Commit 5ac33c8

Browse files
Fix #13728 FP oppositeInnerCondition from lambda (#7391)
1 parent 379842b commit 5ac33c8

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

lib/checkcondition.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,16 @@ static bool isNonConstFunctionCall(const Token *ftok, const Library &library)
615615
return true;
616616
}
617617

618+
static bool isNestedInLambda(const Scope* inner, const Scope* outer)
619+
{
620+
while (inner && inner != outer) {
621+
if (inner->type == ScopeType::eLambda)
622+
return true;
623+
inner = inner->nestedIn;
624+
}
625+
return false;
626+
}
627+
618628
void CheckCondition::multiCondition2()
619629
{
620630
if (!mSettings->severity.isEnabled(Severity::warning) &&
@@ -709,6 +719,8 @@ void CheckCondition::multiCondition2()
709719
const Token * const endToken = tok->scope()->bodyEnd;
710720

711721
for (; tok && tok != endToken; tok = tok->next()) {
722+
if (isNestedInLambda(tok->scope(), cond1->scope()))
723+
continue;
712724
if (isExpressionChangedAt(cond1, tok, 0, false, *mSettings))
713725
break;
714726
if (Token::Match(tok, "if|return")) {

test/testcondition.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class TestCondition : public TestFixture {
8888
TEST_CASE(oppositeInnerConditionOr);
8989
TEST_CASE(oppositeInnerConditionEmpty);
9090
TEST_CASE(oppositeInnerConditionFollowVar);
91+
TEST_CASE(oppositeInnerConditionLambda);
9192

9293
TEST_CASE(identicalInnerCondition);
9394

@@ -2734,6 +2735,22 @@ class TestCondition : public TestFixture {
27342735
ASSERT_EQUALS("", errout_str());
27352736
}
27362737

2738+
void oppositeInnerConditionLambda() {
2739+
check("void f() {\n" // #13728
2740+
" for (int i = 0; i < 2;) {\n"
2741+
" auto inc = [&]() {\n"
2742+
" if (i >= 2)\n"
2743+
" throw 0;\n"
2744+
" return i++;\n"
2745+
" };\n"
2746+
" inc();\n"
2747+
" inc();\n"
2748+
" inc();\n"
2749+
" }\n"
2750+
"}\n");
2751+
ASSERT_EQUALS("", errout_str());
2752+
}
2753+
27372754
void identicalInnerCondition() {
27382755
check("void f1(int a, int b) { if(a==b) if(a==b) {}}");
27392756
ASSERT_EQUALS("[test.cpp:1] -> [test.cpp:1]: (warning) Identical inner 'if' condition is always true.\n", errout_str());

0 commit comments

Comments
 (0)