Fixed #6875 (Improve 'Redundant condition' error message)

This commit is contained in:
Daniel Marjamäki 2015-08-05 11:15:54 +02:00
parent 4bebb80300
commit eda5272dfd
2 changed files with 31 additions and 9 deletions

View File

@ -642,11 +642,33 @@ void CheckCondition::checkIncorrectLogicOperator()
}
// 'A && (!A || B)' is equivalent with 'A || B'
if (printStyle && (tok->str() == "||") && tok->astOperand1() && tok->astOperand2() && tok->astOperand2()->str() == "&&") {
// 'A && (!A || B)' is equivalent with 'A && B'
// 'A || (!A && B)' is equivalent with 'A || B'
if (printStyle && tok->astOperand1() && tok->astOperand2() &&
((tok->str() == "||" && tok->astOperand2()->str() == "&&") ||
(tok->str() == "&&" && tok->astOperand2()->str() == "||"))) {
const Token* tok2 = tok->astOperand2()->astOperand1();
if (isOppositeCond(true, _tokenizer->isCPP(), tok->astOperand1(), tok2, _settings->library.functionpure)) {
redundantConditionError(tok, tok2->expressionString() + ". 'A && (!A || B)' is equivalent to 'A || B'");
std::string expr1(tok->astOperand1()->expressionString());
std::string expr2(tok->astOperand2()->astOperand1()->expressionString());
std::string expr3(tok->astOperand2()->astOperand2()->expressionString());
if (expr1.length() + expr2.length() + expr3.length() > 50U) {
if (expr1[0] == '!' && expr2[0] != '!') {
expr1 = "!A";
expr2 = "A";
} else {
expr1 = "!A";
expr2 = "A";
}
expr3 = "B";
}
const std::string cond1 = expr1 + " " + tok->str() + " (" + expr2 + " " + tok->astOperand2()->str() + " " + expr3 + ")";
const std::string cond2 = expr1 + " " + tok->str() + " " + expr3;
redundantConditionError(tok, tok2->expressionString() + ". '" + cond1 + "' is equivalent to '" + cond2 + "'");
continue;
}
}

View File

@ -1364,25 +1364,25 @@ private:
" int y = rand(), z = rand();\n"
" if (y || (!y && z));\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (style) Redundant condition: !y. 'A && (!A || B)' is equivalent to 'A || B'\n", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (style) Redundant condition: !y. 'y || (!y && z)' is equivalent to 'y || z'\n", errout.str());
check("void f() {\n"
" int y = rand(), z = rand();\n"
" if (y || !y && z);\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (style) Redundant condition: !y. 'A && (!A || B)' is equivalent to 'A || B'\n", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (style) Redundant condition: !y. 'y || (!y && z)' is equivalent to 'y || z'\n", errout.str());
check("void f() {\n"
" if (!a || a && b) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) Redundant condition: a. 'A && (!A || B)' is equivalent to 'A || B'\n", errout.str());
ASSERT_EQUALS("[test.cpp:2]: (style) Redundant condition: a. '!a || (a && b)' is equivalent to '!a || b'\n", errout.str());
check("void f() {\n"
" if (!tok->next()->function() || \n"
" (tok->next()->function() && tok->next()->function()->isConstructor()));\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) Redundant condition: tok.next().function(). 'A && (!A || B)' is equivalent to 'A || B'\n", errout.str());
ASSERT_EQUALS("[test.cpp:2]: (style) Redundant condition: tok.next().function(). '!A || (A && B)' is equivalent to '!A || B'\n", errout.str());
check("void f() {\n"
" if (!tok->next()->function() || \n"
@ -1400,7 +1400,7 @@ private:
" if (!tok->next(1)->function(1) || \n"
" (tok->next(1)->function(1) && tok->next(1)->function(1)->isConstructor()));\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) Redundant condition: tok.next(1).function(1). 'A && (!A || B)' is equivalent to 'A || B'\n", errout.str());
ASSERT_EQUALS("[test.cpp:2]: (style) Redundant condition: tok.next(1).function(1). '!A || (A && B)' is equivalent to '!A || B'\n", errout.str());
check("void f() {\n"
" if (!tok->next()->function(1) || \n"
@ -1412,7 +1412,7 @@ private:
" int y = rand(), z = rand();\n"
" if (y==0 || y!=0 && z);\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (style) Redundant condition: y!=0. 'A && (!A || B)' is equivalent to 'A || B'\n", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (style) Redundant condition: y!=0. 'y==0 || (y!=0 && z)' is equivalent to 'y==0 || z'\n", errout.str());
check("void f() {\n"
" if (x>0 || (x<0 && y)) {}\n"