diff --git a/lib/checkcondition.cpp b/lib/checkcondition.cpp index e75335576..ed3fde735 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -1571,16 +1571,17 @@ void CheckCondition::alwaysTrueFalse() if (hasSizeof) continue; - alwaysTrueFalseError(tok, &tok->values().front()); + alwaysTrueFalseError(tok, condition, &tok->values().front()); } } } -void CheckCondition::alwaysTrueFalseError(const Token *tok, const ValueFlow::Value *value) +void CheckCondition::alwaysTrueFalseError(const Token* tok, const Token* condition, const ValueFlow::Value* value) { const bool alwaysTrue = value && (value->intvalue != 0); const std::string expr = tok ? tok->expressionString() : std::string("x"); - const std::string errmsg = "Condition '" + expr + "' is always " + (alwaysTrue ? "true" : "false"); + const std::string conditionStr = (Token::simpleMatch(condition, "return") ? "Return" : "Condition"); + const std::string errmsg = conditionStr + " '" + expr + "' is always " + (alwaysTrue ? "true" : "false"); const ErrorPath errorPath = getErrorPath(tok, value, errmsg); reportError(errorPath, Severity::style, diff --git a/lib/checkcondition.h b/lib/checkcondition.h index fa894b728..da62c48fe 100644 --- a/lib/checkcondition.h +++ b/lib/checkcondition.h @@ -160,7 +160,7 @@ private: void clarifyConditionError(const Token *tok, bool assign, bool boolop); - void alwaysTrueFalseError(const Token *tok, const ValueFlow::Value *value); + void alwaysTrueFalseError(const Token* tok, const Token* condition, const ValueFlow::Value* value); void invalidTestForOverflow(const Token* tok, const ValueType *valueType, const std::string &replace); void pointerAdditionResultNotNullError(const Token *tok, const Token *calc); @@ -190,7 +190,7 @@ private: c.redundantConditionError(nullptr, "If x > 11 the condition x > 10 is always true.", false); c.moduloAlwaysTrueFalseError(nullptr, "1"); c.clarifyConditionError(nullptr, true, false); - c.alwaysTrueFalseError(nullptr, nullptr); + c.alwaysTrueFalseError(nullptr, nullptr, nullptr); c.invalidTestForOverflow(nullptr, nullptr, "false"); c.pointerAdditionResultNotNullError(nullptr, nullptr); c.duplicateConditionalAssignError(nullptr, nullptr); diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 7fc125370..4f98fc821 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -1309,7 +1309,7 @@ private: check("int f(char c) {\n" " return (c <= 'a' && c >= 'z');\n" "}", "test.cpp", false); - ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Condition 'c>='z'' is always false\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Return 'c>='z'' is always false\n", errout.str()); } void incorrectLogicOperator7() { // opposite expressions @@ -1885,7 +1885,7 @@ private: " return a % 5 > 5;\n" "}"); ASSERT_EQUALS( - "[test.cpp:7]: (style) Condition 'a%5>5' is always false\n" + "[test.cpp:7]: (style) Return 'a%5>5' is always false\n" "[test.cpp:2]: (warning) Comparison of modulo result is predetermined, because it is always less than 5.\n" "[test.cpp:3]: (warning) Comparison of modulo result is predetermined, because it is always less than 5.\n" "[test.cpp:4]: (warning) Comparison of modulo result is predetermined, because it is always less than 5.\n" @@ -3205,7 +3205,7 @@ private: " if(x == 0) { x++; return x == 0; }\n" " return false;\n" "}"); - ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Condition 'x==0' is always false\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Return 'x==0' is always false\n", errout.str()); check("void f() {\n" // #6898 (Token::expressionString) " int x = 0;\n" @@ -3426,7 +3426,7 @@ private: " const int b = 52;\n" " return a+b;\n" "}"); - ASSERT_EQUALS("[test.cpp:4]: (style) Condition 'a+b' is always true\n", errout.str()); + ASSERT_EQUALS("[test.cpp:4]: (style) Return 'a+b' is always true\n", errout.str()); check("int f() {\n" " int a = 50;\n" @@ -3990,7 +3990,7 @@ private: check("bool f(bool a, bool b) {\n" " return a || ! b || ! a;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Condition '!a' is always true\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Return '!a' is always true\n", errout.str()); // #10148 check("void f(int i) {\n" @@ -4419,7 +4419,7 @@ private: "[test.cpp:4]: (style) Condition 's.empty()' is always true\n" "[test.cpp:5]: (style) Condition 's.empty()' is always true\n" "[test.cpp:6]: (style) Condition '(bool)0' is always false\n" - "[test.cpp:7]: (style) Condition 's.empty()' is always true\n", + "[test.cpp:7]: (style) Return 's.empty()' is always true\n", errout.str()); check("int f(bool b) {\n" @@ -4430,8 +4430,8 @@ private: " if (b) return static_cast(1);\n" " return (int)0;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:6]: (style) Condition 'static_cast(1)' is always true\n" - "[test.cpp:7]: (style) Condition '(int)0' is always false\n", + ASSERT_EQUALS("[test.cpp:6]: (style) Return 'static_cast(1)' is always true\n" + "[test.cpp:7]: (style) Return '(int)0' is always false\n", errout.str()); check("int f() { return 3; }\n" @@ -4439,7 +4439,7 @@ private: "int h() { if (f()) {} }\n" "int i() { return f() == 3; }\n"); ASSERT_EQUALS("[test.cpp:3]: (style) Condition 'f()' is always true\n" - "[test.cpp:4]: (style) Condition 'f()==3' is always true\n", + "[test.cpp:4]: (style) Return 'f()==3' is always true\n", errout.str()); check("int f() {\n" @@ -4489,7 +4489,7 @@ private: " return (index++) >= s;\n" " }\n" "}\n"); - ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:6]: (style) Condition '(index++)>=s' is always false\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:6]: (style) Return '(index++)>=s' is always false\n", errout.str()); check("struct a {\n" " a *b() const;\n" @@ -4818,7 +4818,7 @@ private: check("bool f(const int *p, const int *q) {\n" " return p != NULL && q != NULL && p == NULL;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:2]: (style) Condition 'p==NULL' is always false\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2]: (style) Return 'p==NULL' is always false\n", errout.str()); } void alwaysTrueContainer() { @@ -5370,7 +5370,7 @@ private: check("bool f(const std::string &s) {\n" " return s.size()>2U && s[0]=='4' && s[0]=='2';\n" "}\n"); - ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Condition 's[0]=='2'' is always false\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Return 's[0]=='2'' is always false\n", errout.str()); } void pointerAdditionResultNotNull() {