diff --git a/lib/checkother.cpp b/lib/checkother.cpp index c4c4cf049b2..ce42524f02d 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -631,7 +631,7 @@ void CheckOther::checkRedundantAssignment() [&](const Token *rhs) { if (Token::simpleMatch(rhs, "{ 0 }")) return ChildrenToVisit::none; - if (Token::Match(rhs, "%str%|%num%|%name%") && !rhs->varId()) + if (Token::Match(rhs, "%num%|%name%") && !rhs->varId()) return ChildrenToVisit::none; if (Token::Match(rhs, ":: %name%") && rhs->hasKnownIntValue()) return ChildrenToVisit::none; diff --git a/test/testother.cpp b/test/testother.cpp index c2781cbab6c..d49d3e540d2 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -10613,6 +10613,21 @@ class TestOther : public TestFixture { ASSERT_EQUALS( "[test.cpp:2:10]: (style) Variable 'a' can be declared as pointer to const [constVariablePointer]\n", errout_str()); + + check("std::string f() {\n" // #14534 + " std::string s = \"abc\";\n" + " s = \"def\";\n" + " return s;\n" + "}\n" + "const char* g() {\n" + " const char* p = \"abc\";\n" + " p = \"def\";\n" + " return p;\n" + "}"); + ASSERT_EQUALS( + "[test.cpp:2:19] -> [test.cpp:3:7]: (style) Redundant initialization for 's'. The initialized value is overwritten before it is read. [redundantInitialization]\n" + "[test.cpp:7:19] -> [test.cpp:8:7]: (style) Redundant initialization for 'p'. The initialized value is overwritten before it is read. [redundantInitialization]\n", + errout_str()); } void redundantVarAssignment_struct() {