diff --git a/clang-tools-extra/clang-tidy/bugprone/PointerArithmeticOnPolymorphicObjectCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/PointerArithmeticOnPolymorphicObjectCheck.cpp index c21abad94791..ef5fee9f3d2c 100644 --- a/clang-tools-extra/clang-tidy/bugprone/PointerArithmeticOnPolymorphicObjectCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/PointerArithmeticOnPolymorphicObjectCheck.cpp @@ -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("+", "-", "+=", "-="), diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index a346fee4ea33..343f216ff13b 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -223,6 +223,10 @@ Changes in existing checks ` 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 + ` check + by fixing a false positive when ``operator[]`` is used in a dependent context. + - Improved :doc:`bugprone-std-namespace-modification ` check by fixing false positives when extending the standard library with a specialization of diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/pointer-arithmetic-on-polymorphic-object-all.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/pointer-arithmetic-on-polymorphic-object-all.cpp index caf24f79ad2d..f8163a2fd3fb 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/pointer-arithmetic-on-polymorphic-object-all.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/pointer-arithmetic-on-polymorphic-object-all.cpp @@ -1,5 +1,7 @@ // RUN: %check_clang_tidy %s bugprone-pointer-arithmetic-on-polymorphic-object %t -- +#include + class Base { public: virtual ~Base() {} @@ -138,3 +140,13 @@ void typeAliases(BaseAlias *b, DerivedAlias *d, FinalDerivedAlias *fd, fdp += 1; // no-warning } + +template +struct TemplateHolder : Base { + std::map _map; + void test() { + auto &x = _map[this]; + // no-warning + (void)x; + } +};