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