-use rustc::hir::*;
-use rustc::lint::*;
-use rustc::{declare_lint, lint_array};
+// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
use if_chain::if_chain;
-use rustc::ty::TypeVariants;
+use rustc::hir::*;
+use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
+use rustc::ty;
+use rustc::{declare_tool_lint, lint_array};
+use rustc_errors::Applicability;
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()`.
///
/// **Why is this bad?** It's more clear to the reader to use the name of the type whose default is
then {
match qpath {
QPath::Resolved(..) => {
+ if_chain! {
+ // Detect and ignore <Foo as Default>::default() because these calls do
+ // explicitly name the type.
+ if let ExprKind::Call(ref method, ref _args) = expr.node;
+ if let ExprKind::Path(ref p) = method.node;
+ if let QPath::Resolved(Some(_ty), _path) = p;
+ then {
+ 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 expr_ty = cx.tables.expr_ty(expr);
- if let TypeVariants::TyAdt(..) = expr_ty.sty {
+ if let ty::Adt(..) = expr_ty.sty {
let replacement = format!("{}::default()", expr_ty);
span_lint_and_sugg(
cx,
expr.span,
&format!("Calling {} is more clear than this expression", replacement),
"try",
- replacement);
+ replacement,
+ Applicability::Unspecified, // First resolve the TODO above
+ );
}
},
QPath::TypeRelative(..) => {},
}
}
- }
+ }
}
}