Skip to content

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

Merged
chrchr-github merged 8 commits into
cppcheck-opensource:mainfrom
chrchr-github:chr_15023
Oct 10, 2026
Merged

chrchr-github merged 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; }

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.

@dmcppcheck

Copy link
Copy Markdown

Test results for commit 74b9be3 (tools/test-my-pr.py, main compared to this PR):

Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-8846/
packages: 100
Differing warnings: 0 (+0 -0)
Timing: +0.1% (http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-8846/2026-09-30_09-07-47-74b9be34c093_timing.html)

Posted automatically by the cppcheck PR test runner. +N: warnings only with this PR, -N: warnings only with main. The AI review is written by Claude and can be wrong.

@danmar danmar left a comment

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.

lgtm. I assume you have rejected the AI review? Then feel free to merge.

@chrchr-github
chrchr-github merged commit 61dd0f6 into cppcheck-opensource:main Oct 10, 2026
71 of 72 checks passed
@chrchr-github
chrchr-github deleted the chr_15023 branch October 10, 2026 12:39
chrchr-github pushed a commit to chrchr-github/cppcheck that referenced this pull request Oct 10, 2026
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.

3 participants