Simplify clippy::default_trait_access

Remove repeated matching on the same QPath.
This commit is contained in:
Michael Wright 2020-09-04 05:15:31 +02:00
parent 8829214764
commit cf1cc7c449

View file

@ -38,37 +38,23 @@ impl<'tcx> LateLintPass<'tcx> for DefaultTraitAccess {
if let ExprKind::Path(ref qpath) = path.kind; if let ExprKind::Path(ref qpath) = path.kind;
if let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id(); if let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id();
if match_def_path(cx, def_id, &paths::DEFAULT_TRAIT_METHOD); if match_def_path(cx, def_id, &paths::DEFAULT_TRAIT_METHOD);
// Detect and ignore <Foo as Default>::default() because these calls do explicitly name the type.
if let QPath::Resolved(None, _path) = qpath;
then { then {
match qpath { let expr_ty = cx.typeck_results().expr_ty(expr);
QPath::Resolved(..) => { if let ty::Adt(def, ..) = expr_ty.kind {
if_chain! { // TODO: Work out a way to put "whatever the imported way of referencing
// Detect and ignore <Foo as Default>::default() because these calls do // this type in this file" rather than a fully-qualified type.
// explicitly name the type. let replacement = format!("{}::default()", cx.tcx.def_path_str(def.did));
if let ExprKind::Call(ref method, ref _args) = expr.kind; span_lint_and_sugg(
if let ExprKind::Path(ref p) = method.kind; cx,
if let QPath::Resolved(Some(_ty), _path) = p; DEFAULT_TRAIT_ACCESS,
then { expr.span,
return; &format!("calling `{}` is more clear than this expression", replacement),
} "try",
} replacement,
Applicability::Unspecified, // First resolve the TODO above
// TODO: Work out a way to put "whatever the imported way of referencing );
// this type in this file" rather than a fully-qualified type.
let expr_ty = cx.typeck_results().expr_ty(expr);
if let ty::Adt(def, ..) = expr_ty.kind {
let replacement = format!("{}::default()", cx.tcx.def_path_str(def.did));
span_lint_and_sugg(
cx,
DEFAULT_TRAIT_ACCESS,
expr.span,
&format!("calling `{}` is more clear than this expression", replacement),
"try",
replacement,
Applicability::Unspecified, // First resolve the TODO above
);
}
},
QPath::TypeRelative(..) | QPath::LangItem(..) => {},
} }
} }
} }