about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-06-11 15:27:36 +0000
committerbors <bors@rust-lang.org>2024-06-11 15:27:36 +0000
commit9ddea51a7369344519dd5855f2e04294fdc613c4 (patch)
tree0fcc81b1fe4192e1fa7c7c93689859b44cee6fc5
parent87c895ad83729146d11f6ec90d746f6faf2e676f (diff)
parentf7515ae9058786a61ffa30529f30575065b73450 (diff)
downloadrust-9ddea51a7369344519dd5855f2e04294fdc613c4.tar.gz
rust-9ddea51a7369344519dd5855f2e04294fdc613c4.zip
Auto merge of #12912 - lochetti:fix_12824, r=dswij
Don't lint indexing_slicing lints on proc macros

This pr fixes https://github.com/rust-lang/rust-clippy/issues/12824

Even though the issue mentions the indexing case only, it was easy to apply the fix to the slicing case as well.

changelog: [`out_of_bounds_indexing`, `indexing_slicing`]: Don't lint on procedural macros.
-rw-r--r--clippy_lints/src/indexing_slicing.rs6
-rw-r--r--tests/ui/indexing_slicing_index.rs20
-rw-r--r--tests/ui/indexing_slicing_index.stderr32
-rw-r--r--tests/ui/indexing_slicing_slice.rs21
-rw-r--r--tests/ui/indexing_slicing_slice.stderr38
5 files changed, 80 insertions, 37 deletions
diff --git a/clippy_lints/src/indexing_slicing.rs b/clippy_lints/src/indexing_slicing.rs
index b13b4d145df..d54f2af65cd 100644
--- a/clippy_lints/src/indexing_slicing.rs
+++ b/clippy_lints/src/indexing_slicing.rs
@@ -2,8 +2,8 @@
 
 use clippy_utils::consts::{constant, Constant};
 use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
-use clippy_utils::higher;
 use clippy_utils::ty::{deref_chain, get_adt_inherent_method};
+use clippy_utils::{higher, is_from_proc_macro};
 use rustc_ast::ast::RangeLimits;
 use rustc_hir::{Expr, ExprKind};
 use rustc_lint::{LateContext, LateLintPass};
@@ -102,7 +102,9 @@ impl IndexingSlicing {
 
 impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
-        if self.suppress_restriction_lint_in_const && cx.tcx.hir().is_inside_const_context(expr.hir_id) {
+        if (self.suppress_restriction_lint_in_const && cx.tcx.hir().is_inside_const_context(expr.hir_id))
+            || is_from_proc_macro(cx, expr)
+        {
             return;
         }
 
diff --git a/tests/ui/indexing_slicing_index.rs b/tests/ui/indexing_slicing_index.rs
index 2e726141649..2af5fcc82a9 100644
--- a/tests/ui/indexing_slicing_index.rs
+++ b/tests/ui/indexing_slicing_index.rs
@@ -1,4 +1,5 @@
 //@compile-flags: -Zdeduplicate-diagnostics=yes
+//@aux-build: proc_macros.rs
 
 #![warn(clippy::indexing_slicing)]
 // We also check the out_of_bounds_indexing lint here, because it lints similar things and
@@ -11,6 +12,9 @@
     clippy::useless_vec
 )]
 
+extern crate proc_macros;
+use proc_macros::with_span;
+
 const ARR: [i32; 2] = [1, 2];
 const REF: &i32 = &ARR[idx()]; // This should be linted, since `suppress-restriction-lint-in-const` default is false.
 //~^ ERROR: indexing may panic
@@ -22,6 +26,22 @@ const fn idx4() -> usize {
     4
 }
 
+with_span!(
+    span
+
+    fn dont_lint_proc_macro_array() {
+        let x = [1, 2, 3, 4];
+        let index: usize = 1;
+        x[index];
+        x[10];
+
+        let x = vec![0; 5];
+        let index: usize = 1;
+        x[index];
+        x[10];
+    }
+);
+
 fn main() {
     let x = [1, 2, 3, 4];
     let index: usize = 1;
diff --git a/tests/ui/indexing_slicing_index.stderr b/tests/ui/indexing_slicing_index.stderr
index 386f91becf1..71677584d25 100644
--- a/tests/ui/indexing_slicing_index.stderr
+++ b/tests/ui/indexing_slicing_index.stderr
@@ -1,5 +1,5 @@
 error: indexing may panic
-  --> tests/ui/indexing_slicing_index.rs:15:20
+  --> tests/ui/indexing_slicing_index.rs:19:20
    |
 LL | const REF: &i32 = &ARR[idx()]; // This should be linted, since `suppress-restriction-lint-in-const` default is false.
    |                    ^^^^^^^^^^
@@ -10,19 +10,19 @@ LL | const REF: &i32 = &ARR[idx()]; // This should be linted, since `suppress-re
    = help: to override `-D warnings` add `#[allow(clippy::indexing_slicing)]`
 
 error[E0080]: evaluation of `main::{constant#3}` failed
-  --> tests/ui/indexing_slicing_index.rs:47:14
+  --> tests/ui/indexing_slicing_index.rs:67:14
    |
 LL |     const { &ARR[idx4()] };
    |              ^^^^^^^^^^^ index out of bounds: the length is 2 but the index is 4
 
 note: erroneous constant encountered
-  --> tests/ui/indexing_slicing_index.rs:47:5
+  --> tests/ui/indexing_slicing_index.rs:67:5
    |
 LL |     const { &ARR[idx4()] };
    |     ^^^^^^^^^^^^^^^^^^^^^^
 
 error: indexing may panic
-  --> tests/ui/indexing_slicing_index.rs:28:5
+  --> tests/ui/indexing_slicing_index.rs:48:5
    |
 LL |     x[index];
    |     ^^^^^^^^
@@ -30,7 +30,7 @@ LL |     x[index];
    = help: consider using `.get(n)` or `.get_mut(n)` instead
 
 error: index is out of bounds
-  --> tests/ui/indexing_slicing_index.rs:31:5
+  --> tests/ui/indexing_slicing_index.rs:51:5
    |
 LL |     x[4];
    |     ^^^^
@@ -39,13 +39,13 @@ LL |     x[4];
    = help: to override `-D warnings` add `#[allow(clippy::out_of_bounds_indexing)]`
 
 error: index is out of bounds
-  --> tests/ui/indexing_slicing_index.rs:33:5
+  --> tests/ui/indexing_slicing_index.rs:53:5
    |
 LL |     x[1 << 3];
    |     ^^^^^^^^^
 
 error: indexing may panic
-  --> tests/ui/indexing_slicing_index.rs:44:14
+  --> tests/ui/indexing_slicing_index.rs:64:14
    |
 LL |     const { &ARR[idx()] };
    |              ^^^^^^^^^^
@@ -54,7 +54,7 @@ LL |     const { &ARR[idx()] };
    = note: the suggestion might not be applicable in constant blocks
 
 error: indexing may panic
-  --> tests/ui/indexing_slicing_index.rs:47:14
+  --> tests/ui/indexing_slicing_index.rs:67:14
    |
 LL |     const { &ARR[idx4()] };
    |              ^^^^^^^^^^^
@@ -63,13 +63,13 @@ LL |     const { &ARR[idx4()] };
    = note: the suggestion might not be applicable in constant blocks
 
 error: index is out of bounds
-  --> tests/ui/indexing_slicing_index.rs:54:5
+  --> tests/ui/indexing_slicing_index.rs:74:5
    |
 LL |     y[4];
    |     ^^^^
 
 error: indexing may panic
-  --> tests/ui/indexing_slicing_index.rs:57:5
+  --> tests/ui/indexing_slicing_index.rs:77:5
    |
 LL |     v[0];
    |     ^^^^
@@ -77,7 +77,7 @@ LL |     v[0];
    = help: consider using `.get(n)` or `.get_mut(n)` instead
 
 error: indexing may panic
-  --> tests/ui/indexing_slicing_index.rs:59:5
+  --> tests/ui/indexing_slicing_index.rs:79:5
    |
 LL |     v[10];
    |     ^^^^^
@@ -85,7 +85,7 @@ LL |     v[10];
    = help: consider using `.get(n)` or `.get_mut(n)` instead
 
 error: indexing may panic
-  --> tests/ui/indexing_slicing_index.rs:61:5
+  --> tests/ui/indexing_slicing_index.rs:81:5
    |
 LL |     v[1 << 3];
    |     ^^^^^^^^^
@@ -93,13 +93,13 @@ LL |     v[1 << 3];
    = help: consider using `.get(n)` or `.get_mut(n)` instead
 
 error: index is out of bounds
-  --> tests/ui/indexing_slicing_index.rs:69:5
+  --> tests/ui/indexing_slicing_index.rs:89:5
    |
 LL |     x[N];
    |     ^^^^
 
 error: indexing may panic
-  --> tests/ui/indexing_slicing_index.rs:72:5
+  --> tests/ui/indexing_slicing_index.rs:92:5
    |
 LL |     v[N];
    |     ^^^^
@@ -107,7 +107,7 @@ LL |     v[N];
    = help: consider using `.get(n)` or `.get_mut(n)` instead
 
 error: indexing may panic
-  --> tests/ui/indexing_slicing_index.rs:74:5
+  --> tests/ui/indexing_slicing_index.rs:94:5
    |
 LL |     v[M];
    |     ^^^^
@@ -115,7 +115,7 @@ LL |     v[M];
    = help: consider using `.get(n)` or `.get_mut(n)` instead
 
 error: index is out of bounds
-  --> tests/ui/indexing_slicing_index.rs:78:13
+  --> tests/ui/indexing_slicing_index.rs:98:13
    |
 LL |     let _ = x[4];
    |             ^^^^
diff --git a/tests/ui/indexing_slicing_slice.rs b/tests/ui/indexing_slicing_slice.rs
index 69291acd9c7..f37bcc4aa0c 100644
--- a/tests/ui/indexing_slicing_slice.rs
+++ b/tests/ui/indexing_slicing_slice.rs
@@ -1,3 +1,5 @@
+//@aux-build: proc_macros.rs
+
 #![warn(clippy::indexing_slicing)]
 // We also check the out_of_bounds_indexing lint here, because it lints similar things and
 // we want to avoid false positives.
@@ -11,6 +13,9 @@
 )]
 #![warn(clippy::indexing_slicing)]
 
