Opposite inner conditions - made check 'experimental' because there are unsolved false positives.

This commit is contained in:
Daniel Marjamäki 2012-10-21 18:18:29 +02:00
parent b06cebe9b5
commit e7483af028
3 changed files with 18 additions and 18 deletions

View File

@ -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; return;
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase(); const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
@ -3532,7 +3533,7 @@ void CheckOther::avoidDeadEndInNestedIfs()
} }
} else if (Token::Match(tok, "if ( %any% !=|<|>|<=|>= %any% )")) { } 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())) 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) == "!=") { } else if (scope->classDef->strAt(3) == "!=") {
@ -3553,7 +3554,7 @@ void CheckOther::avoidDeadEndInNestedIfs()
} }
} else if (Token::Match(tok, "if ( %any% ==|>=|<= %any% )")) { } 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())) 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) == "<") { } else if (scope->classDef->strAt(3) == "<") {
@ -3574,7 +3575,7 @@ void CheckOther::avoidDeadEndInNestedIfs()
} }
} else if (Token::Match(tok, "if ( %any% <|<=|>|>=|== %any% )")) { } 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())) 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) == "<=") { } else if (scope->classDef->strAt(3) == "<=") {
@ -3595,17 +3596,16 @@ void CheckOther::avoidDeadEndInNestedIfs()
} }
} else if (Token::Match(tok, "if ( %any% <|<=|>|>= %any% )")) { } 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())) 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);
} }

View File

@ -84,7 +84,7 @@ public:
CheckOther checkOther(tokenizer, settings, errorLogger); CheckOther checkOther(tokenizer, settings, errorLogger);
// Checks // Checks
checkOther.avoidDeadEndInNestedIfs(); checkOther.oppositeInnerCondition();
checkOther.clarifyCalculation(); checkOther.clarifyCalculation();
checkOther.clarifyStatement(); checkOther.clarifyStatement();
checkOther.checkConstantFunctionParameter(); 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 **/ /** 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 ? .." */ /** @brief Clarify calculation for ".. a * b ? .." */
void clarifyCalculation(); void clarifyCalculation();
@ -279,7 +279,7 @@ public:
private: private:
// Error messages.. // Error messages..
void warningDeadCode(const Token *tok); void oppositeInnerConditionError(const Token *tok);
void clarifyCalculationError(const Token *tok, const std::string &op); void clarifyCalculationError(const Token *tok, const std::string &op);
void clarifyConditionError(const Token *tok, bool assign, bool boolop); void clarifyConditionError(const Token *tok, bool assign, bool boolop);
void clarifyStatementError(const Token* tok); void clarifyStatementError(const Token* tok);
@ -370,7 +370,7 @@ private:
c.redundantAssignmentError(0, 0, "var"); c.redundantAssignmentError(0, 0, "var");
// style/warning // style/warning
c.warningDeadCode(0); c.oppositeInnerConditionError(0);
c.cstyleCastError(0); c.cstyleCastError(0);
c.dangerousUsageStrtolError(0, "strtol"); c.dangerousUsageStrtolError(0, "strtol");
c.passedByValueError(0, "parametername"); c.passedByValueError(0, "parametername");

View File

@ -33,7 +33,7 @@ private:
void run() { void run() {
TEST_CASE(avoidDeadEndInNestedIfs); TEST_CASE(oppositeInnerCondition);
TEST_CASE(assignBoolToPointer); TEST_CASE(assignBoolToPointer);
TEST_CASE(zeroDiv1); TEST_CASE(zeroDiv1);
@ -262,13 +262,13 @@ private:
} }
void avoidDeadEndInNestedIfs() { void oppositeInnerCondition() {
check("void foo(int a, int b)\n" check("void foo(int a, int b)\n"
"{\n" "{\n"
" if(a==b)\n" " if(a==b)\n"
" if(a!=b)\n" " if(a!=b)\n"
" cout << a;\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()); 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" check("void foo(int i) \n"
@ -279,7 +279,7 @@ private:
" cout << a; \n" " cout << a; \n"
" }\n" " }\n"
" }\n" " }\n"
"}"); "}", "test.cpp", true, true);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
@ -294,7 +294,7 @@ private:
" if(i<5){\n" " if(i<5){\n"
" }\n" " }\n"
" }\n" " }\n"
"}"); "}", "test.cpp", true, true);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }