From e7483af0283464ef0fe3899c818cbf4327ba569b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 21 Oct 2012 18:18:29 +0200 Subject: [PATCH] Opposite inner conditions - made check 'experimental' because there are unsolved false positives. --- lib/checkother.cpp | 18 +++++++++--------- lib/checkother.h | 8 ++++---- test/testother.cpp | 10 +++++----- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 501e5b309..78e787d40 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -3495,9 +3495,10 @@ void CheckOther::incompleteArrayFillError(const Token* tok, const std::string& b } -void CheckOther::avoidDeadEndInNestedIfs() +void CheckOther::oppositeInnerCondition() { - if (!_settings->isEnabled("style")) + // FIXME: This check is experimental because of #4170 and #4186. Fix those tickets and remove the "experimental". + if (!_settings->isEnabled("style") || !_settings->inconclusive || !_settings->experimental) return; const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase(); @@ -3532,7 +3533,7 @@ void CheckOther::avoidDeadEndInNestedIfs() } } else if (Token::Match(tok, "if ( %any% !=|<|>|<=|>= %any% )")) { if ((tok->strAt(2) == op1Tok->str() && tok->strAt(4) == op2Tok->str()) || (tok->strAt(2) == op2Tok->str() && tok->strAt(4) == op1Tok->str())) - warningDeadCode(toke); + oppositeInnerConditionError(toke); } } } else if (scope->classDef->strAt(3) == "!=") { @@ -3553,7 +3554,7 @@ void CheckOther::avoidDeadEndInNestedIfs() } } else if (Token::Match(tok, "if ( %any% ==|>=|<= %any% )")) { if ((tok->strAt(2) == op1Tok->str() && tok->strAt(4) == op2Tok->str()) || (tok->strAt(2) == op2Tok->str() && tok->strAt(4) == op1Tok->str())) - warningDeadCode(toke); + oppositeInnerConditionError(toke); } } } else if (scope->classDef->strAt(3) == "<") { @@ -3574,7 +3575,7 @@ void CheckOther::avoidDeadEndInNestedIfs() } } else if (Token::Match(tok, "if ( %any% <|<=|>|>=|== %any% )")) { if ((tok->strAt(2) == op1Tok->str() && tok->strAt(4) == op2Tok->str()) || (tok->strAt(2) == op2Tok->str() && tok->strAt(4) == op1Tok->str())) - warningDeadCode(toke); + oppositeInnerConditionError(toke); } } } else if (scope->classDef->strAt(3) == "<=") { @@ -3595,17 +3596,16 @@ void CheckOther::avoidDeadEndInNestedIfs() } } else if (Token::Match(tok, "if ( %any% <|<=|>|>= %any% )")) { if ((tok->strAt(2) == op1Tok->str() && tok->strAt(4) == op2Tok->str()) || (tok->strAt(2) == op2Tok->str() && tok->strAt(4) == op1Tok->str())) - warningDeadCode(toke); + oppositeInnerConditionError(toke); } } } } } - } } -void CheckOther::warningDeadCode(const Token *tok) +void CheckOther::oppositeInnerConditionError(const Token *tok) { - reportError(tok, Severity::warning, "redundantOperationIn", "Opposite conditions in nested 'if' blocks lead to a dead code block.", true); + reportError(tok, Severity::warning, "oppositeInnerCondition", "Opposite conditions in nested 'if' blocks lead to a dead code block.", true); } diff --git a/lib/checkother.h b/lib/checkother.h index 9534ba231..3ffc8772b 100644 --- a/lib/checkother.h +++ b/lib/checkother.h @@ -84,7 +84,7 @@ public: CheckOther checkOther(tokenizer, settings, errorLogger); // Checks - checkOther.avoidDeadEndInNestedIfs(); + checkOther.oppositeInnerCondition(); checkOther.clarifyCalculation(); checkOther.clarifyStatement(); checkOther.checkConstantFunctionParameter(); @@ -116,7 +116,7 @@ public: } /** To check the dead code in a program, which is unaccessible due to the counter-conditions check in nested-if statements **/ - void avoidDeadEndInNestedIfs(); + void oppositeInnerCondition(); /** @brief Clarify calculation for ".. a * b ? .." */ void clarifyCalculation(); @@ -279,7 +279,7 @@ public: private: // Error messages.. - void warningDeadCode(const Token *tok); + void oppositeInnerConditionError(const Token *tok); void clarifyCalculationError(const Token *tok, const std::string &op); void clarifyConditionError(const Token *tok, bool assign, bool boolop); void clarifyStatementError(const Token* tok); @@ -370,7 +370,7 @@ private: c.redundantAssignmentError(0, 0, "var"); // style/warning - c.warningDeadCode(0); + c.oppositeInnerConditionError(0); c.cstyleCastError(0); c.dangerousUsageStrtolError(0, "strtol"); c.passedByValueError(0, "parametername"); diff --git a/test/testother.cpp b/test/testother.cpp index 513c4ff8a..9afb3aeed 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -33,7 +33,7 @@ private: void run() { - TEST_CASE(avoidDeadEndInNestedIfs); + TEST_CASE(oppositeInnerCondition); TEST_CASE(assignBoolToPointer); TEST_CASE(zeroDiv1); @@ -262,13 +262,13 @@ private: } - void avoidDeadEndInNestedIfs() { + void oppositeInnerCondition() { check("void foo(int a, int b)\n" "{\n" " if(a==b)\n" " if(a!=b)\n" " cout << a;\n" - "}"); + "}", "test.cpp", true, true); ASSERT_EQUALS("[test.cpp:3]: (warning, inconclusive) Opposite conditions in nested 'if' blocks lead to a dead code block.\n", errout.str()); check("void foo(int i) \n" @@ -279,7 +279,7 @@ private: " cout << a; \n" " }\n" " }\n" - "}"); + "}", "test.cpp", true, true); ASSERT_EQUALS("", errout.str()); @@ -294,7 +294,7 @@ private: " if(i<5){\n" " }\n" " }\n" - "}"); + "}", "test.cpp", true, true); ASSERT_EQUALS("", errout.str()); }