use rustc::hir::*;
use rustc::lint::*;
+use rustc::{declare_lint, lint_array};
+use if_chain::if_chain;
+use rustc::ty::TyKind;
-use crate::utils::{match_def_path, opt_def_id, paths, span_lint_and_sugg};
+use crate::utils::{any_parent_is_automatically_derived, match_def_path, opt_def_id, paths, span_lint_and_sugg};
/// **What it does:** Checks for literal calls to `Default::default()`.
/// ```
declare_clippy_lint! {
pub DEFAULT_TRAIT_ACCESS,
- style,
+ pedantic,
"checks for literal calls to Default::default()"
}
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DefaultTraitAccess {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
if_chain! {
- if let ExprCall(ref path, ..) = expr.node;
- if let ExprPath(ref qpath) = path.node;
+ if let ExprKind::Call(ref path, ..) = expr.node;
+ if !any_parent_is_automatically_derived(cx.tcx, expr.id);
+ if let ExprKind::Path(ref qpath) = path.node;
if let Some(def_id) = opt_def_id(cx.tables.qpath_def(qpath, path.hir_id));
if match_def_path(cx.tcx, def_id, &paths::DEFAULT_TRAIT_METHOD);
then {
match qpath {
QPath::Resolved(..) => {
+ if let ExprKind::Call(ref method, ref _args) = expr.node {
+ if format!("{:?}", method).contains(" as Default>") {
+ return
+ }
+ }
+
+
// 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 replacement = format!("{}::default()", cx.tables.expr_ty(expr));
- span_lint_and_sugg(
- cx,
- DEFAULT_TRAIT_ACCESS,
- expr.span,
- &format!("Calling {} is more clear than this expression", replacement),
- "try",
- replacement);
+ let expr_ty = cx.tables.expr_ty(expr);
+ if let TyKind::Adt(..) = expr_ty.sty {
+ let replacement = format!("{}::default()", expr_ty);
+ span_lint_and_sugg(
+ cx,
+ DEFAULT_TRAIT_ACCESS,
+ expr.span,
+ &format!("Calling {} is more clear than this expression", replacement),
+ "try",
+ replacement);
+ }
},
QPath::TypeRelative(..) => {},
}