Fix #15023 FN redundantInitialization with braces or parentheses - #8846
Open
chrchr-github wants to merge 8 commits into
Open
chrchr-github wants to merge 8 commits into
chrchr-github wants to merge 8 commits into
Conversation
chrchr-github
marked this pull request as ready for review
September 11, 2026 10:49
danmar
reviewed
Oct 1, 2026
| if (tok->isCpp() && tok->astOperand1()->valueType()) { | ||
| // If there is a custom assignment operator => this is inconclusive | ||
| if (tok->astOperand1()->valueType()->typeScope) { | ||
| const std::string op = "operator" + tok->str(); |
Collaborator
There was a problem hiding this comment.
This is an AI review. Take it with a grain of salt and feel free to reject it by resolving the comment.
For the new ( / { initialization forms, tok->str() is ( or {, so this looks for operator( / operator{ and the custom-assignment-operator check never triggers. Result with this PR:
int g();
struct A { A(int); A& operator=(int); A& operator=(const A&); };
void f1() { A a(g()); a = 1; } // redundantInitialization (conclusive)
void f2() { A a = g(); a = 1; } // redundantInitialization (inconclusive)I'd expect both to be inconclusive. What matters is the later a = 1, so maybe:
Suggested change
| const std::string op = "operator" + tok->str(); | |
| const std::string op = isInitialization && Token::Match(tok, "[{(]") ? "operator=" : "operator" + tok->str(); |
With that, f1 becomes inconclusive like f2, P p{ g(), g() }; p = P{1, 2}; for a plain struct is still reported conclusively, and TestOther passes.
FYI, this PR and #8847 restructure the same block in checkRedundantAssignment(), so whichever is merged second will need a rebase.
Collaborator
Author
There was a problem hiding this comment.
"operator" + tok->str();should always be"operator="- the inconclusive logic is flawed already, there is a conclusive warning for
S f() { S s = g(); s = 1; return s; }
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Best viewed with whitespace changes hidden.