Author: flovent Date: 2025-07-02T17:41:24+03:00 New Revision: 5a8d096ae3443909bbcc87465653c15b58bc2e11
URL: https://github.com/llvm/llvm-project/commit/5a8d096ae3443909bbcc87465653c15b58bc2e11 DIFF: https://github.com/llvm/llvm-project/commit/5a8d096ae3443909bbcc87465653c15b58bc2e11.diff LOG: [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 <typename R> int f(std::map<R*, int>& map, R* r) { return map[r]; } ``` so is needed to see if index is integer type to avoid this situation. Added: Modified: clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsPointerArithmeticCheck.cpp clang-tools-extra/docs/ReleaseNotes.rst clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-pointer-arithmetic.cpp Removed: ################################################################################ diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsPointerArithmeticCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsPointerArithmeticCheck.cpp index a1494a095f5b6..0d68790349fb5 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 8c331e0b0a403..198efee7754de 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -216,6 +216,11 @@ Changes in existing checks <clang-tidy/checks/cppcoreguidelines/missing-std-forward>` check by adding a flag to specify the function used for forwarding instead of ``std::forward``. +- Improved :doc:`cppcoreguidelines-pro-bounds-pointer-arithmetic + <clang-tidy/checks/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 <clang-tidy/checks/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 7cbc6ddf96ab6..7f69eddee03aa 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 <typename, typename> +class pair {}; + +template <typename Key, typename Value> +class map { + public: + using value_type = pair<Key, Value>; + value_type& operator[](const Key& key); + value_type& operator[](Key&& key); + }; +} + +template <typename R> +int f(std::map<R*, int>& map, R* r) { + return map[r]; // OK +} + +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits