Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 47 additions & 40 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -682,53 +682,60 @@ void CheckOtherImpl::checkRedundantAssignment()
else
start = tok->findExpressionStartEndTokens().second->next();

const Token * tokenToCheck = tok->astOperand1();
std::vector<const Token*> tokensToCheck{ tok->astOperand1() };
if (Token::simpleMatch(tok->astOperand1(), "[") && Token::simpleMatch(tok->astOperand1()->astOperand1(), "auto")) // structured binding
tokensToCheck = astFlatten(tok->astOperand1()->astOperand2(), ",");
Comment on lines +685 to +687

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an AI review. Take it with a grain of salt and feel free to reject it by resolving the comment.

Looks good to me. I built it and checked that reference bindings don't produce false positives: auto& [x, y] = s; x = 1;, auto&& [x, y] = std::move(s);, for (auto& [k, v] : m) v = 0; and auto& [x, y] = arr; all stay silent. auto [x, y] = p; with a std::pair is reported.

Question: the ticket and the PR title say "FN redundantInitialization", but since isInitialization is only set for split ; %var% = declarations, the structured binding is reported as redundantAssignment ("is reassigned a value before the old one has been used", with notes "x is assigned" / "x is overwritten"). Semantically auto [x, y] = s; is an initialization. Should isInitialization be set for the structured binding case so that redundantInitialization is reported, including the existing "trivial rhs" handling? If redundantAssignment is intended, maybe adjust the title so the history matches.


// Check if we are working with union
for (const Token* tempToken = tokenToCheck; Token::simpleMatch(tempToken, ".");) {
tempToken = tempToken->astOperand1();
if (tempToken && tempToken->variable() && tempToken->variable()->type() && tempToken->variable()->type()->isUnionType())
tokenToCheck = tempToken;
}
for (const Token* tokenToCheck : tokensToCheck) {

if (start->hasKnownSymbolicValue(tokenToCheck) && Token::simpleMatch(start->astParent(), "=") && !diag(tok)) {
const ValueFlow::Value* val = start->getKnownValue(ValueFlow::Value::ValueType::SYMBOLIC);
if (val->intvalue == 0) // no offset
redundantAssignmentSameValueError(tokenToCheck, val, tok->astOperand1()->expressionString());
}
// Check if we are working with union
for (const Token* tempToken = tokenToCheck; Token::simpleMatch(tempToken, ".");) {
tempToken = tempToken->astOperand1();
if (tempToken && tempToken->variable() && tempToken->variable()->type() && tempToken->variable()->type()->isUnionType())
tokenToCheck = tempToken;
}

// Get next assignment..
const Token *nextAssign = fwdAnalysis.reassign(tokenToCheck, start, scope->bodyEnd);
// extra check for union
if (nextAssign && tokenToCheck != tok->astOperand1()) {
nextAssign = fwdAnalysis.reassign(tok->astOperand1(), start, scope->bodyEnd);
// reading another member of the same union in the rhs is a use through aliasing
if (nextAssign && fwdAnalysis.hasOperand(nextAssign->astOperand2(), tokenToCheck))
nextAssign = nullptr;
}
if (start->hasKnownSymbolicValue(tokenToCheck) && Token::simpleMatch(start->astParent(), "=") && !diag(tok)) {
const ValueFlow::Value* val = start->getKnownValue(ValueFlow::Value::ValueType::SYMBOLIC);
if (val->intvalue == 0) // no offset
redundantAssignmentSameValueError(tokenToCheck, val, tok->astOperand1()->expressionString());
}

if (!nextAssign)
continue;
// Get next assignment..
const Token* nextAssign = fwdAnalysis.reassign(tokenToCheck, start, scope->bodyEnd);
// extra check for union
const bool isUnion = tokenToCheck != tok->astOperand1() && !isStructuredBindingVariable(tokenToCheck->variable());
if (nextAssign && isUnion) {
nextAssign = fwdAnalysis.reassign(tok->astOperand1(), start, scope->bodyEnd);
// reading another member of the same union in the rhs is a use through aliasing
if (nextAssign && fwdAnalysis.hasOperand(nextAssign->astOperand2(), tokenToCheck))
nextAssign = nullptr;
}

// there is redundant assignment. Is there a case between the assignments?
bool hasCase = false;
for (const Token *tok2 = tok; tok2 != nextAssign; tok2 = tok2->next()) {
if (tok2->str() == "break" || tok2->str() == "return")
break;
if (tok2->str() == "case") {
hasCase = true;
break;
if (!nextAssign)
continue;

// there is redundant assignment. Is there a case between the assignments?
bool hasCase = false;
for (const Token* tok2 = tok; tok2 != nextAssign; tok2 = tok2->next()) {
if (tok2->str() == "break" || tok2->str() == "return")
break;
if (tok2->str() == "case") {
hasCase = true;
break;
}
}
}

// warn
if (hasCase)
redundantAssignmentInSwitchError(tok, nextAssign, tok->astOperand1()->expressionString());
else if (isInitialization)
redundantInitializationError(tok, nextAssign, tok->astOperand1()->expressionString(), inconclusive);
else {
diag(nextAssign);
redundantAssignmentError(tok, nextAssign, tok->astOperand1()->expressionString(), inconclusive);
// warn
const Token* exprTok = isUnion ? tok->astOperand1() : tokenToCheck;
if (hasCase)
redundantAssignmentInSwitchError(tok, nextAssign, exprTok->expressionString());
else if (isInitialization)
redundantInitializationError(tok, nextAssign, exprTok->expressionString(), inconclusive);
else {
diag(nextAssign);
redundantAssignmentError(tok, nextAssign, exprTok->expressionString(), inconclusive);
}
}
}
}
Expand Down
24 changes: 24 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11519,6 +11519,30 @@ class TestOther : public TestFixture {
"[test.cpp:8:8]: style: Redundant initialization for 'i'. The initialized value is overwritten before it is read. [redundantInitialization]\n"
"[test.cpp:7:12]: note: i is initialized\n"
"[test.cpp:8:8]: note: i is overwritten\n", errout_str());

check("struct S { int a, b; };\n" // #15021
"int f(S s) {\n"
" auto [x, y] = s;\n"
" x = 1;\n"
" y = 2;\n"
" return x + y;\n"
"}\n"
"struct T { int c; };\n"
"int g(T t) {\n"
" auto [z] = t;\n"
" z = 0;\n"
" return z;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4:7]: style: Variable 'x' is reassigned a value before the old one has been used. [redundantAssignment]\n"
"[test.cpp:3:17]: note: x is assigned\n"
"[test.cpp:4:7]: note: x is overwritten\n"
"[test.cpp:5:7]: style: Variable 'y' is reassigned a value before the old one has been used. [redundantAssignment]\n"
"[test.cpp:3:17]: note: y is assigned\n"
"[test.cpp:5:7]: note: y is overwritten\n"
"[test.cpp:11:7]: style: Variable 'z' is reassigned a value before the old one has been used. [redundantAssignment]\n"
"[test.cpp:10:14]: note: z is assigned\n"
"[test.cpp:11:7]: note: z is overwritten\n",
errout_str());
}

// cppcheck-suppress unusedPrivateFunction
Expand Down
Loading