Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #13728 FP oppositeInnerCondition from lambda #7391

Merged
merged 3 commits into from
Mar 25, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/checkcondition.cpp
Original file line number Diff line number Diff line change
@@ -615,6 +615,16 @@ static bool isNonConstFunctionCall(const Token *ftok, const Library &library)
return true;
}

static bool isNestedInLambda(const Scope* inner, const Scope* outer)
{
while (inner && inner != outer) {
if (inner->type == ScopeType::eLambda)
return true;
inner = inner->nestedIn;
}
return false;
}

void CheckCondition::multiCondition2()
{
if (!mSettings->severity.isEnabled(Severity::warning) &&
@@ -709,6 +719,8 @@ void CheckCondition::multiCondition2()
const Token * const endToken = tok->scope()->bodyEnd;

for (; tok && tok != endToken; tok = tok->next()) {
if (isNestedInLambda(tok->scope(), cond1->scope()))
continue;
if (isExpressionChangedAt(cond1, tok, 0, false, *mSettings))
break;
if (Token::Match(tok, "if|return")) {
17 changes: 17 additions & 0 deletions test/testcondition.cpp
Original file line number Diff line number Diff line change
@@ -88,6 +88,7 @@ class TestCondition : public TestFixture {
TEST_CASE(oppositeInnerConditionOr);
TEST_CASE(oppositeInnerConditionEmpty);
TEST_CASE(oppositeInnerConditionFollowVar);
TEST_CASE(oppositeInnerConditionLambda);

TEST_CASE(identicalInnerCondition);

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

void oppositeInnerConditionLambda() {
check("void f() {\n" // #13728
" for (int i = 0; i < 2;) {\n"
" auto inc = [&]() {\n"
" if (i >= 2)\n"
" throw 0;\n"
" return i++;\n"
" };\n"
" inc();\n"
" inc();\n"
" inc();\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}

void identicalInnerCondition() {
check("void f1(int a, int b) { if(a==b) if(a==b) {}}");
ASSERT_EQUALS("[test.cpp:1] -> [test.cpp:1]: (warning) Identical inner 'if' condition is always true.\n", errout_str());
Loading
Oops, something went wrong.