11
11
//! As a heuristic, `expr_type_is_certain` may produce false negatives, but a false positive should
12
12
//! be considered a bug.
13
13
14
- #![ allow( clippy:: wrong_self_convention) ]
15
-
16
14
use crate :: def_path_res;
17
15
use rustc_hir:: def:: Res ;
18
16
use rustc_hir:: def_id:: DefId ;
@@ -130,6 +128,12 @@ impl<'cx, 'tcx> Visitor<'cx> for CertaintyVisitor<'cx, 'tcx> {
130
128
131
129
fn type_certainty ( cx : & LateContext < ' _ > , ty : & hir:: Ty < ' _ > ) -> Certainty {
132
130
// Handle `TyKind::Path` specially so that its `DefId` can be preserved.
131
+ //
132
+ // Note that `CertaintyVisitor::new` initializes the visitor's internal certainty to
133
+ // `Certainty::Certain(None)`. Furthermore, if a `TyKind::Path` is encountered while traversing
134
+ // `ty`, the result of the call to `qpath_certainty` is combined with the visitor's internal
135
+ // certainty using `Certainty::meet`. Thus, if the `TyKind::Path` were not treated specially here,
136
+ // the resulting certainty would be `Certainty::Certain(None)`.
133
137
if let TyKind :: Path ( qpath) = & ty. kind {
134
138
qpath_certainty ( cx, qpath, true )
135
139
} else {
@@ -288,16 +292,12 @@ fn type_is_inferrable_from_arguments(cx: &LateContext<'_>, expr: &Expr<'_>) -> b
288
292
let generics = cx. tcx . generics_of ( callee_def_id) ;
289
293
let fn_sig = cx. tcx . fn_sig ( callee_def_id) . skip_binder ( ) ;
290
294
295
+ // Check that all type parameters appear in the functions input types.
291
296
( 0 ..( generics. parent_count + generics. params . len ( ) ) as u32 ) . all ( |index| {
292
- fn_sig. inputs ( ) . iter ( ) . any ( |input_ty| {
293
- input_ty. skip_binder ( ) . walk ( ) . any ( |arg| {
294
- if let GenericArgKind :: Type ( ty) = arg. unpack ( ) {
295
- ty. is_param ( index)
296
- } else {
297
- false
298
- }
299
- } )
300
- } )
297
+ fn_sig
298
+ . inputs ( )
299
+ . iter ( )
300
+ . any ( |input_ty| contains_param ( * input_ty. skip_binder ( ) , index) )
301
301
} )
302
302
}
303
303
@@ -308,3 +308,8 @@ fn self_ty<'tcx>(cx: &LateContext<'tcx>, method_def_id: DefId) -> Ty<'tcx> {
308
308
fn adt_def_id ( ty : Ty < ' _ > ) -> Option < DefId > {
309
309
ty. peel_refs ( ) . ty_adt_def ( ) . map ( AdtDef :: did)
310
310
}
311
+
312
+ fn contains_param ( ty : Ty < ' _ > , index : u32 ) -> bool {
313
+ ty. walk ( )
314
+ . any ( |arg| matches ! ( arg. unpack( ) , GenericArgKind :: Type ( ty) if ty. is_param( index) ) )
315
+ }
0 commit comments