Skip to content

Fix #15023 FN redundantInitialization with braces or parentheses - #8846

Open
chrchr-github wants to merge 8 commits into
cppcheck-opensource:mainfrom
chrchr-github:chr_15023
Open

chrchr-github wants to merge 8 commits into
cppcheck-opensource:mainfrom
chrchr-github:chr_15023

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
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();

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.

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

  • "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; }

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