Skip to content

Fix #15304 (crash in exprDependsOnThis: using-declaration replaces injected-class-name inside class definition) - #9108

Open
danmar wants to merge 4 commits into
cppcheck-opensource:mainfrom
cppchecksolutions:fix-using-injected-class-name
Open

danmar wants to merge 4 commits into
cppcheck-opensource:mainfrom
cppchecksolutions:fix-using-injected-class-name

Conversation

@danmar

@danmar danmar commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator

using N::C; caused simplifyUsing to replace C inside the definition of class N::C, e.g. ~C() became ~N::C(). The destructor was then not recognized and its body was parsed as class members, creating bogus function declarations whose arguments have no scope. exprDependsOnThis dereferenced that null scope.

Seen when checking quickshell-0.3.2/src/services/polkit/listener.cpp.

…jected-class-name inside class definition)

`using N::C;` caused simplifyUsing to replace `C` inside the definition of
class N::C, e.g. `~C()` became `~N::C()`. The destructor was then not
recognized and its body was parsed as class members, creating bogus function
declarations whose arguments have no scope. exprDependsOnThis dereferenced
that null scope.

Seen when checking quickshell-0.3.2/src/services/polkit/listener.cpp.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@danmar danmar added the ai-assisted contains code which was written with the assistance of AI - please review extra carefully label Dec 3, 2026

@dmcppcheck dmcppcheck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

AI review

The fix works for the case in the PR, where a class is defined inside its namespace. It misses two closely related forms of the same bug:

  • Class defined with a qualified name (class N::C { ... };): ScopeInfo3::name of that record is "N :: C", so s->name != tok1->str() never matches. ~C() still becomes ~ N :: C ( ), the constructor becomes N :: C (...), and the class still gets a false noConstructor warning.
  • Out-of-line destructor (N::C::~C() {...}): this is still rewritten to N :: C :: ~ N :: C ( ). The destructor body is then not linked to its declaration, and warnings such as noCopyConstructor are lost.

Neither form is a regression; base behaves the same. The null check added in exprDependsOnThis is fine. I found no other problems.

Comments

