From 1638831487916e14ebeeb7f1d93a6248a693787b Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Sun, 9 Jun 2024 00:18:14 +0200 Subject: [PATCH] Fix #12732 FP knownConditionTrueFalse with switch (regression) (#6495) --- lib/forwardanalyzer.cpp | 2 +- test/testcondition.cpp | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/forwardanalyzer.cpp b/lib/forwardanalyzer.cpp index d36e38407c5..458d52ad991 100644 --- a/lib/forwardanalyzer.cpp +++ b/lib/forwardanalyzer.cpp @@ -614,7 +614,7 @@ namespace { const Scope* scope = tok->scope(); if (!scope) return Break(); - if (contains({Scope::eDo, Scope::eFor, Scope::eWhile, Scope::eIf, Scope::eElse}, scope->type)) { + if (contains({Scope::eDo, Scope::eFor, Scope::eWhile, Scope::eIf, Scope::eElse, Scope::eSwitch}, scope->type)) { const bool inElse = scope->type == Scope::eElse; const bool inDoWhile = scope->type == Scope::eDo; const bool inLoop = contains({Scope::eDo, Scope::eFor, Scope::eWhile}, scope->type); diff --git a/test/testcondition.cpp b/test/testcondition.cpp index f51d65c759b..8128cd2166b 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -4607,6 +4607,19 @@ class TestCondition : public TestFixture { " if (q) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:4]: (style) Condition 'q' is always true\n", errout_str()); + + check("void f(int i) {\n" + " int j = 0;\n" + " switch (i) {\n" + " case 1:\n" + " j = 0;\n" + " break;\n" + " default:\n" + " j = 1;\n" + " }\n" + " if (j != 0) {}\n" + "}\n"); + ASSERT_EQUALS("", errout_str()); } void alwaysTrueSymbolic()