Skip to content

Commit 1b667aa

Browse files
committed
Don't lint indexing_slicing lints on proc macros
1 parent 336046c commit 1b667aa

5 files changed

+90
-36
lines changed

clippy_lints/src/indexing_slicing.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
33
use clippy_utils::consts::{constant, Constant};
44
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
5-
use clippy_utils::higher;
65
use clippy_utils::ty::{deref_chain, get_adt_inherent_method};
6+
use clippy_utils::{higher, is_from_proc_macro};
77
use rustc_ast::ast::RangeLimits;
88
use rustc_hir::{Expr, ExprKind};
99
use rustc_lint::{LateContext, LateLintPass};
@@ -126,6 +126,10 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
126126
return;
127127
};
128128

129+
if is_from_proc_macro(cx, expr) {
130+
return;
131+
}
132+
129133
let const_range = to_const_range(cx, range, size);
130134

131135
if let (Some(start), _) = const_range {
@@ -166,6 +170,10 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
166170
(None, None) => return, // [..] is ok.
167171
};
168172

173+
if is_from_proc_macro(cx, expr) {
174+
return;
175+
}
176+
169177
span_lint_and_then(cx, INDEXING_SLICING, expr.span, "slicing may panic", |diag| {
170178
diag.help(help_msg);
171179

@@ -190,6 +198,7 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
190198
&& *utype == ty::UintTy::Usize
191199
&& let ty::Array(_, s) = ty.kind()
192200
&& let Some(size) = s.try_eval_target_usize(cx.tcx, cx.param_env)
201+
&& !is_from_proc_macro(cx, expr)
193202
{
194203
// get constant offset and check whether it is in bounds
195204
let off = usize::try_from(off).unwrap();
@@ -204,6 +213,10 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
204213
}
205214
}
206215

216+
if is_from_proc_macro(cx, expr) {
217+
return;
218+
}
219+
207220
span_lint_and_then(cx, INDEXING_SLICING, expr.span, "indexing may panic", |diag| {
208221
diag.help("consider using `.get(n)` or `.get_mut(n)` instead");
209222

tests/ui/indexing_slicing_index.rs

+20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//@compile-flags: -Zdeduplicate-diagnostics=yes
2+
//@aux-build: proc_macros.rs
23

34
#![warn(clippy::indexing_slicing)]
45
// We also check the out_of_bounds_indexing lint here, because it lints similar things and
@@ -11,6 +12,9 @@
1112
clippy::useless_vec
1213
)]
1314

15+
extern crate proc_macros;
16+
use proc_macros::with_span;
17+
1418
const ARR: [i32; 2] = [1, 2];
1519
const REF: &i32 = &ARR[idx()]; // This should be linted, since `suppress-restriction-lint-in-const` default is false.
1620
//~^ ERROR: indexing may panic
@@ -22,6 +26,22 @@ const fn idx4() -> usize {
2226
4
2327
}
2428

29+
with_span!(
30+
span
31+
32+
fn dont_lint_proc_macro_array() {
33+
let x = [1, 2, 3, 4];
34+
let index: usize = 1;
35+
x[index];
36+
x[10];
37+
38+
let x = vec![0; 5];
39+
let index: usize = 1;
40+
x[index];
41+
x[10];
42+
}
43+
);
44+
2545
fn main() {
2646
let x = [1, 2, 3, 4];
2747
let index: usize = 1;

tests/ui/indexing_slicing_index.stderr

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: indexing may panic
2-
--> tests/ui/indexing_slicing_index.rs:15:20
2+
--> tests/ui/indexing_slicing_index.rs:19:20
33
|
44
LL | const REF: &i32 = &ARR[idx()]; // This should be linted, since `suppress-restriction-lint-in-const` default is false.
55
| ^^^^^^^^^^
@@ -10,27 +10,27 @@ LL | const REF: &i32 = &ARR[idx()]; // This should be linted, since `suppress-re
1010
= help: to override `-D warnings` add `#[allow(clippy::indexing_slicing)]`
1111

1212
error[E0080]: evaluation of `main::{constant#3}` failed
13-
--> tests/ui/indexing_slicing_index.rs:47:14
13+
--> tests/ui/indexing_slicing_index.rs:67:14
1414
|
1515
LL | const { &ARR[idx4()] };
1616
| ^^^^^^^^^^^ index out of bounds: the length is 2 but the index is 4
1717

1818
note: erroneous constant encountered
19-
--> tests/ui/indexing_slicing_index.rs:47:5
19+
--> tests/ui/indexing_slicing_index.rs:67:5
2020
|
2121
LL | const { &ARR[idx4()] };
2222
| ^^^^^^^^^^^^^^^^^^^^^^
2323

2424
error: indexing may panic
25-
--> tests/ui/indexing_slicing_index.rs:28:5
25+
--> tests/ui/indexing_slicing_index.rs:48:5
2626
|
2727
LL | x[index];
2828
| ^^^^^^^^
2929
|
3030
= help: consider using `.get(n)` or `.get_mut(n)` instead
3131

3232
error: index is out of bounds
33-
--> tests/ui/indexing_slicing_index.rs:31:5
33+
--> tests/ui/indexing_slicing_index.rs:51:5
3434
|
3535
LL | x[4];
3636
| ^^^^
@@ -39,13 +39,13 @@ LL | x[4];
3939
= help: to override `-D warnings` add `#[allow(clippy::out_of_bounds_indexing)]`
4040

4141
error: index is out of bounds
42-
--> tests/ui/indexing_slicing_index.rs:33:5
42+
--> tests/ui/indexing_slicing_index.rs:53:5
4343
|
4444
LL | x[1 << 3];
4545
| ^^^^^^^^^
4646

4747
error: indexing may panic
48-
--> tests/ui/indexing_slicing_index.rs:44:14
48+
--> tests/ui/indexing_slicing_index.rs:64:14
4949
|
5050
LL | const { &ARR[idx()] };
5151
| ^^^^^^^^^^
@@ -54,7 +54,7 @@ LL | const { &ARR[idx()] };
5454
= note: the suggestion might not be applicable in constant blocks
5555

5656
error: indexing may panic
57-
--> tests/ui/indexing_slicing_index.rs:47:14
57+
--> tests/ui/indexing_slicing_index.rs:67:14
5858
|
5959
LL | const { &ARR[idx4()] };
6060
| ^^^^^^^^^^^
@@ -63,59 +63,59 @@ LL | const { &ARR[idx4()] };
6363
= note: the suggestion might not be applicable in constant blocks
6464

6565
error: index is out of bounds
66-
--> tests/ui/indexing_slicing_index.rs:54:5
66+
--> tests/ui/indexing_slicing_index.rs:74:5
6767
|
6868
LL | y[4];
6969
| ^^^^
7070

7171
error: indexing may panic
72-
--> tests/ui/indexing_slicing_index.rs:57:5
72+
--> tests/ui/indexing_slicing_index.rs:77:5
7373
|
7474
LL | v[0];
7575
| ^^^^
7676
|
7777
= help: consider using `.get(n)` or `.get_mut(n)` instead
7878

7979
error: indexing may panic
80-
--> tests/ui/indexing_slicing_index.rs:59:5
80+
--> tests/ui/indexing_slicing_index.rs:79:5
8181
|
8282
LL | v[10];
8383
| ^^^^^
8484
|
8585
= help: consider using `.get(n)` or `.get_mut(n)` instead
8686

8787
error: indexing may panic
88-
--> tests/ui/indexing_slicing_index.rs:61:5
88+
--> tests/ui/indexing_slicing_index.rs:81:5
8989
|
9090
LL | v[1 << 3];
9191
| ^^^^^^^^^
9292
|
9393
= help: consider using `.get(n)` or `.get_mut(n)` instead
9494

9595
error: index is out of bounds
96-
--> tests/ui/indexing_slicing_index.rs:69:5
96+
--> tests/ui/indexing_slicing_index.rs:89:5
9797
|
9898
LL | x[N];
9999
| ^^^^
100100

101101
error: indexing may panic
102-
--> tests/ui/indexing_slicing_index.rs:72:5
102+
--> tests/ui/indexing_slicing_index.rs:92:5
103103
|
104104
LL | v[N];
105105
| ^^^^
106106
|
107107
= help: consider using `.get(n)` or `.get_mut(n)` instead
108108

109109
error: indexing may panic
110-
--> tests/ui/indexing_slicing_index.rs:74:5
110+
--> tests/ui/indexing_slicing_index.rs:94:5
111111
|
112112
LL | v[M];
113113
| ^^^^
114114
|
115115
= help: consider using `.get(n)` or `.get_mut(n)` instead
116116

117117
error: index is out of bounds
118-
--> tests/ui/indexing_slicing_index.rs:78:13
118+
--> tests/ui/indexing_slicing_index.rs:98:13
119119
|
120120
LL | let _ = x[4];
121121
| ^^^^

tests/ui/indexing_slicing_slice.rs

+21
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@aux-build: proc_macros.rs
2+
13
#![warn(clippy::indexing_slicing)]
24
// We also check the out_of_bounds_indexing lint here, because it lints similar things and
35
// we want to avoid false positives.
@@ -11,6 +13,9 @@
1113
)]
1214
#![warn(clippy::indexing_slicing)]
1315

16+
extern crate proc_macros;
17+
use proc_macros::with_span;
18+
1419
use std::ops::Index;
1520

1621
struct BoolMap<T> {
@@ -86,6 +91,22 @@ impl<T> Index<i32> for Z<T> {
8691
}
8792
}
8893

94+
with_span!(
95+
span
96+
97+
fn dont_lint_proc_macro() {
98+
let x = [1, 2, 3, 4];
99+
let index: usize = 1;
100+
&x[index..];
101+
&x[..10];
102+
103+
let x = vec![0; 5];
104+
let index: usize = 1;
105+
&x[index..];
106+
&x[..10];
107+
}
108+
);
109+
89110
fn main() {
90111
let x = [1, 2, 3, 4];
91112
let index: usize = 1;

0 commit comments

Comments
 (0)