Skip to content

Commit 1c117f1

Browse files
committed
ignore generics in handling
1 parent ae59f50 commit 1c117f1

File tree

4 files changed

+32
-30
lines changed

4 files changed

+32
-30
lines changed

clippy_lints/src/indexing_slicing.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
112112
&& deref.any(|l| {
113113
l.peel_refs().is_slice()
114114
|| l.peel_refs().is_array()
115-
|| ty_has_appliciable_get_function(cx, l.peel_refs(), expr_ty, expr)
115+
|| ty_has_applicable_get_function(cx, l.peel_refs(), expr_ty, expr)
116116
})
117117
{
118118
let note = "the suggestion might not be applicable in constant blocks";
@@ -244,25 +244,27 @@ fn to_const_range(cx: &LateContext<'_>, range: higher::Range<'_>, array_size: u1
244244

245245
/// Checks if the output Ty of the `get` method on this Ty (if any) matches the Ty returned by the
246246
/// indexing operation (if any).
247-
fn ty_has_appliciable_get_function<'tcx>(
247+
fn ty_has_applicable_get_function<'tcx>(
248248
cx: &LateContext<'tcx>,
249249
ty: Ty<'tcx>,
250250
array_ty: Ty<'tcx>,
251251
index_expr: &Expr<'_>,
252252
) -> bool {
253-
if let ty::Adt(_, array_args) = array_ty.kind()
253+
if let ty::Adt(_, _) = array_ty.kind()
254254
&& let Some(get_output_ty) = get_adt_inherent_method(cx, ty, sym!(get)).map(|m| {
255255
cx.tcx
256256
.fn_sig(m.def_id)
257-
.instantiate(cx.tcx, array_args)
257+
.skip_binder()
258258
.output()
259259
.skip_binder()
260260
})
261261
&& let ty::Adt(def, args) = get_output_ty.kind()
262262
&& cx.tcx.is_diagnostic_item(sym::Option, def.0.did)
263-
&& let Some(option_generic_param) = args.get(0)
263+
&& let Some(option_generic_param) = args.first()
264264
&& let generic_ty = option_generic_param.expect_ty().peel_refs()
265-
&& cx.typeck_results().expr_ty(index_expr).peel_refs() == generic_ty.peel_refs()
265+
// FIXME: ideally this would handle type params and projections properly, for now just assume it's the same type
266+
&& (cx.typeck_results().expr_ty(index_expr).peel_refs() == generic_ty.peel_refs()
267+
|| matches!(generic_ty.peel_refs().kind(), ty::Param(_) | ty::Alias(_, _)))
266268
{
267269
true
268270
} else {

clippy_utils/src/ty.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use rustc_middle::traits::EvaluationResult;
1818
use rustc_middle::ty::layout::ValidityRequirement;
1919
use rustc_middle::ty::{
2020
self, AdtDef, AliasTy, AssocItem, AssocKind, Binder, BoundRegion, FnSig, GenericArg, GenericArgKind,
21-
GenericArgsRef, GenericParamDefKind, IntTy, List, ParamEnv, Region, RegionKind, ToPredicate, TraitRef, Ty, TyCtxt,
22-
TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor, UintTy, VariantDef, VariantDiscr,
21+
GenericArgsRef, GenericParamDefKind, IntTy, ParamEnv, Region, RegionKind, TraitRef, Ty, TyCtxt, TypeSuperVisitable,
22+
TypeVisitable, TypeVisitableExt, TypeVisitor, UintTy, Upcast, VariantDef, VariantDiscr,
2323
};
2424
use rustc_span::symbol::Ident;
2525
use rustc_span::{sym, Span, Symbol, DUMMY_SP};
@@ -1346,7 +1346,7 @@ pub fn deref_chain<'cx, 'tcx>(cx: &'cx LateContext<'tcx>, ty: Ty<'tcx>) -> impl
13461346
/// This does not look for impls in the type's `Deref::Target` type.
13471347
/// If you need this, you should wrap this call in `clippy_utils::ty::deref_chain().any(...)`.
13481348
pub fn get_adt_inherent_method<'a>(cx: &'a LateContext<'_>, ty: Ty<'_>, method_name: Symbol) -> Option<&'a AssocItem> {
1349-
if let Some(ty_did) = ty.ty_adt_def().map(ty::AdtDef::did) {
1349+
if let Some(ty_did) = ty.ty_adt_def().map(AdtDef::did) {
13501350
cx.tcx
13511351
.inherent_impls(ty_did)
13521352
.into_iter()
@@ -1356,7 +1356,7 @@ pub fn get_adt_inherent_method<'a>(cx: &'a LateContext<'_>, ty: Ty<'_>, method_n
13561356
.associated_items(did)
13571357
.filter_by_name_unhygienic(method_name)
13581358
.next()
1359-
.filter(|item| item.kind == ty::AssocKind::Fn)
1359+
.filter(|item| item.kind == AssocKind::Fn)
13601360
})
13611361
.next()
13621362
.flatten()

tests/ui/indexing_slicing_slice.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl<T> Index<i32> for Y<T> {
7676
struct Z<T>(T);
7777
impl<T> Z<T> {
7878
fn get<T2>() -> T2 {
79-
todo!()
79+
unimplemented!()
8080
}
8181
}
8282
impl<T> Index<i32> for Z<T> {

tests/ui/indexing_slicing_slice.stderr

Lines changed: 19 additions & 19 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:81:6
2+
--> tests/ui/indexing_slicing_slice.rs:94: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:83:6
12+
--> tests/ui/indexing_slicing_slice.rs:96: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:85:6
20+
--> tests/ui/indexing_slicing_slice.rs:98: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:87:6
28+
--> tests/ui/indexing_slicing_slice.rs:100: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:87:6
36+
--> tests/ui/indexing_slicing_slice.rs:100: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:90:6
44+
--> tests/ui/indexing_slicing_slice.rs:103: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:90:8
52+
--> tests/ui/indexing_slicing_slice.rs:103: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:94:6
61+
--> tests/ui/indexing_slicing_slice.rs:107: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:96:6
69+
--> tests/ui/indexing_slicing_slice.rs:109: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:104:12
77+
--> tests/ui/indexing_slicing_slice.rs:117:12
7878
|
7979
LL | &y[0..=4];
8080
| ^
8181

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

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

110110
error: slicing may panic
111-
--> tests/ui/indexing_slicing_slice.rs:117:6
111+
--> tests/ui/indexing_slicing_slice.rs:130: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:119:6
119+
--> tests/ui/indexing_slicing_slice.rs:132: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:137:5
127+
--> tests/ui/indexing_slicing_slice.rs:150: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:140:5
135+
--> tests/ui/indexing_slicing_slice.rs:153: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:143:5
143+
--> tests/ui/indexing_slicing_slice.rs:156:5
144144
|
145145
LL | y[0];
146146
| ^^^^

0 commit comments

Comments
 (0)