[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
Mon Jun 30 07:32:04 PDT 2025
https://github.com/flovent updated https://github.com/llvm/llvm-project/pull/127394
>From d8d48e730d0192fe39cce72b47a62a929647484c 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 e50d40b76e8c4..88b2aff786667 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -327,6 +327,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 d280cb22c0f8c1b92f080bf35013b52079938051 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 58e87d182dcdf5699d49b518d99b0057add59eaf 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 88b2aff786667..606d6517829b4 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -212,6 +212,10 @@ Changes in existing checks
<clang-tidy/checks/cppcoreguidelines/avoid-goto>` check by adding the option
`IgnoreMacros` to ignore ``goto`` labels defined in macros.
+- 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-special-member-functions
<clang-tidy/checks/cppcoreguidelines/special-member-functions>` check by
adding the option `IgnoreMacros` to ignore classes defined in macros.
@@ -327,10 +331,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 51b3460c693429635c198dfc4f287f1bd1962a52 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 606d6517829b4..cd9dcd59ce003 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -214,7 +214,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-special-member-functions
<clang-tidy/checks/cppcoreguidelines/special-member-functions>` check by
>From 3aad049b38a7c19e21a1ce1fd46392dfb3345a23 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 2b8773f6262c4254f38e6e01341b434b6a64454b 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