Skip to content

Commit 2f53aaa

Browse files
committed
mem_replace: match on path.
1 parent 12c7bc1 commit 2f53aaa

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

clippy_lints/src/mem_replace.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::rustc::hir::{Expr, ExprKind, MutMutable, QPath};
22
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
33
use crate::rustc::{declare_tool_lint, lint_array};
4-
use crate::utils::{match_def_path, match_qpath, match_type, opt_def_id, paths, snippet, span_lint_and_sugg};
4+
use crate::utils::{match_def_path, match_qpath, opt_def_id, paths, snippet, span_lint_and_sugg};
55
use if_chain::if_chain;
66

77
/// **What it does:** Checks for `mem::replace()` on an `Option` with
@@ -40,25 +40,42 @@ impl LintPass for MemReplace {
4040
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MemReplace {
4141
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
4242
if_chain! {
43+
// Check that `expr` is a call to `mem::replace()`
4344
if let ExprKind::Call(ref func, ref func_args) = expr.node;
4445
if func_args.len() == 2;
4546
if let ExprKind::Path(ref func_qpath) = func.node;
4647
if let Some(def_id) = opt_def_id(cx.tables.qpath_def(func_qpath, func.hir_id));
4748
if match_def_path(cx.tcx, def_id, &paths::MEM_REPLACE);
48-
if let ExprKind::AddrOf(MutMutable, ref replaced) = func_args[0].node;
49-
if match_type(cx, cx.tables.expr_ty(replaced), &paths::OPTION);
49+
50+
// Check that second argument is `Option::None`
5051
if let ExprKind::Path(ref replacement_qpath) = func_args[1].node;
5152
if match_qpath(replacement_qpath, &paths::OPTION_NONE);
52-
if let ExprKind::Path(QPath::Resolved(None, ref replaced_path)) = replaced.node;
53+
5354
then {
54-
let sugg = format!("{}.take()", snippet(cx, replaced_path.span, ""));
55+
// Since this is a late pass (already type-checked),
56+
// and we already know that the second argument is an
57+
// `Option`, we do not need to check if the first
58+
// argument's type. All that's left is to get
59+
// replacee's path.
60+
let replaced_path = match func_args[0].node {
61+
ExprKind::AddrOf(MutMutable, ref replaced) => {
62+
if let ExprKind::Path(QPath::Resolved(None, ref replaced_path)) = replaced.node {
63+
replaced_path
64+
} else {
65+
return
66+
}
67+
},
68+
ExprKind::Path(QPath::Resolved(None, ref replaced_path)) => replaced_path,
69+
_ => return,
70+
};
71+
5572
span_lint_and_sugg(
5673
cx,
5774
MEM_REPLACE_OPTION_WITH_NONE,
5875
expr.span,
5976
"replacing an `Option` with `None`",
6077
"consider `Option::take()` instead",
61-
sugg
78+
format!("{}.take()", snippet(cx, replaced_path.span, ""))
6279
);
6380
}
6481
}

tests/ui/mem_replace.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ use std::mem;
66
fn main() {
77
let mut an_option = Some(1);
88
let _ = mem::replace(&mut an_option, None);
9+
let an_option = &mut Some(1);
10+
let _ = mem::replace(an_option, None);
911
}

tests/ui/mem_replace.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,11 @@ error: replacing an `Option` with `None`
66
|
77
= note: `-D clippy::mem-replace-option-with-none` implied by `-D warnings`
88

9-
error: aborting due to previous error
9+
error: replacing an `Option` with `None`
10+
--> $DIR/mem_replace.rs:10:13
11+
|
12+
10 | let _ = mem::replace(an_option, None);
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Option::take()` instead: `an_option.take()`
14+
15+
error: aborting due to 2 previous errors
1016

0 commit comments

Comments
 (0)