2 match_def_path, match_qpath, paths, snippet_with_applicability, span_help_and_lint, span_lint_and_sugg,
4 use if_chain::if_chain;
5 use rustc::hir::{Expr, ExprKind, Mutability, QPath};
6 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
7 use rustc::{declare_lint_pass, declare_tool_lint};
8 use rustc_errors::Applicability;
10 declare_clippy_lint! {
11 /// **What it does:** Checks for `mem::replace()` on an `Option` with
14 /// **Why is this bad?** `Option` already has the method `take()` for
15 /// taking its current value (Some(..) or None) and replacing it with
18 /// **Known problems:** None.
24 /// let mut an_option = Some(0);
25 /// let replaced = mem::replace(&mut an_option, None);
27 /// Is better expressed with:
29 /// let mut an_option = Some(0);
30 /// let taken = an_option.take();
32 pub MEM_REPLACE_OPTION_WITH_NONE,
34 "replacing an `Option` with `None` instead of `take()`"
37 declare_clippy_lint! {
38 /// **What it does:** Checks for `mem::replace(&mut _, mem::uninitialized())`
39 /// and `mem::replace(&mut _, mem::zeroed())`.
41 /// **Why is this bad?** This will lead to undefined behavior even if the
42 /// value is overwritten later, because the uninitialized value may be
43 /// observed in the case of a panic.
45 /// **Known problems:** None.
51 ///# fn may_panic(v: Vec<i32>) -> Vec<i32> { v }
53 /// #[allow(deprecated, invalid_value)]
54 /// fn myfunc (v: &mut Vec<i32>) {
55 /// let taken_v = unsafe { mem::replace(v, mem::uninitialized()) };
56 /// let new_v = may_panic(taken_v); // undefined behavior on panic
57 /// mem::forget(mem::replace(v, new_v));
61 /// The [take_mut](https://docs.rs/take_mut) crate offers a sound solution,
62 /// at the cost of either lazily creating a replacement value or aborting
63 /// on panic, to ensure that the uninitialized value cannot be observed.
64 pub MEM_REPLACE_WITH_UNINIT,
66 "`mem::replace(&mut _, mem::uninitialized())` or `mem::replace(&mut _, mem::zeroed())`"
69 declare_lint_pass!(MemReplace =>
70 [MEM_REPLACE_OPTION_WITH_NONE, MEM_REPLACE_WITH_UNINIT]);
72 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MemReplace {
73 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
75 // Check that `expr` is a call to `mem::replace()`
76 if let ExprKind::Call(ref func, ref func_args) = expr.kind;
77 if func_args.len() == 2;
78 if let ExprKind::Path(ref func_qpath) = func.kind;
79 if let Some(def_id) = cx.tables.qpath_res(func_qpath, func.hir_id).opt_def_id();
80 if match_def_path(cx, def_id, &paths::MEM_REPLACE);
82 // Check that second argument is `Option::None`
84 if let ExprKind::Path(ref replacement_qpath) = func_args[1].kind {
85 if match_qpath(replacement_qpath, &paths::OPTION_NONE) {
87 // Since this is a late pass (already type-checked),
88 // and we already know that the second argument is an
89 // `Option`, we do not need to check the first
90 // argument's type. All that's left is to get
92 let replaced_path = match func_args[0].kind {
93 ExprKind::AddrOf(Mutability::Mutable, ref replaced) => {
94 if let ExprKind::Path(QPath::Resolved(None, ref replaced_path)) = replaced.kind {
100 ExprKind::Path(QPath::Resolved(None, ref replaced_path)) => replaced_path,
104 let mut applicability = Applicability::MachineApplicable;
107 MEM_REPLACE_OPTION_WITH_NONE,
109 "replacing an `Option` with `None`",
110 "consider `Option::take()` instead",
111 format!("{}.take()", snippet_with_applicability(cx, replaced_path.span, "", &mut applicability)),
116 if let ExprKind::Call(ref repl_func, ref repl_args) = func_args[1].kind {
118 if repl_args.is_empty();
119 if let ExprKind::Path(ref repl_func_qpath) = repl_func.kind;
120 if let Some(repl_def_id) = cx.tables.qpath_res(repl_func_qpath, repl_func.hir_id).opt_def_id();
122 if match_def_path(cx, repl_def_id, &paths::MEM_UNINITIALIZED) {
125 MEM_REPLACE_WITH_UNINIT,
127 "replacing with `mem::uninitialized()`",
128 "consider using the `take_mut` crate instead",
130 } else if match_def_path(cx, repl_def_id, &paths::MEM_ZEROED) &&
131 !cx.tables.expr_ty(&func_args[1]).is_primitive() {
134 MEM_REPLACE_WITH_UNINIT,
136 "replacing with `mem::zeroed()`",
137 "consider using a default value or the `take_mut` crate instead",