Skip to content

Commit c658805

Browse files
authored
Merge branch 'rust-lang:master' into manual-char-comparison-pattern
2 parents 73adcce + 9ddea51 commit c658805

5 files changed

+80
-37
lines changed

clippy_lints/src/indexing_slicing.rs

+4-2
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};
@@ -102,7 +102,9 @@ impl IndexingSlicing {
102102

103103
impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
104104
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
105-
if self.suppress_restriction_lint_in_const && cx.tcx.hir().is_inside_const_context(expr.hir_id) {
105+
if (self.suppress_restriction_lint_in_const && cx.tcx.hir().is_inside_const_context(expr.hir_id))
106+
|| is_from_proc_macro(cx, expr)
107+
{
106108
return;
107109
}
108110

tests/ui/indexing_slicing_index.rs

+20
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_index.stderr

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: indexing may panic
2-
--> tests/ui/indexing_slicing_index.rs:15:20
2+
--> tests/ui/indexing_slicing_index.rs:19:20
33
|
44
LL | const REF: &i32 = &ARR[idx()]; // This should be linted, since `suppress-restriction-lint-in-const` default is false.
55
| ^^^^^^^^^^
@@ -10,27 +10,27 @@ LL | const REF: &i32 = &ARR[idx()]; // This should be linted, since `suppress-re
1010
= help: to override `-D warnings` add `#[allow(clippy::indexing_slicing)]`
1111

1212
error[E0080]: evaluation of `main::{constant#3}` failed
13-
--> tests/ui/indexing_slicing_index.rs:47:14
13+
--> tests/ui/indexing_slicing_index.rs:67:14
1414
|
1515
LL | const { &ARR[idx4()] };
1616
| ^^^^^^^^^^^ index out of bounds: the length is 2 but the index is 4
1717

1818
note: erroneous constant encountered
19-
--> tests/ui/indexing_slicing_index.rs:47:5
19+
--> tests/ui/indexing_slicing_index.rs:67:5
2020
|
2121
LL | const { &ARR[idx4()] };
2222
| ^^^^^^^^^^^^^^^^^^^^^^
2323

2424
error: indexing may panic
25-
--> tests/ui/indexing_slicing_index.rs:28:5
25+
--> tests/ui/indexing_slicing_index.rs:48:5
2626
|
2727
LL | x[index];
2828
| ^^^^^^^^
2929
|
3030
= help: consider using `.get(n)` or `.get_mut(n)` instead
3131

3232
error: index is out of bounds
33-
--> tests/ui/indexing_slicing_index.rs:31:5
33+
--> tests/ui/indexing_slicing_index.rs:51:5
3434
|
3535
LL | x[4];
3636
| ^^^^
@@ -39,13 +39,13 @@ LL | x[4];
3939
= help: to override `-D warnings` add `#[allow(clippy::out_of_bounds_indexing)]`
4040

4141
error: index is out of bounds
42-
--> tests/ui/indexing_slicing_index.rs:33:5
42+
--> tests/ui/indexing_slicing_index.rs:53:5
4343
|
4444
LL | x[1 << 3];
4545
| ^^^^^^^^^
4646

4747
error: indexing may panic
48-
--> tests/ui/indexing_slicing_index.rs:44:14
48+
--> tests/ui/indexing_slicing_index.rs:64:14
4949
|
5050
LL | const { &ARR[idx()] };
5151
| ^^^^^^^^^^
@@ -54,7 +54,7 @@ LL | const { &ARR[idx()] };
5454
= note: the suggestion might not be applicable in constant blocks
5555

5656
error: indexing may panic
57-
--> tests/ui/indexing_slicing_index.rs:47:14
57+
--> tests/ui/indexing_slicing_index.rs:67:14
5858
|
5959
LL | const { &ARR[idx4()] };
6060
| ^^^^^^^^^^^
@@ -63,59 +63,59 @@ LL | const { &ARR[idx4()] };
6363
= note: the suggestion might not be applicable in constant blocks
6464

6565
error: index is out of bounds
66-
--> tests/ui/indexing_slicing_index.rs:54:5
66+
--> tests/ui/indexing_slicing_index.rs:74:5
6767
|
6868
LL | y[4];
6969
| ^^^^
7070

7171
error: indexing may panic
72-
--> tests/ui/indexing_slicing_index.rs:57:5
72+
--> tests/ui/indexing_slicing_index.rs:77:5
7373
|
7474
LL | v[0];
7575
| ^^^^
7676
|
7777
= help: consider using `.get(n)` or `.get_mut(n)` instead
7878

7979
error: indexing may panic
80-
--> tests/ui/indexing_slicing_index.rs:59:5
80+
--> tests/ui/indexing_slicing_index.rs:79:5
8181
|
8282
LL | v[10];
8383
| ^^^^^
8484
|
8585
= help: consider using `.get(n)` or `.get_mut(n)` instead
8686

8787
error: indexing may panic
88-
--> tests/ui/indexing_slicing_index.rs:61:5
88+
--> tests/ui/indexing_slicing_index.rs:81:5
8989
|
9090
LL | v[1 << 3];
9191
| ^^^^^^^^^
9292
|
9393
= help: consider using `.get(n)` or `.get_mut(n)` instead
9494

9595
error: index is out of bounds
96-
--> tests/ui/indexing_slicing_index.rs:69:5
96+
--> tests/ui/indexing_slicing_index.rs:89:5
9797
|
9898
LL | x[N];
9999
| ^^^^
100100

101101
error: indexing may panic
102-
--> tests/ui/indexing_slicing_index.rs:72:5
102+
--> tests/ui/indexing_slicing_index.rs:92:5
103103
|
104104
LL | v[N];
105105
| ^^^^
106106
|
107107
= help: consider using `.get(n)` or `.get_mut(n)` instead
108108

109109
error: indexing may panic
110-
--> tests/ui/indexing_slicing_index.rs:74:5
110+
--> tests/ui/indexing_slicing_index.rs:94:5
111111
|
112112
LL | v[M];
113113
| ^^^^
114114
|
115115
= help: consider using `.get(n)` or `.get_mut(n)` instead
116116

117117
error: index is out of bounds
118-
--> tests/ui/indexing_slicing_index.rs:78:13
118+
--> tests/ui/indexing_slicing_index.rs:98:13
119119
|
120120
LL | let _ = x[4];
121121
| ^^^^

tests/ui/indexing_slicing_slice.rs

+21
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;

tests/ui/indexing_slicing_slice.stderr

+19-19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: slicing may panic
2-
--> tests/ui/indexing_slicing_slice.rs:94:6
2+
--> tests/ui/indexing_slicing_slice.rs:115:6
33
|
44
LL | &x[index..];
55
| ^^^^^^^^^^
@@ -9,47 +9,47 @@ LL | &x[index..];
99
= help: to override `-D warnings` add `#[allow(clippy::indexing_slicing)]`
1010

1111
error: slicing may panic
12-
--> tests/ui/indexing_slicing_slice.rs:96:6
12+
--> tests/ui/indexing_slicing_slice.rs:117:6
1313
|
1414
LL | &x[..index];
1515
| ^^^^^^^^^^
1616
|
1717
= help: consider using `.get(..n)`or `.get_mut(..n)` instead
1818

1919
error: slicing may panic
20-
--> tests/ui/indexing_slicing_slice.rs:98:6
20+
--> tests/ui/indexing_slicing_slice.rs:119:6
2121
|
2222
LL | &x[index_from..index_to];
2323
| ^^^^^^^^^^^^^^^^^^^^^^^
2424
|
2525
= help: consider using `.get(n..m)` or `.get_mut(n..m)` instead
2626

2727
error: slicing may panic
28-
--> tests/ui/indexing_slicing_slice.rs:100:6
28+
--> tests/ui/indexing_slicing_slice.rs:121:6
2929
|
3030
LL | &x[index_from..][..index_to];
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
3232
|
3333
= help: consider using `.get(..n)`or `.get_mut(..n)` instead
3434

3535
error: slicing may panic
36-
--> tests/ui/indexing_slicing_slice.rs:100:6
36+
--> tests/ui/indexing_slicing_slice.rs:121:6
3737
|
3838
LL | &x[index_from..][..index_to];
3939
| ^^^^^^^^^^^^^^^
4040
|
4141
= help: consider using `.get(n..)` or .get_mut(n..)` instead
4242

4343
error: slicing may panic
44-
--> tests/ui/indexing_slicing_slice.rs:103:6
44+
--> tests/ui/indexing_slicing_slice.rs:124:6
4545
|
4646
LL | &x[5..][..10];
4747
| ^^^^^^^^^^^^
4848
|
4949
= help: consider using `.get(..n)`or `.get_mut(..n)` instead
5050

5151
error: range is out of bounds
52-
--> tests/ui/indexing_slicing_slice.rs:103:8
52+
--> tests/ui/indexing_slicing_slice.rs:124:8
5353
|
5454
LL | &x[5..][..10];
5555
| ^
@@ -58,89 +58,89 @@ LL | &x[5..][..10];
5858
= help: to override `-D warnings` add `#[allow(clippy::out_of_bounds_indexing)]`
5959

6060
error: slicing may panic
61-
--> tests/ui/indexing_slicing_slice.rs:107:6
61+
--> tests/ui/indexing_slicing_slice.rs:128:6
6262
|
6363
LL | &x[0..][..3];
6464
| ^^^^^^^^^^^
6565
|
6666
= help: consider using `.get(..n)`or `.get_mut(..n)` instead
6767

6868
error: slicing may panic
69-
--> tests/ui/indexing_slicing_slice.rs:109:6
69+
--> tests/ui/indexing_slicing_slice.rs:130:6
7070
|
7171
LL | &x[1..][..5];
7272
| ^^^^^^^^^^^
7373
|
7474
= help: consider using `.get(..n)`or `.get_mut(..n)` instead
7575

7676
error: range is out of bounds
77-
--> tests/ui/indexing_slicing_slice.rs:117:12
77+
--> tests/ui/indexing_slicing_slice.rs:138:12
7878
|
7979
LL | &y[0..=4];
8080
| ^
8181

8282
error: range is out of bounds
83-
--> tests/ui/indexing_slicing_slice.rs:119:11
83+
--> tests/ui/indexing_slicing_slice.rs:140:11
8484
|
8585
LL | &y[..=4];
8686
| ^
8787

8888
error: slicing may panic
89-
--> tests/ui/indexing_slicing_slice.rs:125:6
89+
--> tests/ui/indexing_slicing_slice.rs:146:6
9090
|
9191
LL | &v[10..100];
9292
| ^^^^^^^^^^
9393
|
9494
= help: consider using `.get(n..m)` or `.get_mut(n..m)` instead
9595

9696
error: slicing may panic
97-
--> tests/ui/indexing_slicing_slice.rs:127:6
97+
--> tests/ui/indexing_slicing_slice.rs:148:6
9898
|
9999
LL | &x[10..][..100];
100100
| ^^^^^^^^^^^^^^
101101
|
102102
= help: consider using `.get(..n)`or `.get_mut(..n)` instead
103103

104104
error: range is out of bounds
105-
--> tests/ui/indexing_slicing_slice.rs:127:8
105+
--> tests/ui/indexing_slicing_slice.rs:148:8
106106
|
107107
LL | &x[10..][..100];
108108
| ^^
109109

110110
error: slicing may panic
111-
--> tests/ui/indexing_slicing_slice.rs:130:6
111+
--> tests/ui/indexing_slicing_slice.rs:151:6
112112
|
113113
LL | &v[10..];
114114
| ^^^^^^^
115115
|
116116
= help: consider using `.get(n..)` or .get_mut(n..)` instead
117117

118118
error: slicing may panic
119-
--> tests/ui/indexing_slicing_slice.rs:132:6
119+
--> tests/ui/indexing_slicing_slice.rs:153:6
120120
|
121121
LL | &v[..100];
122122
| ^^^^^^^^
123123
|
124124
= help: consider using `.get(..n)`or `.get_mut(..n)` instead
125125

126126
error: indexing may panic
127-
--> tests/ui/indexing_slicing_slice.rs:150:5
127+
--> tests/ui/indexing_slicing_slice.rs:171:5
128128
|
129129
LL | map_with_get[true];
130130
| ^^^^^^^^^^^^^^^^^^
131131
|
132132
= help: consider using `.get(n)` or `.get_mut(n)` instead
133133

134134
error: indexing may panic
135-
--> tests/ui/indexing_slicing_slice.rs:153:5
135+
--> tests/ui/indexing_slicing_slice.rs:174:5
136136
|
137137
LL | s[0];
138138
| ^^^^
139139
|
140140
= help: consider using `.get(n)` or `.get_mut(n)` instead
141141

142142
error: indexing may panic
143-
--> tests/ui/indexing_slicing_slice.rs:156:5
143+
--> tests/ui/indexing_slicing_slice.rs:177:5
144144
|
145145
LL | y[0];
146146
| ^^^^

0 commit comments

Comments
 (0)