Opposite inner conditions - made check 'experimental' because there are unsolved false positives.
This commit is contained in:
parent
b06cebe9b5
commit
e7483af028
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue