From 5a8d096ae3443909bbcc87465653c15b58bc2e11 Mon Sep 17 00:00:00 2001 From: flovent Date: Wed, 2 Jul 2025 22:41:24 +0800 Subject: [PATCH] [clang-tidy] Fix false positive for cppcoreguidelines-pro-bounds-pointer-arithmetic (#127394) this PR fixes #126424 for `ArraySubScriptExpr`, `hasBase` Matcher will get right operand when it is not integer type, but is not for sure that left operand is integer type. For the example code below `hasBase` will get `r` for the Subsequent matching and causing false positive. ``` template int f(std::map& map, R* r) { return map[r]; } ``` so is needed to see if index is integer type to avoid this situation. --- .../ProBoundsPointerArithmeticCheck.cpp | 3 ++- clang-tools-extra/docs/ReleaseNotes.rst | 5 +++++ .../pro-bounds-pointer-arithmetic.cpp | 22 +++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsPointerArithmeticCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsPointerArithmeticCheck.cpp index a1494a095f5b..0d68790349fb 100644 --- a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsPointerArithmeticCheck.cpp +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsPointerArithmeticCheck.cpp @@ -42,7 +42,8 @@ void ProBoundsPointerArithmeticCheck::registerMatchers(MatchFinder *Finder) { arraySubscriptExpr( hasBase(ignoringImpCasts( anyOf(AllPointerTypes, - hasType(decayedType(hasDecayedType(pointerType()))))))) + hasType(decayedType(hasDecayedType(pointerType())))))), + hasIndex(hasType(isInteger()))) .bind("expr"), this); } diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 8c331e0b0a40..198efee7754d 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -216,6 +216,11 @@ Changes in existing checks ` check by adding a flag to specify the function used for forwarding instead of ``std::forward``. +- Improved :doc:`cppcoreguidelines-pro-bounds-pointer-arithmetic + ` check by + fixing false positives when calling indexing operators that do not perform + pointer arithmetic in template, for example ``std::map::operator[]``. + - Improved :doc:`cppcoreguidelines-rvalue-reference-param-not-moved ` check by adding a flag to specify the function used for moving instead of diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-pointer-arithmetic.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-pointer-arithmetic.cpp index 7cbc6ddf96ab..7f69eddee03a 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-pointer-arithmetic.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-pointer-arithmetic.cpp @@ -87,3 +87,25 @@ void okay() { for(int ii : a) ; // OK, pointer arithmetic generated by compiler } + +namespace gh126424 { + +namespace std { +template +class pair {}; + +template +class map { + public: + using value_type = pair; + value_type& operator[](const Key& key); + value_type& operator[](Key&& key); + }; +} + +template +int f(std::map& map, R* r) { + return map[r]; // OK +} + +}