]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/default_trait_access.rs
Auto merge of #5334 - flip1995:backport_remerge, r=flip1995
[rust.git] / clippy_lints / src / default_trait_access.rs
1 use if_chain::if_chain;
2 use rustc::ty;
3 use rustc_errors::Applicability;
4 use rustc_hir::{Expr, ExprKind, QPath};
5 use rustc_lint::{LateContext, LateLintPass};
6 use rustc_session::{declare_lint_pass, declare_tool_lint};
7
8 use crate::utils::{any_parent_is_automatically_derived, match_def_path, paths, span_lint_and_sugg};
9
10 declare_clippy_lint! {
11     /// **What it does:** Checks for literal calls to `Default::default()`.
12     ///
13     /// **Why is this bad?** It's more clear to the reader to use the name of the type whose default is
14     /// being gotten than the generic `Default`.
15     ///
16     /// **Known problems:** None.
17     ///
18     /// **Example:**
19     /// ```rust
20     /// // Bad
21     /// let s: String = Default::default();
22     ///
23     /// // Good
24     /// let s = String::default();
25     /// ```
26     pub DEFAULT_TRAIT_ACCESS,
27     pedantic,
28     "checks for literal calls to `Default::default()`"
29 }
30
31 declare_lint_pass!(DefaultTraitAccess => [DEFAULT_TRAIT_ACCESS]);
32
33 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DefaultTraitAccess {
34     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
35         if_chain! {
36             if let ExprKind::Call(ref path, ..) = expr.kind;
37             if !any_parent_is_automatically_derived(cx.tcx, expr.hir_id);
38             if let ExprKind::Path(ref qpath) = path.kind;
39             if let Some(def_id) = cx.tables.qpath_res(qpath, path.hir_id).opt_def_id();
40             if match_def_path(cx, def_id, &paths::DEFAULT_TRAIT_METHOD);
41             then {
42                 match qpath {
43                     QPath::Resolved(..) => {
44                         if_chain! {
45                             // Detect and ignore <Foo as Default>::default() because these calls do
46                             // explicitly name the type.
47                             if let ExprKind::Call(ref method, ref _args) = expr.kind;
48                             if let ExprKind::Path(ref p) = method.kind;
49                             if let QPath::Resolved(Some(_ty), _path) = p;
50                             then {
51                                 return;
52                             }
53                         }
54
55                         // TODO: Work out a way to put "whatever the imported way of referencing
56                         // this type in this file" rather than a fully-qualified type.
57                         let expr_ty = cx.tables.expr_ty(expr);
58                         if let ty::Adt(..) = expr_ty.kind {
59                             let replacement = format!("{}::default()", expr_ty);
60                             span_lint_and_sugg(
61                                 cx,
62                                 DEFAULT_TRAIT_ACCESS,
63                                 expr.span,
64                                 &format!("Calling `{}` is more clear than this expression", replacement),
65                                 "try",
66                                 replacement,
67                                 Applicability::Unspecified, // First resolve the TODO above
68                             );
69                          }
70                     },
71                     QPath::TypeRelative(..) => {},
72                 }
73             }
74         }
75     }
76 }