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.
This commit is contained in:
bors 2024-06-11 15:27:36 +00:00
commit 9ddea51a73
5 changed files with 80 additions and 37 deletions

View File

@ -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 @@ pub fn new(suppress_restriction_lint_in_const: bool) -> Self {
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;
}

View File

@ -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;

View File

@ -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];
| ^^^^

View File

@ -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 @@ fn index(&self, _index: i32) -> &Self::Output {
}
}
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;

View File

@ -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];
| ^^^^