Skip to content

Commit 18c133d

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

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

clippy_lints/src/indexing_slicing.rs

Lines changed: 14 additions & 1 deletion
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

Lines changed: 20 additions & 0 deletions
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_slice.rs

Lines changed: 21 additions & 0 deletions
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)