Skip to content

Commit ae59f50

Browse files
committed
add additional testcases
1 parent e186ed2 commit ae59f50

File tree

3 files changed

+89
-22
lines changed

3 files changed

+89
-22
lines changed

clippy_lints/src/indexing_slicing.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,6 @@ fn ty_has_appliciable_get_function<'tcx>(
262262
&& cx.tcx.is_diagnostic_item(sym::Option, def.0.did)
263263
&& let Some(option_generic_param) = args.get(0)
264264
&& let generic_ty = option_generic_param.expect_ty().peel_refs()
265-
&& let _ = println!(
266-
"{}, {}",
267-
cx.typeck_results().expr_ty(index_expr).peel_refs(),
268-
generic_ty.peel_refs()
269-
)
270265
&& cx.typeck_results().expr_ty(index_expr).peel_refs() == generic_ty.peel_refs()
271266
{
272267
true

tests/ui/indexing_slicing_slice.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,45 @@ impl<T> BoolMapWithGet<T> {
4747
}
4848
}
4949

50+
struct S<T>(T);
51+
impl S<i32> {
52+
fn get() -> Option<i32> {
53+
unimplemented!()
54+
}
55+
}
56+
impl<T> Index<i32> for S<T> {
57+
type Output = T;
58+
fn index(&self, _index: i32) -> &Self::Output {
59+
&self.0
60+
}
61+
}
62+
63+
struct Y<T>(T);
64+
impl Y<i32> {
65+
fn get<U>() -> Option<U> {
66+
unimplemented!()
67+
}
68+
}
69+
impl<T> Index<i32> for Y<T> {
70+
type Output = T;
71+
fn index(&self, _index: i32) -> &Self::Output {
72+
&self.0
73+
}
74+
}
75+
76+
struct Z<T>(T);
77+
impl<T> Z<T> {
78+
fn get<T2>() -> T2 {
79+
todo!()
80+
}
81+
}
82+
impl<T> Index<i32> for Z<T> {
83+
type Output = T;
84+
fn index(&self, _index: i32) -> &Self::Output {
85+
&self.0
86+
}
87+
}
88+
5089
fn main() {
5190
let x = [1, 2, 3, 4];
5291
let index: usize = 1;
@@ -109,4 +148,13 @@ fn main() {
109148

110149
// Lint on this, because `get` does exist with same signature
111150
map_with_get[true];
151+
152+
let s = S::<i32>(1);
153+
s[0];
154+
155+
let y = Y::<i32>(1);
156+
y[0];
157+
158+
let z = Z::<i32>(1);
159+
z[0];
112160
}
Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: slicing may panic
2-
--> tests/ui/indexing_slicing_slice.rs:27:6
2+
--> tests/ui/indexing_slicing_slice.rs:81: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:29:6
12+
--> tests/ui/indexing_slicing_slice.rs:83: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:31:6
20+
--> tests/ui/indexing_slicing_slice.rs:85: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:33:6
28+
--> tests/ui/indexing_slicing_slice.rs:87: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:33:6
36+
--> tests/ui/indexing_slicing_slice.rs:87: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:36:6
44+
--> tests/ui/indexing_slicing_slice.rs:90: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:36:8
52+
--> tests/ui/indexing_slicing_slice.rs:90:8
5353
|
5454
LL | &x[5..][..10];
5555
| ^
@@ -58,70 +58,94 @@ 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:40:6
61+
--> tests/ui/indexing_slicing_slice.rs:94: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:42:6
69+
--> tests/ui/indexing_slicing_slice.rs:96: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:50:12
77+
--> tests/ui/indexing_slicing_slice.rs:104:12
7878
|
7979
LL | &y[0..=4];
8080
| ^
8181

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

8888
error: slicing may panic
89-
--> tests/ui/indexing_slicing_slice.rs:58:6
89+
--> tests/ui/indexing_slicing_slice.rs:112: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:60:6
97+
--> tests/ui/indexing_slicing_slice.rs:114: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:60:8
105+
--> tests/ui/indexing_slicing_slice.rs:114:8
106106
|
107107
LL | &x[10..][..100];
108108
| ^^
109109

110110
error: slicing may panic
111-
--> tests/ui/indexing_slicing_slice.rs:63:6
111+
--> tests/ui/indexing_slicing_slice.rs:117: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:65:6
119+
--> tests/ui/indexing_slicing_slice.rs:119:6
120120
|
121121
LL | &v[..100];
122122
| ^^^^^^^^
123123
|
124124
= help: consider using `.get(..n)`or `.get_mut(..n)` instead
125125

126-
error: aborting due to 16 previous errors
126+
error: indexing may panic
127+
--> tests/ui/indexing_slicing_slice.rs:137:5
128+
|
129+
LL | map_with_get[true];
130+
| ^^^^^^^^^^^^^^^^^^
131+
|
132+
= help: consider using `.get(n)` or `.get_mut(n)` instead
133+
134+
error: indexing may panic
135+
--> tests/ui/indexing_slicing_slice.rs:140:5
136+
|
137+
LL | s[0];
138+
| ^^^^
139+
|
140+
= help: consider using `.get(n)` or `.get_mut(n)` instead
141+
142+
error: indexing may panic
143+
--> tests/ui/indexing_slicing_slice.rs:143:5
144+
|
145+
LL | y[0];
146+
| ^^^^
147+
|
148+
= help: consider using `.get(n)` or `.get_mut(n)` instead
149+
150+
error: aborting due to 19 previous errors
127151

0 commit comments

Comments
 (0)