ignore generics in handling

This commit is contained in:
Jacher 2024-05-30 13:15:25 +00:00
parent ae59f5002d
commit 1c117f12ea
4 changed files with 32 additions and 30 deletions

View File

@ -112,7 +112,7 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
&& deref.any(|l| { && deref.any(|l| {
l.peel_refs().is_slice() l.peel_refs().is_slice()
|| l.peel_refs().is_array() || l.peel_refs().is_array()
|| ty_has_appliciable_get_function(cx, l.peel_refs(), expr_ty, expr) || ty_has_applicable_get_function(cx, l.peel_refs(), expr_ty, expr)
}) })
{ {
let note = "the suggestion might not be applicable in constant blocks"; 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
/// Checks if the output Ty of the `get` method on this Ty (if any) matches the Ty returned by the /// Checks if the output Ty of the `get` method on this Ty (if any) matches the Ty returned by the
/// indexing operation (if any). /// indexing operation (if any).
fn ty_has_appliciable_get_function<'tcx>( fn ty_has_applicable_get_function<'tcx>(
cx: &LateContext<'tcx>, cx: &LateContext<'tcx>,
ty: Ty<'tcx>, ty: Ty<'tcx>,
array_ty: Ty<'tcx>, array_ty: Ty<'tcx>,
index_expr: &Expr<'_>, index_expr: &Expr<'_>,
) -> bool { ) -> bool {
if let ty::Adt(_, array_args) = array_ty.kind() if let ty::Adt(_, _) = array_ty.kind()
&& let Some(get_output_ty) = get_adt_inherent_method(cx, ty, sym!(get)).map(|m| { && let Some(get_output_ty) = get_adt_inherent_method(cx, ty, sym!(get)).map(|m| {
cx.tcx cx.tcx
.fn_sig(m.def_id) .fn_sig(m.def_id)
.instantiate(cx.tcx, array_args) .skip_binder()
.output() .output()
.skip_binder() .skip_binder()
}) })
&& let ty::Adt(def, args) = get_output_ty.kind() && let ty::Adt(def, args) = get_output_ty.kind()
&& cx.tcx.is_diagnostic_item(sym::Option, def.0.did) && cx.tcx.is_diagnostic_item(sym::Option, def.0.did)
&& let Some(option_generic_param) = args.get(0) && let Some(option_generic_param) = args.first()
&& let generic_ty = option_generic_param.expect_ty().peel_refs() && let generic_ty = option_generic_param.expect_ty().peel_refs()
&& cx.typeck_results().expr_ty(index_expr).peel_refs() == generic_ty.peel_refs() // FIXME: ideally this would handle type params and projections properly, for now just assume it's the same type
&& (cx.typeck_results().expr_ty(index_expr).peel_refs() == generic_ty.peel_refs()
|| matches!(generic_ty.peel_refs().kind(), ty::Param(_) | ty::Alias(_, _)))
{ {
true true
} else { } else {

View File

@ -18,8 +18,8 @@
use rustc_middle::ty::layout::ValidityRequirement; use rustc_middle::ty::layout::ValidityRequirement;
use rustc_middle::ty::{ use rustc_middle::ty::{
self, AdtDef, AliasTy, AssocItem, AssocKind, Binder, BoundRegion, FnSig, GenericArg, GenericArgKind, self, AdtDef, AliasTy, AssocItem, AssocKind, Binder, BoundRegion, FnSig, GenericArg, GenericArgKind,
GenericArgsRef, GenericParamDefKind, IntTy, List, ParamEnv, Region, RegionKind, ToPredicate, TraitRef, Ty, TyCtxt, GenericArgsRef, GenericParamDefKind, IntTy, ParamEnv, Region, RegionKind, TraitRef, Ty, TyCtxt, TypeSuperVisitable,
TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor, UintTy, VariantDef, VariantDiscr, TypeVisitable, TypeVisitableExt, TypeVisitor, UintTy, Upcast, VariantDef, VariantDiscr,
}; };
use rustc_span::symbol::Ident; use rustc_span::symbol::Ident;
use rustc_span::{sym, Span, Symbol, DUMMY_SP}; 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
/// This does not look for impls in the type's `Deref::Target` type. /// This does not look for impls in the type's `Deref::Target` type.
/// If you need this, you should wrap this call in `clippy_utils::ty::deref_chain().any(...)`. /// If you need this, you should wrap this call in `clippy_utils::ty::deref_chain().any(...)`.
pub fn get_adt_inherent_method<'a>(cx: &'a LateContext<'_>, ty: Ty<'_>, method_name: Symbol) -> Option<&'a AssocItem> { pub fn get_adt_inherent_method<'a>(cx: &'a LateContext<'_>, ty: Ty<'_>, method_name: Symbol) -> Option<&'a AssocItem> {
if let Some(ty_did) = ty.ty_adt_def().map(ty::AdtDef::did) { if let Some(ty_did) = ty.ty_adt_def().map(AdtDef::did) {
cx.tcx cx.tcx
.inherent_impls(ty_did) .inherent_impls(ty_did)
.into_iter() .into_iter()
@ -1356,7 +1356,7 @@ pub fn get_adt_inherent_method<'a>(cx: &'a LateContext<'_>, ty: Ty<'_>, method_n
.associated_items(did) .associated_items(did)
.filter_by_name_unhygienic(method_name) .filter_by_name_unhygienic(method_name)
.next() .next()
.filter(|item| item.kind == ty::AssocKind::Fn) .filter(|item| item.kind == AssocKind::Fn)
}) })
.next() .next()
.flatten() .flatten()

View File

@ -76,7 +76,7 @@ fn index(&self, _index: i32) -> &Self::Output {
struct Z<T>(T); struct Z<T>(T);
impl<T> Z<T> { impl<T> Z<T> {
fn get<T2>() -> T2 { fn get<T2>() -> T2 {
todo!() unimplemented!()
} }
} }
impl<T> Index<i32> for Z<T> { impl<T> Index<i32> for Z<T> {

View File

@ -1,5 +1,5 @@
error: slicing may panic error: slicing may panic
--> tests/ui/indexing_slicing_slice.rs:81:6 --> tests/ui/indexing_slicing_slice.rs:94:6
| |
LL | &x[index..]; LL | &x[index..];
| ^^^^^^^^^^ | ^^^^^^^^^^
@ -9,7 +9,7 @@ LL | &x[index..];
= help: to override `-D warnings` add `#[allow(clippy::indexing_slicing)]` = help: to override `-D warnings` add `#[allow(clippy::indexing_slicing)]`
error: slicing may panic error: slicing may panic
--> tests/ui/indexing_slicing_slice.rs:83:6 --> tests/ui/indexing_slicing_slice.rs:96:6
| |
LL | &x[..index]; LL | &x[..index];
| ^^^^^^^^^^ | ^^^^^^^^^^
@ -17,7 +17,7 @@ LL | &x[..index];
= help: consider using `.get(..n)`or `.get_mut(..n)` instead = help: consider using `.get(..n)`or `.get_mut(..n)` instead
error: slicing may panic error: slicing may panic
--> tests/ui/indexing_slicing_slice.rs:85:6 --> tests/ui/indexing_slicing_slice.rs:98:6
| |
LL | &x[index_from..index_to]; 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 = help: consider using `.get(n..m)` or `.get_mut(n..m)` instead
error: slicing may panic error: slicing may panic
--> tests/ui/indexing_slicing_slice.rs:87:6 --> tests/ui/indexing_slicing_slice.rs:100:6
| |
LL | &x[index_from..][..index_to]; 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 = help: consider using `.get(..n)`or `.get_mut(..n)` instead
error: slicing may panic error: slicing may panic
--> tests/ui/indexing_slicing_slice.rs:87:6 --> tests/ui/indexing_slicing_slice.rs:100:6
| |
LL | &x[index_from..][..index_to]; 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 = help: consider using `.get(n..)` or .get_mut(n..)` instead
error: slicing may panic error: slicing may panic
--> tests/ui/indexing_slicing_slice.rs:90:6 --> tests/ui/indexing_slicing_slice.rs:103:6
| |
LL | &x[5..][..10]; LL | &x[5..][..10];
| ^^^^^^^^^^^^ | ^^^^^^^^^^^^
@ -49,7 +49,7 @@ LL | &x[5..][..10];
= help: consider using `.get(..n)`or `.get_mut(..n)` instead = help: consider using `.get(..n)`or `.get_mut(..n)` instead
error: range is out of bounds error: range is out of bounds
--> tests/ui/indexing_slicing_slice.rs:90:8 --> tests/ui/indexing_slicing_slice.rs:103:8
| |
LL | &x[5..][..10]; LL | &x[5..][..10];
| ^ | ^
@ -58,7 +58,7 @@ LL | &x[5..][..10];
= help: to override `-D warnings` add `#[allow(clippy::out_of_bounds_indexing)]` = help: to override `-D warnings` add `#[allow(clippy::out_of_bounds_indexing)]`
error: slicing may panic error: slicing may panic
--> tests/ui/indexing_slicing_slice.rs:94:6 --> tests/ui/indexing_slicing_slice.rs:107:6
| |
LL | &x[0..][..3]; LL | &x[0..][..3];
| ^^^^^^^^^^^ | ^^^^^^^^^^^
@ -66,7 +66,7 @@ LL | &x[0..][..3];
= help: consider using `.get(..n)`or `.get_mut(..n)` instead = help: consider using `.get(..n)`or `.get_mut(..n)` instead
error: slicing may panic error: slicing may panic
--> tests/ui/indexing_slicing_slice.rs:96:6 --> tests/ui/indexing_slicing_slice.rs:109:6
| |
LL | &x[1..][..5]; LL | &x[1..][..5];
| ^^^^^^^^^^^ | ^^^^^^^^^^^
@ -74,19 +74,19 @@ LL | &x[1..][..5];
= help: consider using `.get(..n)`or `.get_mut(..n)` instead = help: consider using `.get(..n)`or `.get_mut(..n)` instead
error: range is out of bounds error: range is out of bounds
--> tests/ui/indexing_slicing_slice.rs:104:12 --> tests/ui/indexing_slicing_slice.rs:117:12
| |
LL | &y[0..=4]; LL | &y[0..=4];
| ^ | ^
error: range is out of bounds error: range is out of bounds
--> tests/ui/indexing_slicing_slice.rs:106:11 --> tests/ui/indexing_slicing_slice.rs:119:11
| |
LL | &y[..=4]; LL | &y[..=4];
| ^ | ^
error: slicing may panic error: slicing may panic
--> tests/ui/indexing_slicing_slice.rs:112:6 --> tests/ui/indexing_slicing_slice.rs:125:6
| |
LL | &v[10..100]; LL | &v[10..100];
| ^^^^^^^^^^ | ^^^^^^^^^^
@ -94,7 +94,7 @@ LL | &v[10..100];
= help: consider using `.get(n..m)` or `.get_mut(n..m)` instead = help: consider using `.get(n..m)` or `.get_mut(n..m)` instead
error: slicing may panic error: slicing may panic
--> tests/ui/indexing_slicing_slice.rs:114:6 --> tests/ui/indexing_slicing_slice.rs:127:6
| |
LL | &x[10..][..100]; LL | &x[10..][..100];
| ^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^
@ -102,13 +102,13 @@ LL | &x[10..][..100];
= help: consider using `.get(..n)`or `.get_mut(..n)` instead = help: consider using `.get(..n)`or `.get_mut(..n)` instead
error: range is out of bounds error: range is out of bounds
--> tests/ui/indexing_slicing_slice.rs:114:8 --> tests/ui/indexing_slicing_slice.rs:127:8
| |
LL | &x[10..][..100]; LL | &x[10..][..100];
| ^^ | ^^
error: slicing may panic error: slicing may panic
--> tests/ui/indexing_slicing_slice.rs:117:6 --> tests/ui/indexing_slicing_slice.rs:130:6
| |
LL | &v[10..]; LL | &v[10..];
| ^^^^^^^ | ^^^^^^^
@ -116,7 +116,7 @@ LL | &v[10..];
= help: consider using `.get(n..)` or .get_mut(n..)` instead = help: consider using `.get(n..)` or .get_mut(n..)` instead
error: slicing may panic error: slicing may panic
--> tests/ui/indexing_slicing_slice.rs:119:6 --> tests/ui/indexing_slicing_slice.rs:132:6
| |
LL | &v[..100]; LL | &v[..100];
| ^^^^^^^^ | ^^^^^^^^
@ -124,7 +124,7 @@ LL | &v[..100];
= help: consider using `.get(..n)`or `.get_mut(..n)` instead = help: consider using `.get(..n)`or `.get_mut(..n)` instead
error: indexing may panic error: indexing may panic
--> tests/ui/indexing_slicing_slice.rs:137:5 --> tests/ui/indexing_slicing_slice.rs:150:5
| |
LL | map_with_get[true]; LL | map_with_get[true];
| ^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^
@ -132,7 +132,7 @@ LL | map_with_get[true];
= help: consider using `.get(n)` or `.get_mut(n)` instead = help: consider using `.get(n)` or `.get_mut(n)` instead
error: indexing may panic error: indexing may panic
--> tests/ui/indexing_slicing_slice.rs:140:5 --> tests/ui/indexing_slicing_slice.rs:153:5
| |
LL | s[0]; LL | s[0];
| ^^^^ | ^^^^
@ -140,7 +140,7 @@ LL | s[0];
= help: consider using `.get(n)` or `.get_mut(n)` instead = help: consider using `.get(n)` or `.get_mut(n)` instead
error: indexing may panic error: indexing may panic
--> tests/ui/indexing_slicing_slice.rs:143:5 --> tests/ui/indexing_slicing_slice.rs:156:5
| |
LL | y[0]; LL | y[0];
| ^^^^ | ^^^^