Skip to content

Commit

Permalink
Merge branch 'rust-lang:master' into manual-char-comparison-pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
AurelienFT authored Jun 11, 2024
2 parents 73adcce + 9ddea51 commit c658805
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 37 deletions.
6 changes: 4 additions & 2 deletions 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 @@ -102,7 +102,9 @@ impl IndexingSlicing {

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

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
38 changes: 19 additions & 19 deletions tests/ui/indexing_slicing_slice.stderr
Original file line number Diff line number Diff line change
@@ -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..];
| ^^^^^^^^^^
Expand All @@ -9,47 +9,47 @@ 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];
| ^^^^^^^^^^
|
= 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];
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= 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];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= 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];
| ^^^^^^^^^^^^^^^
|
= 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];
| ^^^^^^^^^^^^
|
= 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];
| ^
Expand All @@ -58,89 +58,89 @@ 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];
| ^^^^^^^^^^^
|
= 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];
| ^^^^^^^^^^^
|
= 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];
| ^^^^^^^^^^
|
= 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];
| ^^^^^^^^^^^^^^
|
= 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..];
| ^^^^^^^
|
= 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];
| ^^^^^^^^
|
= 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];
| ^^^^^^^^^^^^^^^^^^
|
= 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];
| ^^^^
|
= 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];
| ^^^^
Expand Down

0 comments on commit c658805

Please sign in to comment.