Skip to content

Commit da9399d

Browse files
Fix #14534 FN redundantInitialization (std::string, initialization with string, regression) (#8262)
1 parent 4d8eab3 commit da9399d

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/checkother.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ void CheckOther::checkRedundantAssignment()
631631
[&](const Token *rhs) {
632632
if (Token::simpleMatch(rhs, "{ 0 }"))
633633
return ChildrenToVisit::none;
634-
if (Token::Match(rhs, "%str%|%num%|%name%") && !rhs->varId())
634+
if (Token::Match(rhs, "%num%|%name%") && !rhs->varId())
635635
return ChildrenToVisit::none;
636636
if (Token::Match(rhs, ":: %name%") && rhs->hasKnownIntValue())
637637
return ChildrenToVisit::none;

test/testother.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10613,6 +10613,21 @@ class TestOther : public TestFixture {
1061310613
ASSERT_EQUALS(
1061410614
"[test.cpp:2:10]: (style) Variable 'a' can be declared as pointer to const [constVariablePointer]\n",
1061510615
errout_str());
10616+
10617+
check("std::string f() {\n" // #14534
10618+
" std::string s = \"abc\";\n"
10619+
" s = \"def\";\n"
10620+
" return s;\n"
10621+
"}\n"
10622+
"const char* g() {\n"
10623+
" const char* p = \"abc\";\n"
10624+
" p = \"def\";\n"
10625+
" return p;\n"
10626+
"}");
10627+
ASSERT_EQUALS(
10628+
"[test.cpp:2:19] -> [test.cpp:3:7]: (style) Redundant initialization for 's'. The initialized value is overwritten before it is read. [redundantInitialization]\n"
10629+
"[test.cpp:7:19] -> [test.cpp:8:7]: (style) Redundant initialization for 'p'. The initialized value is overwritten before it is read. [redundantInitialization]\n",
10630+
errout_str());
1061610631
}
1061710632

1061810633
void redundantVarAssignment_struct() {

0 commit comments

Comments
 (0)