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