+extern crate proc_macros;
+use proc_macros::with_span;
+
 use std::ops::Index;
 
 struct BoolMap<T> {
@@ -86,6 +91,22 @@ impl<T> Index<i32> for Z<T> {
     }
 }
 
+with_span!(
+    span
+
+    fn dont_lint_proc_macro() {
+        let x = [1, 2, 3, 4];
+        let index: usize = 1;
+        &x[index..];
+        &x[..10];
+
+        let x = vec![0; 5];
+        let index: usize = 1;
+        &x[index..];
+        &x[..10];
+    }
+);
+
 fn main() {
     let x = [1, 2, 3, 4];
     let index: usize = 1;
diff --git a/tests/ui/indexing_slicing_slice.stderr b/tests/ui/indexing_slicing_slice.stderr
index a7da3fe3faa..1e72506746e 100644
--- a/tests/ui/indexing_slicing_slice.stderr
+++ b/tests/ui/indexing_slicing_slice.stderr
@@ -1,5 +1,5 @@
 error: slicing may panic
-  --> tests/ui/indexing_slicing_slice.rs:94:6
+  --> tests/ui/indexing_slicing_slice.rs:115:6
    |
 LL |     &x[index..];
    |      ^^^^^^^^^^
@@ -9,7 +9,7 @@ LL |     &x[index..];
    = help: to override `-D warnings` add `#[allow(clippy::indexing_slicing)]`
 
 error: slicing may panic
-  --> tests/ui/indexing_slicing_slice.rs:96:6
+  --> tests/ui/indexing_slicing_slice.rs:117:6
    |
 LL |     &x[..index];
    |      ^^^^^^^^^^
@@ -17,7 +17,7 @@ LL |     &x[..index];
    = help: consider using `.get(..n)`or `.get_mut(..n)` instead
 
 error: slicing may panic
-  --> tests/ui/indexing_slicing_slice.rs:98:6
+  --> tests/ui/indexing_slicing_slice.rs:119:6
    |
 LL |     &x[index_from..index_to];
    |      ^^^^^^^^^^^^^^^^^^^^^^^
@@ -25,7 +25,7 @@ LL |     &x[index_from..index_to];
    = help: consider using `.get(n..m)` or `.get_mut(n..m)` instead
 
 error: slicing may panic
-  --> tests/ui/indexing_slicing_slice.rs:100:6
+  --> tests/ui/indexing_slicing_slice.rs:121:6
    |
 LL |     &x[index_from..][..index_to];
    |      ^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -33,7 +33,7 @@ LL |     &x[index_from..][..index_to];
    = help: consider using `.get(..n)`or `.get_mut(..n)` instead
 
 error: slicing may panic
-  --> tests/ui/indexing_slicing_slice.rs:100:6
+  --> tests/ui/indexing_slicing_slice.rs:121:6
    |
 LL |     &x[index_from..][..index_to];
    |      ^^^^^^^^^^^^^^^
@@ -41,7 +41,7 @@ LL |     &x[index_from..][..index_to];
    = help: consider using `.get(n..)` or .get_mut(n..)` instead
 
 error: slicing may panic
-  --> tests/ui/indexing_slicing_slice.rs:103:6
+  --> tests/ui/indexing_slicing_slice.rs:124:6
    |
 LL |     &x[5..][..10];
    |      ^^^^^^^^^^^^
@@ -49,7 +49,7 @@ LL |     &x[5..][..10];
    = help: consider using `.get(..n)`or `.get_mut(..n)` instead
 
 error: range is out of bounds
-  --> tests/ui/indexing_slicing_slice.rs:103:8
+  --> tests/ui/indexing_slicing_slice.rs:124:8
    |
 LL |     &x[5..][..10];
    |        ^
@@ -58,7 +58,7 @@ LL |     &x[5..][..10];
    = help: to override `-D warnings` add `#[allow(clippy::out_of_bounds_indexing)]`
 
 error: slicing may panic
-  --> tests/ui/indexing_slicing_slice.rs:107:6
+  --> tests/ui/indexing_slicing_slice.rs:128:6
    |
 LL |     &x[0..][..3];
    |      ^^^^^^^^^^^
@@ -66,7 +66,7 @@ LL |     &x[0..][..3];
    = help: consider using `.get(..n)`or `.get_mut(..n)` instead
 
 error: slicing may panic
-  --> tests/ui/indexing_slicing_slice.rs:109:6
+  --> tests/ui/indexing_slicing_slice.rs:130:6
    |
 LL |     &x[1..][..5];
    |      ^^^^^^^^^^^
@@ -74,19 +74,19 @@ LL |     &x[1..][..5];
    = help: consider using `.get(..n)`or `.get_mut(..n)` instead
 
 error: range is out of bounds
-  --> tests/ui/indexing_slicing_slice.rs:117:12
+  --> tests/ui/indexing_slicing_slice.rs:138:12
    |
 LL |     &y[0..=4];
    |            ^
 
 error: range is out of bounds
-  --> tests/ui/indexing_slicing_slice.rs:119:11
+  --> tests/ui/indexing_slicing_slice.rs:140:11
    |
 LL |     &y[..=4];
    |           ^
 
 error: slicing may panic
-  --> tests/ui/indexing_slicing_slice.rs:125:6
+  --> tests/ui/indexing_slicing_slice.rs:146:6
    |
 LL |     &v[10..100];
    |      ^^^^^^^^^^
@@ -94,7 +94,7 @@ LL |     &v[10..100];
    = help: consider using `.get(n..m)` or `.get_mut(n..m)` instead
 
 error: slicing may panic
-  --> tests/ui/indexing_slicing_slice.rs:127:6
+  --> tests/ui/indexing_slicing_slice.rs:148:6
    |
 LL |     &x[10..][..100];
    |      ^^^^^^^^^^^^^^
@@ -102,13 +102,13 @@ LL |     &x[10..][..100];
    = help: consider using `.get(..n)`or `.get_mut(..n)` instead
 
 error: range is out of bounds
-  --> tests/ui/indexing_slicing_slice.rs:127:8
+  --> tests/ui/indexing_slicing_slice.rs:148:8
    |
 LL |     &x[10..][..100];
    |        ^^
 
 error: slicing may panic
-  --> tests/ui/indexing_slicing_slice.rs:130:6
+  --> tests/ui/indexing_slicing_slice.rs:151:6
    |
 LL |     &v[10..];
    |      ^^^^^^^
@@ -116,7 +116,7 @@ LL |     &v[10..];
    = help: consider using `.get(n..)` or .get_mut(n..)` instead
 
 error: slicing may panic
-  --> tests/ui/indexing_slicing_slice.rs:132:6
+  --> tests/ui/indexing_slicing_slice.rs:153:6
    |
 LL |     &v[..100];
    |      ^^^^^^^^
@@ -124,7 +124,7 @@ LL |     &v[..100];
    = help: consider using `.get(..n)`or `.get_mut(..n)` instead
 
 error: indexing may panic
-  --> tests/ui/indexing_slicing_slice.rs:150:5
+  --> tests/ui/indexing_slicing_slice.rs:171:5
    |
 LL |     map_with_get[true];
    |     ^^^^^^^^^^^^^^^^^^
@@ -132,7 +132,7 @@ LL |     map_with_get[true];
    = help: consider using `.get(n)` or `.get_mut(n)` instead
 
 error: indexing may panic
-  --> tests/ui/indexing_slicing_slice.rs:153:5
+  --> tests/ui/indexing_slicing_slice.rs:174:5
    |
 LL |     s[0];
    |     ^^^^
@@ -140,7 +140,7 @@ LL |     s[0];
    = help: consider using `.get(n)` or `.get_mut(n)` instead
 
 error: indexing may panic
-  --> tests/ui/indexing_slicing_slice.rs:156:5
+  --> tests/ui/indexing_slicing_slice.rs:177:5
    |
 LL |     y[0];
    |     ^^^^