Skip to content

Commit 3f96362

Browse files
committed
Start addressing review comments
1 parent 9751dd9 commit 3f96362

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

clippy_utils/src/ty/type_certainty/certainty.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,18 @@ impl Certainty {
9696
}
9797

9898
pub fn clear_def_id(self) -> Certainty {
99-
match self {
100-
Certainty::Uncertain => Certainty::Uncertain,
101-
Certainty::Certain(_) => Certainty::Certain(None),
102-
Certainty::Contradiction => Certainty::Contradiction,
99+
if matches!(self, Certainty::Certain(_)) {
100+
Certainty::Certain(None)
101+
} else {
102+
self
103103
}
104104
}
105105

106106
pub fn with_def_id(self, def_id: DefId) -> Certainty {
107-
match self {
108-
Certainty::Uncertain => Certainty::Uncertain,
109-
Certainty::Certain(_) => Certainty::Certain(Some(def_id)),
110-
Certainty::Contradiction => Certainty::Contradiction,
107+
if matches!(self, Certainty::Certain(_)) {
108+
Certainty::Certain(Some(def_id))
109+
} else {
110+
self
111111
}
112112
}
113113

clippy_utils/src/ty/type_certainty/mod.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
//! As a heuristic, `expr_type_is_certain` may produce false negatives, but a false positive should
1212
//! be considered a bug.
1313
14-
#![allow(clippy::wrong_self_convention)]
15-
1614
use crate::def_path_res;
1715
use rustc_hir::intravisit::{walk_qpath, walk_ty, Visitor};
1816
use rustc_hir::{
@@ -130,6 +128,12 @@ impl<'cx, 'tcx> Visitor<'cx> for CertaintyVisitor<'cx, 'tcx> {
130128

131129
fn type_certainty(cx: &LateContext<'_>, ty: &hir::Ty<'_>) -> Certainty {
132130
// 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)`.
133137
if let TyKind::Path(qpath) = &ty.kind {
134138
qpath_certainty(cx, qpath, true)
135139
} else {
@@ -288,16 +292,12 @@ fn type_is_inferrable_from_arguments(cx: &LateContext<'_>, expr: &Expr<'_>) -> b
288292
let generics = cx.tcx.generics_of(callee_def_id);
289293
let fn_sig = cx.tcx.fn_sig(callee_def_id).skip_binder();
290294

295+
// Check that all type parameters appear in the functions input types.
291296
(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))
301301
})
302302
}
303303

@@ -308,3 +308,8 @@ fn self_ty<'tcx>(cx: &LateContext<'tcx>, method_def_id: DefId) -> Ty<'tcx> {
308308
fn adt_def_id(ty: Ty<'_>) -> Option<DefId> {
309309
ty.peel_refs().ty_adt_def().map(AdtDef::did)
310310
}
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

Comments
 (0)