From eda5272dfdc510b4d10bfcd359eae3103ae660e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 5 Aug 2015 11:15:54 +0200 Subject: [PATCH] Fixed #6875 (Improve 'Redundant condition' error message) --- lib/checkcondition.cpp | 28 +++++++++++++++++++++++++--- test/testcondition.cpp | 12 ++++++------ 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/lib/checkcondition.cpp b/lib/checkcondition.cpp index ed2278ec3..debe04657 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -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; } } diff --git a/test/testcondition.cpp b/test/testcondition.cpp index ba1ab4be8..172fa95e2 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -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"