@@ -2,20 +2,21 @@ use clippy_config::Conf;
2
2
use clippy_utils:: diagnostics:: span_lint_and_sugg;
3
3
use clippy_utils:: is_from_proc_macro;
4
4
use clippy_utils:: msrvs:: { self , Msrv } ;
5
- use clippy_utils:: ty:: { same_type_and_consts , ty_from_hir_ty} ;
5
+ use clippy_utils:: ty:: { same_type_modulo_regions , ty_from_hir_ty} ;
6
6
use rustc_data_structures:: fx:: FxHashSet ;
7
7
use rustc_errors:: Applicability ;
8
8
use rustc_hir:: def:: { CtorOf , DefKind , Res } ;
9
9
use rustc_hir:: def_id:: LocalDefId ;
10
10
use rustc_hir:: intravisit:: { InferKind , Visitor , VisitorExt , walk_ty} ;
11
11
use rustc_hir:: {
12
- self as hir, AmbigArg , Expr , ExprKind , FnRetTy , FnSig , GenericArgsParentheses , GenericParam , GenericParamKind ,
13
- HirId , Impl , ImplItemKind , Item , ItemKind , Pat , PatExpr , PatExprKind , PatKind , Path , QPath , Ty , TyKind ,
12
+ self as hir, AmbigArg , Expr , ExprKind , FnRetTy , FnSig , GenericArgsParentheses , GenericParamKind , HirId , Impl ,
13
+ ImplItemKind , Item , ItemKind , Pat , PatExpr , PatExprKind , PatKind , Path , QPath , Ty , TyKind ,
14
14
} ;
15
15
use rustc_lint:: { LateContext , LateLintPass } ;
16
16
use rustc_middle:: ty:: Ty as MiddleTy ;
17
17
use rustc_session:: impl_lint_pass;
18
18
use rustc_span:: Span ;
19
+ use std:: iter;
19
20
20
21
declare_clippy_lint ! {
21
22
/// ### What it does
@@ -101,17 +102,11 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
101
102
let types_to_skip = generics
102
103
. params
103
104
. iter ( )
104
- . filter_map ( |param| match param {
105
- GenericParam {
106
- kind :
107
- GenericParamKind :: Const {
108
- ty : Ty { hir_id, .. } , ..
109
- } ,
110
- ..
111
- } => Some ( * hir_id) ,
105
+ . filter_map ( |param| match param. kind {
106
+ GenericParamKind :: Const { ty, .. } => Some ( ty. hir_id ) ,
112
107
_ => None ,
113
108
} )
114
- . chain ( std :: iter :: once ( self_ty. hir_id ) )
109
+ . chain ( [ self_ty. hir_id ] )
115
110
. collect ( ) ;
116
111
StackItem :: Check {
117
112
impl_id : item. owner_id . def_id ,
@@ -210,11 +205,11 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
210
205
&& !types_to_skip. contains ( & hir_ty. hir_id )
211
206
&& let ty = ty_from_hir_ty ( cx, hir_ty. as_unambig_ty ( ) )
212
207
&& let impl_ty = cx. tcx . type_of ( impl_id) . instantiate_identity ( )
213
- && same_type_and_consts ( ty, impl_ty)
208
+ && same_type_modulo_regions ( ty, impl_ty)
214
209
// Ensure the type we encounter and the one from the impl have the same lifetime parameters. It may be that
215
- // the lifetime parameters of `ty` are elided (`impl<'a> Foo<'a> { fn new() -> Self { Foo{..} } }`, in
210
+ // the lifetime parameters of `ty` are elided (`impl<'a> Foo<'a> { fn new() -> Self { Foo{..} } }`) , in
216
211
// which case we must still trigger the lint.
217
- && ( has_no_lifetime ( ty ) || same_lifetimes ( ty, impl_ty) )
212
+ && same_lifetimes ( ty, impl_ty)
218
213
&& self . msrv . meets ( cx, msrvs:: TYPE_ALIAS_ENUM_VARIANTS )
219
214
{
220
215
span_lint ( cx, hir_ty. span ) ;
@@ -227,18 +222,16 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
227
222
&& cx. typeck_results ( ) . expr_ty ( expr) == cx. tcx . type_of ( impl_id) . instantiate_identity ( )
228
223
&& self . msrv . meets ( cx, msrvs:: TYPE_ALIAS_ENUM_VARIANTS )
229
224
{
230
- } else {
231
- return ;
232
- }
233
- match expr. kind {
234
- ExprKind :: Struct ( QPath :: Resolved ( _, path) , ..) => check_path ( cx, path) ,
235
- ExprKind :: Call ( fun, _) => {
236
- if let ExprKind :: Path ( QPath :: Resolved ( _, path) ) = fun. kind {
237
- check_path ( cx, path) ;
238
- }
239
- } ,
240
- ExprKind :: Path ( QPath :: Resolved ( _, path) ) => check_path ( cx, path) ,
241
- _ => ( ) ,
225
+ match expr. kind {
226
+ ExprKind :: Struct ( QPath :: Resolved ( _, path) , ..) => check_path ( cx, path) ,
227
+ ExprKind :: Call ( fun, _) => {
228
+ if let ExprKind :: Path ( QPath :: Resolved ( _, path) ) = fun. kind {
229
+ check_path ( cx, path) ;
230
+ }
231
+ } ,
232
+ ExprKind :: Path ( QPath :: Resolved ( _, path) ) => check_path ( cx, path) ,
233
+ _ => ( ) ,
234
+ }
242
235
}
243
236
}
244
237
@@ -308,36 +301,20 @@ fn lint_path_to_variant(cx: &LateContext<'_>, path: &Path<'_>) {
308
301
}
309
302
}
310
303
311
- /// Returns `true` if types `a` and `b` have the same lifetime parameters, otherwise returns
312
- /// `false`.
304
+ /// Checks whether types `a` and `b` have the same lifetime parameters.
313
305
///
314
306
/// This function does not check that types `a` and `b` are the same types.
315
307
fn same_lifetimes < ' tcx > ( a : MiddleTy < ' tcx > , b : MiddleTy < ' tcx > ) -> bool {
316
308
use rustc_middle:: ty:: { Adt , GenericArgKind } ;
317
- match ( & a. kind ( ) , & b. kind ( ) ) {
318
- ( & Adt ( _, args_a) , & Adt ( _, args_b) ) => {
319
- args_a
320
- . iter ( )
321
- . zip ( args_b. iter ( ) )
322
- . all ( |( arg_a, arg_b) | match ( arg_a. kind ( ) , arg_b. kind ( ) ) {
323
- // TODO: Handle inferred lifetimes
324
- ( GenericArgKind :: Lifetime ( inner_a) , GenericArgKind :: Lifetime ( inner_b) ) => inner_a == inner_b,
325
- ( GenericArgKind :: Type ( type_a) , GenericArgKind :: Type ( type_b) ) => same_lifetimes ( type_a, type_b) ,
326
- _ => true ,
327
- } )
309
+ match ( a. kind ( ) , b. kind ( ) ) {
310
+ ( Adt ( _, args_a) , Adt ( _, args_b) ) => {
311
+ iter:: zip ( * args_a, * args_b) . all ( |( arg_a, arg_b) | match ( arg_a. kind ( ) , arg_b. kind ( ) ) {
312
+ // TODO: Handle inferred lifetimes
313
+ ( GenericArgKind :: Lifetime ( inner_a) , GenericArgKind :: Lifetime ( inner_b) ) => inner_a == inner_b,
314
+ ( GenericArgKind :: Type ( type_a) , GenericArgKind :: Type ( type_b) ) => same_lifetimes ( type_a, type_b) ,
315
+ _ => true ,
316
+ } )
328
317
} ,
329
318
_ => a == b,
330
319
}
331
320
}
332
-
333
- /// Returns `true` if `ty` has no lifetime parameter, otherwise returns `false`.
334
- fn has_no_lifetime ( ty : MiddleTy < ' _ > ) -> bool {
335
- use rustc_middle:: ty:: { Adt , GenericArgKind } ;
336
- match ty. kind ( ) {
337
- & Adt ( _, args) => !args
338
- . iter ( )
339
- // TODO: Handle inferred lifetimes
340
- . any ( |arg| matches ! ( arg. kind( ) , GenericArgKind :: Lifetime ( ..) ) ) ,
341
- _ => true ,
342
- }
343
- }
0 commit comments