[clang-tools-extra] [clang-tidy] Fix false positive for cppcoreguidelines-pro-bounds-pointer-arithmetic (PR #127394)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 2 07:07:19 PDT 2025
https://github.com/flovent updated https://github.com/llvm/llvm-project/pull/127394
>From 98738596d122c71f5d1dbd7efa88f58292b7a8e6 Mon Sep 17 00:00:00 2001
From: flovent <flbven at protonmail.com>
Date: Sun, 16 Feb 2025 21:07:55 +0800
Subject: [PATCH 1/6] [clang-tidy] Fix false positive for
cppcoreguidelines-pro-bounds-pointer-arithmetic
---
.../ProBoundsPointerArithmeticCheck.cpp | 3 ++-
clang-tools-extra/docs/ReleaseNotes.rst | 4 ++++
...-bounds-pointer-arithmetic-issue126424.cpp | 19 +++++++++++++++++++
3 files changed, 25 insertions(+), 1 deletion(-)
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-pointer-arithmetic-issue126424.cpp
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..a0ade22d3dd61 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -336,6 +336,10 @@ Changes in existing checks
<clang-tidy/checks/readability/redundant-smartptr-get>` check by fixing
some false positives involving smart pointers to arrays.
+- Improved :doc:`cppcoreguidelines-pro-bounds-pointer-arithmetic
+ <clang-tidy/checks/cppcoreguidelines/pro-bounds-pointer-arithmetic>` check by
+ fix false positives related to operator overloading and templates.
+
Removed checks
^^^^^^^^^^^^^^
diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-pointer-arithmetic-issue126424.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-pointer-arithmetic-issue126424.cpp
new file mode 100644
index 0000000000000..b6b7a9664f38d
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-pointer-arithmetic-issue126424.cpp
@@ -0,0 +1,19 @@
+// RUN: %check_clang_tidy %s cppcoreguidelines-pro-bounds-pointer-arithmetic %t
+
+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
+}
\ No newline at end of file
>From e42a812c92303262933ad898c5dc44cc3fa4a431 Mon Sep 17 00:00:00 2001
From: flovent <flbven at protonmail.com>
Date: Sun, 16 Feb 2025 21:18:45 +0800
Subject: [PATCH 2/6] add missing new line for testcase
---
.../pro-bounds-pointer-arithmetic-issue126424.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-pointer-arithmetic-issue126424.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-pointer-arithmetic-issue126424.cpp
index b6b7a9664f38d..a76798423ee17 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-pointer-arithmetic-issue126424.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-pointer-arithmetic-issue126424.cpp
@@ -16,4 +16,4 @@ class map {
template <typename R>
int f(std::map<R*, int>& map, R* r) {
return map[r]; // OK
-}
\ No newline at end of file
+}
>From 7446a4181e5aeed22b048956ea9c27832e670e9b Mon Sep 17 00:00:00 2001
From: flovent <flbven at protomail.com>
Date: Mon, 17 Feb 2025 20:11:06 +0800
Subject: [PATCH 3/6] keep alpha order for release note
---
clang-tools-extra/docs/ReleaseNotes.rst | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index a0ade22d3dd61..e5054da553ff7 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -216,6 +216,10 @@ 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
+ fix false positives related to operator overloading and templates.
+
- 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
@@ -336,10 +340,6 @@ Changes in existing checks
<clang-tidy/checks/readability/redundant-smartptr-get>` check by fixing
some false positives involving smart pointers to arrays.
-- Improved :doc:`cppcoreguidelines-pro-bounds-pointer-arithmetic
- <clang-tidy/checks/cppcoreguidelines/pro-bounds-pointer-arithmetic>` check by
- fix false positives related to operator overloading and templates.
-
Removed checks
^^^^^^^^^^^^^^
>From e006c0d7d9204dc196275b30d2b4b26fde99b6e2 Mon Sep 17 00:00:00 2001
From: flovent <flbven at protonmail.com>
Date: Sat, 24 May 2025 18:13:09 +0800
Subject: [PATCH 4/6] optimize release notes
---
clang-tools-extra/docs/ReleaseNotes.rst | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index e5054da553ff7..198efee7754de 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -218,7 +218,8 @@ Changes in existing checks
- Improved :doc:`cppcoreguidelines-pro-bounds-pointer-arithmetic
<clang-tidy/checks/cppcoreguidelines/pro-bounds-pointer-arithmetic>` check by
- fix false positives related to operator overloading and templates.
+ 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
>From 98cbea59b0e33794378e1f5f2a6fb12a11d44762 Mon Sep 17 00:00:00 2001
From: flovent <flbven at protonmail.com>
Date: Mon, 30 Jun 2025 20:36:05 +0800
Subject: [PATCH 5/6] merge testcase to main test file
---
...-bounds-pointer-arithmetic-issue126424.cpp | 19 ----------------
.../pro-bounds-pointer-arithmetic.cpp | 22 +++++++++++++++++++
2 files changed, 22 insertions(+), 19 deletions(-)
delete mode 100644 clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-pointer-arithmetic-issue126424.cpp
diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-pointer-arithmetic-issue126424.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-pointer-arithmetic-issue126424.cpp
deleted file mode 100644
index a76798423ee17..0000000000000
--- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-pointer-arithmetic-issue126424.cpp
+++ /dev/null
@@ -1,19 +0,0 @@
-// RUN: %check_clang_tidy %s cppcoreguidelines-pro-bounds-pointer-arithmetic %t
-
-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
-}
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..5650dc8240b83 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 issue126424 {
+
+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
+}
+
+}
>From a91f5b6f49104f06e0ca9769dce4573f69c1cd53 Mon Sep 17 00:00:00 2001
From: flovent <flbven at protonmail.com>
Date: Mon, 30 Jun 2025 22:31:48 +0800
Subject: [PATCH 6/6] `issue` to `gh`
---
.../cppcoreguidelines/pro-bounds-pointer-arithmetic.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
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 5650dc8240b83..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
@@ -88,7 +88,7 @@ void okay() {
for(int ii : a) ; // OK, pointer arithmetic generated by compiler
}
-namespace issue126424 {
+namespace gh126424 {
namespace std {
template <typename, typename>
More information about the cfe-commits
mailing list