2018-07-19 08:00:54 +00:00
|
|
|
use if_chain::if_chain;
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc::ty;
|
|
|
|
use rustc_errors::Applicability;
|
2020-01-06 16:39:50 +00:00
|
|
|
use rustc_hir::*;
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2018-06-14 07:57:27 +00:00
|
|
|
|
2019-05-14 08:06:21 +00:00
|
|
|
use crate::utils::{any_parent_is_automatically_derived, match_def_path, paths, span_lint_and_sugg};
|
2018-06-14 07:57:27 +00:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **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
|
|
|
|
/// being gotten than the generic `Default`.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// // Bad
|
|
|
|
/// let s: String = Default::default();
|
|
|
|
///
|
|
|
|
/// // Good
|
|
|
|
/// let s = String::default();
|
|
|
|
/// ```
|
2018-06-14 07:57:27 +00:00
|
|
|
pub DEFAULT_TRAIT_ACCESS,
|
2018-06-25 18:39:48 +00:00
|
|
|
pedantic,
|
2020-01-06 06:30:43 +00:00
|
|
|
"checks for literal calls to `Default::default()`"
|
2018-06-14 07:57:27 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
declare_lint_pass!(DefaultTraitAccess => [DEFAULT_TRAIT_ACCESS]);
|
2018-06-14 07:57:27 +00:00
|
|
|
|
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DefaultTraitAccess {
|
2019-12-27 07:12:26 +00:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
|
2018-06-14 07:57:27 +00:00
|
|
|
if_chain! {
|
2019-09-27 15:16:06 +00:00
|
|
|
if let ExprKind::Call(ref path, ..) = expr.kind;
|
2019-02-24 18:43:15 +00:00
|
|
|
if !any_parent_is_automatically_derived(cx.tcx, expr.hir_id);
|
2019-09-27 15:16:06 +00:00
|
|
|
if let ExprKind::Path(ref qpath) = path.kind;
|
2019-05-04 00:03:12 +00:00
|
|
|
if let Some(def_id) = cx.tables.qpath_res(qpath, path.hir_id).opt_def_id();
|
2019-05-17 21:53:54 +00:00
|
|
|
if match_def_path(cx, def_id, &paths::DEFAULT_TRAIT_METHOD);
|
2018-11-27 20:49:09 +00:00
|
|
|
then {
|
|
|
|
match qpath {
|
|
|
|
QPath::Resolved(..) => {
|
|
|
|
if_chain! {
|
|
|
|
// Detect and ignore <Foo as Default>::default() because these calls do
|
|
|
|
// explicitly name the type.
|
2019-09-27 15:16:06 +00:00
|
|
|
if let ExprKind::Call(ref method, ref _args) = expr.kind;
|
|
|
|
if let ExprKind::Path(ref p) = method.kind;
|
2018-11-27 20:49:09 +00:00
|
|
|
if let QPath::Resolved(Some(_ty), _path) = p;
|
|
|
|
then {
|
|
|
|
return;
|
|
|
|
}
|
2018-11-27 20:14:15 +00:00
|
|
|
}
|
2018-11-27 20:49:09 +00:00
|
|
|
|
|
|
|
// 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);
|
2019-09-26 09:03:36 +00:00
|
|
|
if let ty::Adt(..) = expr_ty.kind {
|
2018-11-27 20:49:09 +00:00
|
|
|
let replacement = format!("{}::default()", expr_ty);
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
DEFAULT_TRAIT_ACCESS,
|
|
|
|
expr.span,
|
2020-01-06 06:30:43 +00:00
|
|
|
&format!("Calling `{}` is more clear than this expression", replacement),
|
2018-11-27 20:49:09 +00:00
|
|
|
"try",
|
|
|
|
replacement,
|
|
|
|
Applicability::Unspecified, // First resolve the TODO above
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
QPath::TypeRelative(..) => {},
|
|
|
|
}
|
|
|
|
}
|
2018-11-27 20:14:15 +00:00
|
|
|
}
|
2018-06-14 07:57:27 +00:00
|
|
|
}
|
|
|
|
}
|