]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/mem_replace.rs
Auto merge of #3946 - rchaser53:issue-3920, r=flip1995
[rust.git] / clippy_lints / src / mem_replace.rs
1 use crate::utils::{match_def_path, match_qpath, paths, snippet_with_applicability, span_lint_and_sugg};
2 use if_chain::if_chain;
3 use rustc::hir::{Expr, ExprKind, MutMutable, QPath};
4 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
5 use rustc::{declare_tool_lint, lint_array};
6 use rustc_errors::Applicability;
7
8 declare_clippy_lint! {
9     /// **What it does:** Checks for `mem::replace()` on an `Option` with
10     /// `None`.
11     ///
12     /// **Why is this bad?** `Option` already has the method `take()` for
13     /// taking its current value (Some(..) or None) and replacing it with
14     /// `None`.
15     ///
16     /// **Known problems:** None.
17     ///
18     /// **Example:**
19     /// ```rust
20     /// use std::mem;
21     ///
22     /// let mut an_option = Some(0);
23     /// let replaced = mem::replace(&mut an_option, None);
24     /// ```
25     /// Is better expressed with:
26     /// ```rust
27     /// let mut an_option = Some(0);
28     /// let taken = an_option.take();
29     /// ```
30     pub MEM_REPLACE_OPTION_WITH_NONE,
31     style,
32     "replacing an `Option` with `None` instead of `take()`"
33 }
34
35 pub struct MemReplace;
36
37 impl LintPass for MemReplace {
38     fn get_lints(&self) -> LintArray {
39         lint_array![MEM_REPLACE_OPTION_WITH_NONE]
40     }
41
42     fn name(&self) -> &'static str {
43         "MemReplace"
44     }
45 }
46
47 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MemReplace {
48     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
49         if_chain! {
50             // Check that `expr` is a call to `mem::replace()`
51             if let ExprKind::Call(ref func, ref func_args) = expr.node;
52             if func_args.len() == 2;
53             if let ExprKind::Path(ref func_qpath) = func.node;
54             if let Some(def_id) = cx.tables.qpath_def(func_qpath, func.hir_id).opt_def_id();
55             if match_def_path(cx.tcx, def_id, &paths::MEM_REPLACE);
56
57             // Check that second argument is `Option::None`
58             if let ExprKind::Path(ref replacement_qpath) = func_args[1].node;
59             if match_qpath(replacement_qpath, &paths::OPTION_NONE);
60
61             then {
62                 // Since this is a late pass (already type-checked),
63                 // and we already know that the second argument is an
64                 // `Option`, we do not need to check the first
65                 // argument's type. All that's left is to get
66                 // replacee's path.
67                 let replaced_path = match func_args[0].node {
68                     ExprKind::AddrOf(MutMutable, ref replaced) => {
69                         if let ExprKind::Path(QPath::Resolved(None, ref replaced_path)) = replaced.node {
70                             replaced_path
71                         } else {
72                             return
73                         }
74                     },
75                     ExprKind::Path(QPath::Resolved(None, ref replaced_path)) => replaced_path,
76                     _ => return,
77                 };
78
79                 let mut applicability = Applicability::MachineApplicable;
80                 span_lint_and_sugg(
81                     cx,
82                     MEM_REPLACE_OPTION_WITH_NONE,
83                     expr.span,
84                     "replacing an `Option` with `None`",
85                     "consider `Option::take()` instead",
86                     format!("{}.take()", snippet_with_applicability(cx, replaced_path.span, "", &mut applicability)),
87                     applicability,
88                 );
89             }
90         }
91     }
92 }