[clang-tidy] Fix false positive in cert-ctr56-cpp (PointerArithmeticOnPolymorphicObjectCheck) (#187452)

## Summary
This change adds `unless(isInstantiationDependent())` to the
`ArraySubscript` matcher.

## Motivation
In template code, some array subscript expressions are not fully
resolved yet.
Because of this, the matcher may treat them incorrectly and produce
false positives.

## Change
This patch skips instantiation-dependent expressions in templates. 
It helps the matcher work only on resolved expressions.

Closes https://github.com/llvm/llvm-project/issues/187009.
This commit is contained in:
LeeYoungJoon 2026-03-24 10:12:35 +09:00 committed by GitHub
parent 79ae4c7419
commit d734ead132
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 19 additions and 1 deletions

View File

@ -53,7 +53,9 @@ void PointerArithmeticOnPolymorphicObjectCheck::registerMatchers(
? PointerExprWithVirtualMethod
: PolymorphicPointerExpr;
const auto ArraySubscript = arraySubscriptExpr(hasBase(SelectedPointerExpr));
const auto ArraySubscript =
expr(arraySubscriptExpr(hasBase(SelectedPointerExpr)),
unless(isInstantiationDependent()));
const auto BinaryOperators =
binaryOperator(hasAnyOperatorName("+", "-", "+=", "-="),

View File

@ -223,6 +223,10 @@ Changes in existing checks
<clang-tidy/checks/bugprone/macro-parentheses>` check by printing the macro
definition in the warning message if the macro is defined on command line.
- Improved :doc:`bugprone-pointer-arithmetic-on-polymorphic-object
<clang-tidy/checks/bugprone/pointer-arithmetic-on-polymorphic-object>` check
by fixing a false positive when ``operator[]`` is used in a dependent context.
- Improved :doc:`bugprone-std-namespace-modification
<clang-tidy/checks/bugprone/std-namespace-modification>` check by fixing
false positives when extending the standard library with a specialization of

View File

@ -1,5 +1,7 @@
// RUN: %check_clang_tidy %s bugprone-pointer-arithmetic-on-polymorphic-object %t --
#include <map>
class Base {
public:
virtual ~Base() {}
@ -138,3 +140,13 @@ void typeAliases(BaseAlias *b, DerivedAlias *d, FinalDerivedAlias *fd,
fdp += 1;
// no-warning
}
template <typename T>
struct TemplateHolder : Base {
std::map<Base *, T> _map;
void test() {
auto &x = _map[this];
// no-warning
(void)x;
}
};