From 16c06e5714db8c509c4564e424f26d307802adf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 28 Feb 2017 18:46:28 +0100 Subject: [PATCH] Fixed #7588 (Opposite conditions in nested 'if' blocks lead to a dead code block.) --- lib/checkcondition.cpp | 8 ++++++-- test/testcondition.cpp | 10 ++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/checkcondition.cpp b/lib/checkcondition.cpp index 1a4f7de49..7409252c6 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -471,8 +471,12 @@ void CheckCondition::oppositeInnerCondition() } if (Token::Match(tok, "%type% (") && nonlocal) // function call -> bailout if there are nonlocal variables break; - else if ((tok->varId() && vars.find(tok->varId()) != vars.end()) || - (!tok->varId() && nonlocal)) { + // bailout if loop is seen. + // TODO: handle loops. + if (Token::Match(tok, "for|while|do")) + break; + if ((tok->varId() && vars.find(tok->varId()) != vars.end()) || + (!tok->varId() && nonlocal)) { if (Token::Match(tok, "%name% %assign%|++|--")) break; if (Token::Match(tok, "%name% [")) { diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 225eb37d7..8be72eaa3 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -1505,6 +1505,16 @@ private: " }\n" "}"); TODO_ASSERT_EQUALS("error", "", errout.str()); + + check("void foo(unsigned u) {\n" + " if (u != 0) {\n" + " for (int i=0; i<32; i++) {\n" + " if (u == 0) {}\n" // <- don't warn + " u = x;\n" + " }\n" + " }\n" + "}"); + ASSERT_EQUALS("", errout.str()); } // clarify conditions with = and comparison