Skip to content

Commit f207126

Browse files
committed
Fix ID, add tests
1 parent 67b4065 commit f207126

2 files changed

Lines changed: 25 additions & 7 deletions

File tree

‎lib/checkother.cpp‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,8 +690,10 @@ void CheckOtherImpl::checkRedundantAssignment()
690690
start = tok->findExpressionStartEndTokens().second->next();
691691

692692
std::vector<const Token*> tokensToCheck{ tok->astOperand1() };
693-
if (Token::simpleMatch(tok->astOperand1(), "[") && Token::simpleMatch(tok->astOperand1()->astOperand1(), "auto")) // structured binding
693+
if (Token::simpleMatch(tok->astOperand1(), "[") && Token::simpleMatch(tok->astOperand1()->astOperand1(), "auto")) { // structured binding
694694
tokensToCheck = astFlatten(tok->astOperand1()->astOperand2(), ",");
695+
isInitialization = true;
696+
}
695697

696698
for (const Token* tokenToCheck : tokensToCheck) {
697699

‎test/testother.cpp‎

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11569,16 +11569,32 @@ class TestOther : public TestFixture {
1156911569
" z = 0;\n"
1157011570
" return z;\n"
1157111571
"}\n");
11572-
ASSERT_EQUALS("[test.cpp:4:7]: style: Variable 'x' is reassigned a value before the old one has been used. [redundantAssignment]\n"
11573-
"[test.cpp:3:17]: note: x is assigned\n"
11572+
ASSERT_EQUALS("[test.cpp:4:7]: style: Redundant initialization for 'x'. The initialized value is overwritten before it is read. [redundantInitialization]\n"
11573+
"[test.cpp:3:17]: note: x is initialized\n"
1157411574
"[test.cpp:4:7]: note: x is overwritten\n"
11575-
"[test.cpp:5:7]: style: Variable 'y' is reassigned a value before the old one has been used. [redundantAssignment]\n"
11576-
"[test.cpp:3:17]: note: y is assigned\n"
11575+
"[test.cpp:5:7]: style: Redundant initialization for 'y'. The initialized value is overwritten before it is read. [redundantInitialization]\n"
11576+
"[test.cpp:3:17]: note: y is initialized\n"
1157711577
"[test.cpp:5:7]: note: y is overwritten\n"
11578-
"[test.cpp:11:7]: style: Variable 'z' is reassigned a value before the old one has been used. [redundantAssignment]\n"
11579-
"[test.cpp:10:14]: note: z is assigned\n"
11578+
"[test.cpp:11:7]: style: Redundant initialization for 'z'. The initialized value is overwritten before it is read. [redundantInitialization]\n"
11579+
"[test.cpp:10:14]: note: z is initialized\n"
1158011580
"[test.cpp:11:7]: note: z is overwritten\n",
1158111581
errout_str());
11582+
11583+
check("struct S {\n"
11584+
" int a, b;\n"
11585+
"};\n"
11586+
"void f(S& s) {\n"
11587+
" auto& [x, y] = s;\n"
11588+
" y = 1;\n"
11589+
"}\n"
11590+
"int f(S s) {\n"
11591+
" auto [x, y] = s;\n"
11592+
" y = 1;\n"
11593+
"}\n");
11594+
ASSERT_EQUALS("[test.cpp:10:7]: style: Redundant initialization for 'y'. The initialized value is overwritten before it is read. [redundantInitialization]\n"
11595+
"[test.cpp:9:17]: note: y is initialized\n"
11596+
"[test.cpp:10:7]: note: y is overwritten\n",
11597+
errout_str());
1158211598
}
1158311599

1158411600
// cppcheck-suppress unusedPrivateFunction

0 commit comments

Comments
 (0)