diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 086874b90ee..d37761ea97e 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -520,7 +520,9 @@ void CheckOther::checkRedundantAssignment() } if (start->hasKnownSymbolicValue(tokenToCheck) && Token::simpleMatch(start->astParent(), "=") && !diag(tok)) { - redundantAssignmentSameValueError(start, tokenToCheck, tok->astOperand1()->expressionString()); + const ValueFlow::Value* val = start->getKnownValue(ValueFlow::Value::ValueType::SYMBOLIC); + if (val->intvalue == 0) // no offset + redundantAssignmentSameValueError(tokenToCheck, val, tok->astOperand1()->expressionString()); } // Get next assignment.. @@ -593,11 +595,10 @@ void CheckOther::redundantAssignmentInSwitchError(const Token *tok1, const Token "Variable '$symbol' is reassigned a value before the old one has been used. 'break;' missing?", CWE563, Certainty::normal); } -void CheckOther::redundantAssignmentSameValueError(const Token *tok1, const Token* tok2, const std::string &var) +void CheckOther::redundantAssignmentSameValueError(const Token *tok, const ValueFlow::Value* val, const std::string &var) { - const ValueFlow::Value* val = tok1->getKnownValue(ValueFlow::Value::ValueType::SYMBOLIC); auto errorPath = val->errorPath; - errorPath.emplace_back(tok2, ""); + errorPath.emplace_back(tok, ""); reportError(errorPath, Severity::style, "redundantAssignment", "$symbol:" + var + "\n" "Variable '$symbol' is assigned an expression that holds the same value.", CWE563, Certainty::normal); diff --git a/lib/checkother.h b/lib/checkother.h index 9295787207b..13ae3d30b2f 100644 --- a/lib/checkother.h +++ b/lib/checkother.h @@ -254,7 +254,7 @@ class CPPCHECKLIB CheckOther : public Check { void redundantAssignmentError(const Token *tok1, const Token* tok2, const std::string& var, bool inconclusive); void redundantInitializationError(const Token *tok1, const Token* tok2, const std::string& var, bool inconclusive); void redundantAssignmentInSwitchError(const Token *tok1, const Token *tok2, const std::string &var); - void redundantAssignmentSameValueError(const Token *tok1, const Token *tok2, const std::string &var); + void redundantAssignmentSameValueError(const Token* tok, const ValueFlow::Value* val, const std::string& var); void redundantCopyError(const Token *tok1, const Token* tok2, const std::string& var); void redundantBitwiseOperationInSwitchError(const Token *tok, const std::string &varname); void suspiciousCaseInSwitchError(const Token* tok, const std::string& operatorString); diff --git a/test/testother.cpp b/test/testother.cpp index 23baf92285c..49d16833b6a 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -10217,6 +10217,14 @@ class TestOther : public TestFixture { " return a * b * c;\n" "}\n"); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:5]: (style) Redundant initialization for 'b'. The initialized value is overwritten before it is read.\n", errout_str()); + + check("int f(int i) {\n" // #12874 + " int j = i + 1;\n" + " if (i > 5)\n" + " j = i;\n" + " return j;\n" + "}\n"); + ASSERT_EQUALS("", errout_str()); } void varFuncNullUB() { // #4482