Repository navigation
Fix #15269 FP shadowFunction: deduction guide is treated as a function - #9071
autoantwort wants to merge 1 commit into
Conversation
dmcppcheck
left a comment
There was a problem hiding this comment.
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
cppcheckandtestrunnerfrom pr/, andcppcheckfrom 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: noshadowFunction(fixed). - Real functions
[[nodiscard]] auto X(T) -> X<T>andfriend auto X(T) -> ::X<T>: still found as functions, as they should be. - A guide with a
requiresclause: handled.
- The guide in a namespace with an instantiation of
- 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.
|
|
||
| // 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%")) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.)
|
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/ 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>
|
@danmar Changes to |
1d131a1 to
61c87d2
Compare
dmcppcheck
left a comment
There was a problem hiding this comment.
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
cppcheckandtestrunnerfrom pr/, andcppcheckfrom 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
requiresclause,explicit(sizeof(E) > 2)guides, a class-scope guide afterpublic:, guides inside a namespace and non-template guides. TheshadowFunctionfalse 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, andtemplate<class T> auto Z(T t) -> Z<T> { ... }. The PR still reportsshadowFunctionfor local variables that shadow them, the same as base, so these are still treated as functions.
- Guides: a guide with a
- None of these tests produced a wrong result.
Automated review of 61c87d2 by Claude. The pull request was built and tested locally.
|
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/ 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. |
Trac: https://trac.cppcheck.net/ticket/15269
Cause:
SymbolDatabase::isFunction()accepted the deduction guide (unexpected ( E ) . unexpected < E > ;after tokenizing) as a function declaration with a trailing return type, takingtemplateas its return type. In class scope, where any token may precede a function name, non-template guides were accepted too. The enclosing scope got aFunctionnamed like the class template, which the shadow check found.Fix: a function with a trailing return type has
autoor 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 tokenS < .. >), is a deduction guide and not a function.Limitation: a real function with an unknown macro between
autoand 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,
testrunnerpasses. 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