Skip to content

Commit

Permalink
Don't lint indexing_slicing lints on proc macros
Browse files Browse the repository at this point in the history
  • Loading branch information
lochetti committed Jun 9, 2024
1 parent 336046c commit 1b667aa
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 36 deletions.
15 changes: 14 additions & 1 deletion clippy_lints/src/indexing_slicing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -126,6 +126,10 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
return;
};

if is_from_proc_macro(cx, expr) {
return;
}

let const_range = to_const_range(cx, range, size);

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

if is_from_proc_macro(cx, expr) {
return;
}

span_lint_and_then(cx, INDEXING_SLICING, expr.span, "slicing may panic", |diag| {
diag.help(help_msg);

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

if is_from_proc_macro(cx, expr) {
return;
}

span_lint_and_then(cx, INDEXING_SLICING, expr.span, "indexing may panic", |diag| {
diag.help("consider using `.get(n)` or `.get_mut(n)` instead");

Expand Down
20 changes: 20 additions & 0 deletions tests/ui/indexing_slicing_index.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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;
Expand Down
32 changes: 16 additions & 16 deletions tests/ui/indexing_slicing_index.stderr
Original file line number Diff line number Diff line change
@@ -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.
| ^^^^^^^^^^
Expand All @@ -10,27 +10,27 @@ 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];
| ^^^^^^^^
|
= 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];
| ^^^^
Expand All @@ -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()] };
| ^^^^^^^^^^
Expand All @@ -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()] };
| ^^^^^^^^^^^
Expand All @@ -63,59 +63,59 @@ 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];
| ^^^^
|
= 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];
| ^^^^^
|
= 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];
| ^^^^^^^^^
|
= 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];
| ^^^^
|
= 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];
| ^^^^
|
= 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];
| ^^^^
Expand Down
21 changes: 21 additions & 0 deletions tests/ui/indexing_slicing_slice.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -11,6 +13,9 @@
)]
#![warn(clippy::indexing_slicing)]

extern crate proc_macros;
use proc_macros::with_span;

use std::ops::Index;

struct BoolMap<T> {
Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 1b667aa

Please sign in to comment.