Skip to content

Fix #15021 FN redundantInitialization with structured binding - #8847

Open
chrchr-github wants to merge 3 commits into
cppcheck-opensource:mainfrom
chrchr-github:chr_15021
Open

chrchr-github wants to merge 3 commits into
cppcheck-opensource:mainfrom
chrchr-github:chr_15021

Conversation

@chrchr-github

@chrchr-github chrchr-github commented Sep 11, 2026 •

Copy link
Copy Markdown
Collaborator

Best viewed with whitespace changes hidden.

@chrchr-github chrchr-github added the merge-after-next-release Wait with merging this PR until after the next Release label Sep 11, 2026
@chrchr-github
chrchr-github marked this pull request as ready for review September 11, 2026 10:49
@chrchr-github chrchr-github removed the merge-after-next-release Wait with merging this PR until after the next Release label Sep 20, 2026
Comment thread lib/checkother.cpp
Comment on lines +685 to +687
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(), ",");

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants