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