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 70ca9a1 commit 657e867
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
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,7 +2,7 @@

use clippy_utils::consts::{constant, Constant};
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
use clippy_utils::higher;
use clippy_utils::{higher, is_from_proc_macro};
use clippy_utils::ty::{deref_chain, get_adt_inherent_method};
use rustc_ast::ast::RangeLimits;
use rustc_hir::{Expr, ExprKind};
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
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

0 comments on commit 657e867

Please sign in to comment.