Skip to content

Fix #15269 FP shadowFunction: deduction guide is treated as a function - #9071

Open
autoantwort wants to merge 1 commit into
cppcheck-opensource:mainfrom
autoantwort:fix-shadow-deduction-guide
Open

autoantwort wants to merge 1 commit into
cppcheck-opensource:mainfrom
autoantwort:fix-shadow-deduction-guide

Conversation

@autoantwort

@autoantwort autoantwort commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Trac: https://trac.cppcheck.net/ticket/15269

template <class E>
struct unexpected {
    explicit unexpected(E e) : err(e) {}
    E err;
};

template <class E>
unexpected(E) -> unexpected<E>;

int f(int x) {
    const int unexpected = x + 1;
    return unexpected;
}
test.cpp:11:15: style: Local variable 'unexpected' shadows outer function [shadowFunction]

Cause: SymbolDatabase::isFunction() accepted the deduction guide (unexpected ( E ) . unexpected < E > ; after tokenizing) as a function declaration with a trailing return type, taking template as its return type. In class scope, where any token may precede a function name, non-template guides were accepted too. The enclosing scope got a Function named like the class template, which the shadow check found.

Fix: a function with a trailing return type has auto or a qualification directly before its name (after the tokenizer removed calling conventions and attributes). A declaration without it whose trailing type is the same template name, optionally qualified (S < .., ::S < .., O :: S < .., or the instantiated single token S < .. >), is a deduction guide and not a function.

Limitation: a real function with an unknown macro between auto and its name whose trailing type is a same-named class template of an outer scope (namespace n { auto MACRO X(int) -> X<int>; }) is no longer a function.

Verification: new tests fail without the fix, testrunner passes. The test/cfg and samples files give identical results. On a large internal code base the only change vs main is 9 removed shadowFunction FPs (from two deduction guides).

This PR was created by Claude Code (an AI coding agent) on behalf of @autoantwort.

🤖 Generated with Claude Code

@autoantwort
autoantwort marked this pull request as ready for review November 25, 2026 09:29

@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 reported case and for unqualified variants: namespace scope, class scope, explicit, non-template guides, requires-clauses and instantiated templates. It misses one case. If the guide's trailing type is qualified (-> ::U<E> or, in class scope, -> O::U<E>), it is still registered as a function and the shadowFunction false positive remains. GCC and Clang accept such guides. This is the same result as on base, so it is not a regression. I found no other problems.

Comments

# Comment By Where Status
1 Deduction guide with qualified trailing type (-> ::U<E>, -> O::U<E>) still treated as a function AI review lib/symboldatabase.cpp:2021 🆕 New (false negative)

@autoantwort, please fix, resolve or reply to the open and new comments: #1.
For #1 (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

  • Built cppcheck and testrunner from pr/, and cppcheck from base/.
  • ./testrunner TestSymbolDatabase TestOther: all tests pass.
  • Ran scratch files through the cppcheck of the PR and of base:
    • The guide in a namespace with an instantiation of n::unexpected<int>, plus a non-template guide: no shadowFunction (fixed).
    • Real functions [[nodiscard]] auto X(T) -> X<T> and friend auto X(T) -> ::X<T>: still found as functions, as they should be.
    • A guide with a requires clause: handled.
  • The false positive remains when the trailing type is qualified (the same as base):
// f.cpp  (g++ -std=c++20 -fsyntax-only f.cpp accepts it)
template <class E> struct U { U(E){} };
template <class E> U(E*) -> ::U<E>;
int f(int x) {
    const int U = x + 1;
    return U;
}

./cppcheck -q --std=c++20 --enable=style f.cpp

f.cpp:5:15: style: Local variable 'U' shadows outer function [shadowFunction]
f.cpp:2:20: note: Shadowed function

The same happens in class scope with unexpected(E*) -> O::unexpected<E>;.


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

Comment thread lib/symboldatabase.cpp Outdated

// deduction guide 'S ( .. ) -> S < .. > ;': a function with a trailing return type has 'auto' or a
// qualification directly before its name (the tokenizer removed calling conventions and attributes)
if (!Token::Match(tok1, "auto|::") && Token::Match(tok2, ". %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

The check only looks at the token directly after ->. A guide whose trailing type is qualified, such as template <class E> U(E*) -> ::U<E>; or unexpected(E*) -> O::unexpected<E>; inside struct O, is still registered as a function. GCC and Clang accept these. A local variable U then still triggers shadowFunction, as I reproduced. Skipping leading :: and %name% :: qualifiers before comparing the name would cover this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Confirmed, -> ::U<E> and -> O::U<E> (class scope) still warned. Fixed: the qualifiers before the trailing type's name are skipped now, and I added tests for both forms.

(Written by Claude Code on behalf of @autoantwort.)

@dmcppcheck

Copy link
Copy Markdown
Contributor

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

Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9071/
packages: 100
Differing warnings: 0 (+0 -0)
Timing: -0.1% (http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9071/2026-11-25_09-27-36-1d131a150faf_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.

SymbolDatabase::isFunction() accepted a deduction guide like
'template <class T> S(T) -> S<T>;' as a function declaration with a
trailing return type (taking 'template' as its return type), and also
non-template guides in class scope. A local variable named like the
class template then shadowed this function.

A function with a trailing return type has 'auto' or a qualification
before its name, so a declaration without it whose trailing type names
the same template is a deduction guide.

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

Copy link
Copy Markdown
Collaborator

@danmar Changes to symboldatabase.cpp and astutils.cpp should probably also trigger a 1000 package run. What do you think?

@autoantwort
autoantwort force-pushed the fix-shadow-deduction-guide branch from 1d131a1 to 61c87d2 Compare November 26, 2026 15:33

@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

I found no new problems. The earlier finding is fixed: deduction guides with a qualified trailing type (-> ::U<E>, -> O::U<E>) are no longer registered as functions, and the PR has tests for both forms. Real functions with a trailing return type that names a same-named template (auto X(int) -> ::X<int>, static auto, and template functions with a body) are still found as functions. Their shadowFunction results are the same as on base.

Comments

# Comment By Where Status
1 Deduction guide with qualified trailing type (-> ::U, -> O::U) still treated as a function AI review lib/symboldatabase.cpp:2021 ✅ Fixed

Testing

  • I built cppcheck and testrunner from pr/, and cppcheck from base/. Both builds succeeded.
  • ./testrunner TestSymbolDatabase TestOther TestTokenizer TestSimplifyTemplate TestConstructors TestClass TestFunctions TestVarID: 0 failures.
  • I ran scratch files through the cppcheck of the PR and of base with --std=c++20 --enable=style:
    • Guides: a guide with a requires clause, explicit(sizeof(E) > 2) guides, a class-scope guide after public:, guides inside a namespace and non-template guides. The shadowFunction false positives that base reports for these are gone, and no new warnings appear.
    • Real functions: static auto X(int) -> ::X<int>; in a class, auto X(int) -> ::X<int>; at namespace scope, and template<class T> auto Z(T t) -> Z<T> { ... }. The PR still reports shadowFunction for local variables that shadow them, the same as base, so these are still treated as functions.
  • None of these tests produced a wrong result.

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

@dmcppcheck

Copy link
Copy Markdown
Contributor

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

Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9071/
packages: 100
Differing warnings: 0 (+0 -0)
Timing: +0.3% (http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9071/2026-11-25_17-33-03-61c87d2c0ffd_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.

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