# Comment By Where Status
1 Records declared as class N::C { have name "N :: C", so the injected-class-name is still replaced AI review lib/tokenize.cpp:2827 🆕 New
2 Out-of-line N::C::~C() is still rewritten to ~ N :: C, so the destructor body is not linked AI review lib/tokenize.cpp:2825 🆕 New (false negative)

@danmar, please fix, resolve or reply to the open and new comments: #1, #2.
For #2 (false negative), it is enough to create a Trac ticket and link it here. It does not need to be fixed in this PR.

Testing

I built cppcheck and testrunner in pr/ and cppcheck in base/.

Existing tests: ./testrunner TestSimplifyUsing TestSimplifyTypedef TestTokenizer TestSymbolDatabase TestAstUtils ran 1118 tests and none failed.

Case from the PR (t1.cpp): with the PR, the tokens stay ~ C ( ). --enable=all now reports what it reports when there is no using.

Class defined with a qualified name (t2.cpp): this is still wrong with the PR.

namespace N { class C; }
class N::C {
    C(int* p) : p(p) {}
    ~C() { unref(p); }
    C& operator=(const C& other);
    int* p;
};
using N::C;
C c;

pr/cppcheck -q --debug-normal t2.cpp prints:

3: N :: C ( int * p@var1 ) : p@var3 ( p@var1 ) { }
4: ~ N :: C ( ) { unref ( p@var3 ) ; }

pr/cppcheck -q --enable=all t2.cpp prints t2.cpp:2:1: style: The class 'C' does not declare a constructor although it has private member variables which likely require initialization. [noConstructor]. That warning is wrong. Without the using line, the same file gives noExplicitConstructor instead. Base gives the same output as the PR.

Out-of-line destructor (t5.cpp):

namespace N {
class C {
public:
    C();
    ~C();
    int* p;
};
}
using N::C;
N::C::C() : p(new int) {}
N::C::~C() { delete p; }

--debug-normal prints 11: N :: C :: ~ N :: C ( ) { delete p ; }: p has no varid, so the body is not linked to the destructor.

t3.cpp (same pattern, with unref(p); delete p; in the out-of-line destructor): with the using line, noCopyConstructor, noOperatorEq and unusedPrivateFunction are missing. Without the using line they are reported.


Automated review of 84f55fb by Claude. The pull request was built and tested locally.

Comment thread lib/tokenize.cpp Resolved
Comment thread lib/tokenize.cpp Resolved
…ne destructor

Address review comments:
- For "class N::C {" the record name is "N :: C", compare the injected-class-name
  against the last component.
- Never replace the name after "~", e.g. in "N::C::~C()".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@dmcppcheck dmcppcheck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

AI review

Regression: the new early return if (tok1->strAt(-1) == "~") return false; treats every ~ before the alias name as a destructor name. In an expression, ~ is bitwise NOT, so ~U(0) and ~T{1} are no longer expanded when U/T is a type alias. The type information for these expressions is lost: base reports knownConditionTrueFalse for U8 m = ~U8(0); if (m == 255) and the PR does not. Limiting the check to destructor contexts would avoid this, for example :: before the ~, a class or member-function scope whose name matches, or ./-> before the ~.

The fixes for the two earlier review comments (class defined as class N::C { and out-of-line N::C::~C()) are in place and tested. Apart from this regression I found no new problems.

Comments

# Comment By Where Status
1 Class defined as class N::C { still gets injected-class-name replaced AI review general ✅ Fixed
2 Out-of-line N::C::~C() still rewritten to ~ N :: C AI review general ✅ Fixed
3 Unconditional ~ check also skips bitwise NOT like ~U(0), losing alias expansion AI review lib/tokenize.cpp:2824 🆕 New (false negative)

@danmar, please fix, resolve or reply to the open and new comments: #3.
For #3 (false negative), it is enough to create a Trac ticket and link it here. It does not need to be fixed in this PR.

Testing

I built cppcheck and testrunner in pr/ and cppcheck in base/.

Existing tests: ./testrunner TestSimplifyUsing TestSimplifyTypedef TestTokenizer TestSymbolDatabase TestAstUtils TestClass TestConstructors ran 1495 tests and none failed. This includes the new tests simplifyUsing43–45.

Regression: a type alias after unary ~ is no longer simplified

a.cpp:

using U = unsigned char;
unsigned f() {
    unsigned x = ~U(0);
    return x;
}

cppcheck -q --debug-normal a.cpp:

  • base: x@var1 = ~ unsigned char ( 0 ) ;
  • PR: x@var1 = ~ U ( 0 ) ;

c.cpp: using T = int; int h(int v) { return v & ~T{1}; }

  • base: ~ int { 1 }
  • PR: ~ T { 1 }

b.cpp:

using U8 = unsigned char;
int g() {
    U8 m = ~U8(0);
    if (m == 255) return 1;
    return 0;
}

cppcheck -q --enable=all b.cpp:

  • base: b.cpp:5:11: style: Condition 'm==255' is always true [knownConditionTrueFalse], with the note Assignment 'm=~unsigned char(0)', assigned value is 255
  • PR: the warning is not reported. This is a lost true positive.

Automated review of c16acbf by Claude. The pull request was built and tested locally.

Comment thread lib/tokenize.cpp Outdated
}

// destructor name, i.e. "~C" in "~C()" or "N::C::~C()"
if (tok1->strAt(-1) == "~")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

AI review

This returns false for every ~ before the alias name, including the unary bitwise NOT operator in expressions. With using U8 = unsigned char;, U8 m = ~U8(0); now stays as ~ U8 ( 0 ). Base expanded it to ~ unsigned char ( 0 ). The value is lost: base reports knownConditionTrueFalse for a following if (m == 255) and the PR does not. The same happens with ~T{1}.

Please limit the check to destructor contexts: :: before the ~ (N::C::~C), ./-> before the ~ (pseudo-destructor call), or a class or member-function scope whose name matches. Please also add a test with an alias used after unary ~.

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.

Fixed in e129ca0. The check is now Token::Match(tok1->previous(), "~ %name% ( )"), so unary ~ in expressions like ~U(0) / ~T{1} is simplified again. Added test simplifyUsing46.

The name after "~" was never replaced, which also affected expressions
like "~U(0)" where U is a type alias. Limit the check to "~ %name% ( )".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@dmcppcheck dmcppcheck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

AI review

The fixes for the earlier comments are in place. One new problem remains. The destructor check Token::Match(tok1->previous(), "~ %name% ( )") on lib/tokenize.cpp:2824 is still too broad, so a type alias in two places is no longer expanded:

  • Value-initialised bitwise NOT, e.g. ~U8(): the value is lost. Base reports knownConditionTrueFalse here and the PR does not.
  • Pseudo-destructor call through an alias, e.g. y->~Y(): the alias name Y stays in the token list.

I found no other problems.

Comments

# Comment By Where Status
1 Class defined as class N::C { still had its injected-class-name replaced AI review lib/tokenize.cpp:2827 ✅ Fixed
2 Out-of-line N::C::~C() was rewritten to ~ N :: C, so the destructor body was not linked AI review lib/tokenize.cpp:2829 ✅ Fixed
3 Unconditional ~ check also skipped bitwise NOT like ~U(0) AI review lib/tokenize.cpp:2824 ✅ Fixed
4 ~ %name% ( ) also matches ~U() expressions and p->~Alias(), so the alias is no longer expanded AI review lib/tokenize.cpp:2824 🆕 New

@danmar, please fix, resolve or reply to the open and new comments: #4.

Testing

I built cppcheck and testrunner in pr/ and cppcheck in base/.

Tests: ./testrunner TestSimplifyUsing TestSimplifyTypedef TestTokenizer TestSymbolDatabase TestClass TestConstructors reports 0 failures. This includes the new tests simplifyUsing43–46.

Regression: ~Alias() as an expression (a.cpp):

using U8 = unsigned char;
int g() {
    U8 m = ~U8();
    if (m == 255) return 1;
    return 0;
}

Command: cppcheck -q --enable=all --debug-normal a.cpp

  • base: m@var1 = ~ unsigned char ( ) ; and a.cpp:4:11: style: Condition 'm==255' is always true [knownConditionTrueFalse]
  • PR: m@var1 = ~ U8 ( ) ; and no knownConditionTrueFalse. A true positive is lost.

Pseudo-destructor through an alias (b.cpp, excerpt):

template<class T> struct X { ~X(); };
using Y = X<int>;
void h(Y* y) { y->~Y(); }
  • base: y@var5 . ~ X<int> ( ) ;
  • PR: y@var5 . ~ Y ( ) ;. The alias name is left in the token list.

Injected-class-name checks (b.cpp): I also tried these cases: an alias with the class's name declared in another namespace, a nested class that uses an alias from its enclosing class, and C c; inside a member function of C. The PR gives the same tokens and warnings as base for them.


Automated review of e129ca0 by Claude. The pull request was built and tested locally.

Comment thread lib/tokenize.cpp Outdated
}

// destructor name, i.e. "~C" in "~C()" or "N::C::~C()"
if (Token::Match(tok1->previous(), "~ %name% ( )"))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

AI review

~ %name% ( ) still matches some cases that are not the class's own destructor name:

  • Value-initialised bitwise NOT: in using U8 = unsigned char; U8 m = ~U8(); if (m == 255) ... the token list keeps ~ U8 ( ). Base expanded it to ~ unsigned char ( ). The value is lost: base reports knownConditionTrueFalse here and the PR does not.
  • Pseudo-destructor call through an alias: in using Y = X<int>; void h(Y* y) { y->~Y(); } the token list now has y . ~ Y ( ). Base had ~ X<int> ( ).

The skip should only apply when the name after ~ is the class's own name. That is the case when there is a %name% :: before the ~ whose name equals tok1, as in N::C::~C, or inside a record scope whose name matches, which the injected-class-name loop below already handles. Please also add ~U() to simplifyUsing46.

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

Labels

ai-assisted contains code which was written with the assistance of AI - please review extra carefully

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants