From 70b4c945de47fbb6f6d360cd9d15dcb4f473110d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 30 May 2014 19:19:24 +0200 Subject: [PATCH] Fixed #5874 (False positive: 'opposite conditions in nested if' with arrays) --- lib/checkother.cpp | 7 +++++++ test/testother.cpp | 10 ++++++++++ 2 files changed, 17 insertions(+) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 9e189d3d8..a6c9e4ac4 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -3345,6 +3345,13 @@ void CheckOther::oppositeInnerCondition() (!tok->varId() && nonlocal)) { if (Token::Match(tok, "%var% ++|--|=")) break; + if (Token::Match(tok,"%var% [")) { + const Token *tok2 = tok->linkAt(1); + while (Token::simpleMatch(tok2, "] [")) + tok2 = tok2->linkAt(1); + if (Token::simpleMatch(tok2, "] =")) + break; + } if (Token::Match(tok->previous(), "++|--|& %var%")) break; if (Token::Match(tok->previous(), "[(,] %var% [,)]")) { diff --git a/test/testother.cpp b/test/testother.cpp index 56d24ec16..c4a65b96f 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -448,6 +448,16 @@ private: " }\n" "}"); ASSERT_EQUALS("", errout.str()); + + // #5874 - array + check("void testOppositeConditions2() {\n" + " int array[2] = { 0, 0 };\n" + " if (array[0] < 2) {\n" + " array[0] += 5;\n" + " if (array[0] > 2) {}\n" + " }\n" + "}"); + ASSERT_EQUALS("", errout.str()); } void emptyBrackets() {