Skip to content

Commit 401b3d1

Browse files
Use mem::take instead of mem::replace when applicable
`std::mem::take` can be used to replace a value of type `T` with `T::default()` instead of `std::mem::replace`.
1 parent 45196ce commit 401b3d1

File tree

8 files changed

+191
-71
lines changed

8 files changed

+191
-71
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1086,6 +1086,7 @@ Released 2018-09-13
10861086
[`mem_discriminant_non_enum`]: https://rust-lang.github.io/rust-clippy/master/index.html#mem_discriminant_non_enum
10871087
[`mem_forget`]: https://rust-lang.github.io/rust-clippy/master/index.html#mem_forget
10881088
[`mem_replace_option_with_none`]: https://rust-lang.github.io/rust-clippy/master/index.html#mem_replace_option_with_none
1089+
[`mem_replace_with_default`]: https://rust-lang.github.io/rust-clippy/master/index.html#mem_replace_with_default
10891090
[`mem_replace_with_uninit`]: https://rust-lang.github.io/rust-clippy/master/index.html#mem_replace_with_uninit
10901091
[`min_max`]: https://rust-lang.github.io/rust-clippy/master/index.html#min_max
10911092
[`misaligned_transmute`]: https://rust-lang.github.io/rust-clippy/master/index.html#misaligned_transmute

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.
88

9-
[There are 338 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
9+
[There are 339 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
1010

1111
We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you:
1212

clippy_lints/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,7 @@ pub fn register_plugins(store: &mut lint::LintStore, sess: &Session, conf: &Conf
587587
&mem_discriminant::MEM_DISCRIMINANT_NON_ENUM,
588588
&mem_forget::MEM_FORGET,
589589
&mem_replace::MEM_REPLACE_OPTION_WITH_NONE,
590+
&mem_replace::MEM_REPLACE_WITH_DEFAULT,
590591
&mem_replace::MEM_REPLACE_WITH_UNINIT,
591592
&methods::CHARS_LAST_CMP,
592593
&methods::CHARS_NEXT_CMP,
@@ -1575,6 +1576,7 @@ pub fn register_plugins(store: &mut lint::LintStore, sess: &Session, conf: &Conf
15751576
store.register_group(true, "clippy::nursery", Some("clippy_nursery"), vec![
15761577
LintId::of(&attrs::EMPTY_LINE_AFTER_OUTER_ATTR),
15771578
LintId::of(&fallible_impl_from::FALLIBLE_IMPL_FROM),
1579+
LintId::of(&mem_replace::MEM_REPLACE_WITH_DEFAULT),
15781580
LintId::of(&missing_const_for_fn::MISSING_CONST_FOR_FN),
15791581
LintId::of(&mul_add::MANUAL_MUL_ADD),
15801582
LintId::of(&mutex_atomic::MUTEX_INTEGER),

clippy_lints/src/mem_replace.rs

+124-62
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::utils::{
22
match_def_path, match_qpath, paths, snippet_with_applicability, span_help_and_lint, span_lint_and_sugg,
33
};
44
use if_chain::if_chain;
5-
use rustc::hir::{BorrowKind, Expr, ExprKind, Mutability, QPath};
5+
use rustc::hir::{BorrowKind, Expr, ExprKind, HirVec, Mutability, QPath};
66
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
77
use rustc::{declare_lint_pass, declare_tool_lint};
88
use rustc_errors::Applicability;
@@ -66,8 +66,127 @@ declare_clippy_lint! {
6666
"`mem::replace(&mut _, mem::uninitialized())` or `mem::replace(&mut _, mem::zeroed())`"
6767
}
6868

69+
declare_clippy_lint! {
70+
/// **What it does:** Checks for `std::mem::replace` on a value of type
71+
/// `T` with `T::default()`.
72+
///
73+
/// **Why is this bad?** `std::mem` module already has the method `take` to
74+
/// take the current value and replace it with the default value of that type.
75+
///
76+
/// **Known problems:** None.
77+
///
78+
/// **Example:**
79+
/// ```rust
80+
/// let mut text = String::from("foo");
81+
/// let replaced = std::mem::replace(&mut text, String::default());
82+
/// ```
83+
/// Is better expressed with:
84+
/// ```rust
85+
/// let mut text = String::from("foo");
86+
/// let taken = std::mem::take(&mut text);
87+
/// ```
88+
pub MEM_REPLACE_WITH_DEFAULT,
89+
nursery,
90+
"replacing a value of type `T` with `T::default()` instead of using `std::mem::take`"
91+
}
92+
6993
declare_lint_pass!(MemReplace =>
70-
[MEM_REPLACE_OPTION_WITH_NONE, MEM_REPLACE_WITH_UNINIT]);
94+
[MEM_REPLACE_OPTION_WITH_NONE, MEM_REPLACE_WITH_UNINIT, MEM_REPLACE_WITH_DEFAULT]);
95+
96+
fn check_replace_option_with_none(cx: &LateContext<'_, '_>, expr: &'_ Expr, args: &HirVec<Expr>) {
97+
if let ExprKind::Path(ref replacement_qpath) = args[1].kind {
98+
// Check that second argument is `Option::None`
99+
if match_qpath(replacement_qpath, &paths::OPTION_NONE) {
100+
// Since this is a late pass (already type-checked),
101+
// and we already know that the second argument is an
102+
// `Option`, we do not need to check the first
103+
// argument's type. All that's left is to get
104+
// replacee's path.
105+
let replaced_path = match args[0].kind {
106+
ExprKind::AddrOf(BorrowKind::Ref, Mutability::Mutable, ref replaced) => {
107+
if let ExprKind::Path(QPath::Resolved(None, ref replaced_path)) = replaced.kind {
108+
replaced_path
109+
} else {
110+
return;
111+
}
112+
},
113+
ExprKind::Path(QPath::Resolved(None, ref replaced_path)) => replaced_path,
114+
_ => return,
115+
};
116+
117+
let mut applicability = Applicability::MachineApplicable;
118+
span_lint_and_sugg(
119+
cx,
120+
MEM_REPLACE_OPTION_WITH_NONE,
121+
expr.span,
122+
"replacing an `Option` with `None`",
123+
"consider `Option::take()` instead",
124+
format!(
125+
"{}.take()",
126+
snippet_with_applicability(cx, replaced_path.span, "", &mut applicability)
127+
),
128+
applicability,
129+
);
130+
}
131+
}
132+
}
133+
134+
fn check_replace_with_uninit(cx: &LateContext<'_, '_>, expr: &'_ Expr, args: &HirVec<Expr>) {
135+
if let ExprKind::Call(ref repl_func, ref repl_args) = args[1].kind {
136+
if_chain! {
137+
if repl_args.is_empty();
138+
if let ExprKind::Path(ref repl_func_qpath) = repl_func.kind;
139+
if let Some(repl_def_id) = cx.tables.qpath_res(repl_func_qpath, repl_func.hir_id).opt_def_id();
140+
then {
141+
if match_def_path(cx, repl_def_id, &paths::MEM_UNINITIALIZED) {
142+
span_help_and_lint(
143+
cx,
144+
MEM_REPLACE_WITH_UNINIT,
145+
expr.span,
146+
"replacing with `mem::uninitialized()`",
147+
"consider using the `take_mut` crate instead",
148+
);
149+
} else if match_def_path(cx, repl_def_id, &paths::MEM_ZEROED) &&
150+
!cx.tables.expr_ty(&args[1]).is_primitive() {
151+
span_help_and_lint(
152+
cx,
153+
MEM_REPLACE_WITH_UNINIT,
154+
expr.span,
155+
"replacing with `mem::zeroed()`",
156+
"consider using a default value or the `take_mut` crate instead",
157+
);
158+
}
159+
}
160+
}
161+
}
162+
}
163+
164+
fn check_replace_with_default(cx: &LateContext<'_, '_>, expr: &'_ Expr, args: &HirVec<Expr>) {
165+
if let ExprKind::Call(ref repl_func, ref repl_args) = args[1].kind {
166+
if_chain! {
167+
if repl_args.is_empty();
168+
if let ExprKind::Path(ref repl_func_qpath) = repl_func.kind;
169+
if let Some(repl_def_id) = cx.tables.qpath_res(repl_func_qpath, repl_func.hir_id).opt_def_id();
170+
if match_def_path(cx, repl_def_id, &paths::DEFAULT_TRAIT_METHOD);
171+
then {
172+
let mut applicability = Applicability::MachineApplicable;
173+
174+
span_lint_and_sugg(
175+
cx,
176+
MEM_REPLACE_WITH_DEFAULT,
177+
expr.span,
178+
"replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take`",
179+
"consider using",
180+
format!(
181+
"std::mem::take({})",
182+
snippet_with_applicability(cx, args[0].span, "", &mut applicability)
183+
),
184+
applicability,
185+
);
186+
}
187+
}
188+
}
189+
}
71190

72191
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MemReplace {
73192
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
@@ -79,67 +198,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MemReplace {
79198
if let Some(def_id) = cx.tables.qpath_res(func_qpath, func.hir_id).opt_def_id();
80199
if match_def_path(cx, def_id, &paths::MEM_REPLACE);
81200

82-
// Check that second argument is `Option::None`
83201
then {
84-
if let ExprKind::Path(ref replacement_qpath) = func_args[1].kind {
85-
if match_qpath(replacement_qpath, &paths::OPTION_NONE) {
86-
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
91-
// replacee's path.
92-
let replaced_path = match func_args[0].kind {
93-
ExprKind::AddrOf(BorrowKind::Ref, Mutability::Mutable, ref replaced) => {
94-
if let ExprKind::Path(QPath::Resolved(None, ref replaced_path)) = replaced.kind {
95-
replaced_path
96-
} else {
97-
return
98-
}
99-
},
100-
ExprKind::Path(QPath::Resolved(None, ref replaced_path)) => replaced_path,
101-
_ => return,
102-
};
103-
104-
let mut applicability = Applicability::MachineApplicable;
105-
span_lint_and_sugg(
106-
cx,
107-
MEM_REPLACE_OPTION_WITH_NONE,
108-
expr.span,
109-
"replacing an `Option` with `None`",
110-
"consider `Option::take()` instead",
111-
format!("{}.take()", snippet_with_applicability(cx, replaced_path.span, "", &mut applicability)),
112-
applicability,
113-
);
114-
}
115-
}
116-
if let ExprKind::Call(ref repl_func, ref repl_args) = func_args[1].kind {
117-
if_chain! {
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();
121-
then {
122-
if match_def_path(cx, repl_def_id, &paths::MEM_UNINITIALIZED) {
123-
span_help_and_lint(
124-
cx,
125-
MEM_REPLACE_WITH_UNINIT,
126-
expr.span,
127-
"replacing with `mem::uninitialized()`",
128-
"consider using the `take_mut` crate instead",
129-
);
130-
} else if match_def_path(cx, repl_def_id, &paths::MEM_ZEROED) &&
131-
!cx.tables.expr_ty(&func_args[1]).is_primitive() {
132-
span_help_and_lint(
133-
cx,
134-
MEM_REPLACE_WITH_UNINIT,
135-
expr.span,
136-
"replacing with `mem::zeroed()`",
137-
"consider using a default value or the `take_mut` crate instead",
138-
);
139-
}
140-
}
141-
}
142-
}
202+
check_replace_option_with_none(cx, expr, &func_args);
203+
check_replace_with_uninit(cx, expr, &func_args);
204+
check_replace_with_default(cx, expr, &func_args);
143205
}
144206
}
145207
}

src/lintlist/mod.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub use lint::Lint;
66
pub use lint::LINT_LEVELS;
77

88
// begin lint list, do not remove this comment, it’s used in `update_lints`
9-
pub const ALL_LINTS: [Lint; 338] = [
9+
pub const ALL_LINTS: [Lint; 339] = [
1010
Lint {
1111
name: "absurd_extreme_comparisons",
1212
group: "correctness",
@@ -1092,6 +1092,13 @@ pub const ALL_LINTS: [Lint; 338] = [
10921092
deprecation: None,
10931093
module: "mem_replace",
10941094
},
1095+
Lint {
1096+
name: "mem_replace_with_default",
1097+
group: "nursery",
1098+
desc: "replacing a value of type `T` with `T::default()` instead of using `std::mem::take`",
1099+
deprecation: None,
1100+
module: "mem_replace",
1101+
},
10951102
Lint {
10961103
name: "mem_replace_with_uninit",
10971104
group: "correctness",

tests/ui/mem_replace.fixed

+19-2
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,30 @@
99

1010
// run-rustfix
1111
#![allow(unused_imports)]
12-
#![warn(clippy::all, clippy::style, clippy::mem_replace_option_with_none)]
12+
#![warn(
13+
clippy::all,
14+
clippy::style,
15+
clippy::mem_replace_option_with_none,
16+
clippy::mem_replace_with_default
17+
)]
1318

1419
use std::mem;
1520

16-
fn main() {
21+
fn replace_option_with_none() {
1722
let mut an_option = Some(1);
1823
let _ = an_option.take();
1924
let an_option = &mut Some(1);
2025
let _ = an_option.take();
2126
}
27+
28+
fn replace_with_default() {
29+
let mut s = String::from("foo");
30+
let _ = std::mem::take(&mut s);
31+
let s = &mut String::from("foo");
32+
let _ = std::mem::take(s);
33+
}
34+
35+
fn main() {
36+
replace_option_with_none();
37+
replace_with_default();
38+
}

tests/ui/mem_replace.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,30 @@
99

1010
// run-rustfix
1111
#![allow(unused_imports)]
12-
#![warn(clippy::all, clippy::style, clippy::mem_replace_option_with_none)]
12+
#![warn(
13+
clippy::all,
14+
clippy::style,
15+
clippy::mem_replace_option_with_none,
16+
clippy::mem_replace_with_default
17+
)]
1318

1419
use std::mem;
1520

16-
fn main() {
21+
fn replace_option_with_none() {
1722
let mut an_option = Some(1);
1823
let _ = mem::replace(&mut an_option, None);
1924
let an_option = &mut Some(1);
2025
let _ = mem::replace(an_option, None);
2126
}
27+
28+
fn replace_with_default() {
29+
let mut s = String::from("foo");
30+
let _ = std::mem::replace(&mut s, String::default());
31+
let s = &mut String::from("foo");
32+
let _ = std::mem::replace(s, String::default());
33+
}
34+
35+
fn main() {
36+
replace_option_with_none();
37+
replace_with_default();
38+
}

tests/ui/mem_replace.stderr

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
11
error: replacing an `Option` with `None`
2-
--> $DIR/mem_replace.rs:18:13
2+
--> $DIR/mem_replace.rs:23:13
33
|
44
LL | let _ = mem::replace(&mut an_option, None);
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Option::take()` instead: `an_option.take()`
66
|
77
= note: `-D clippy::mem-replace-option-with-none` implied by `-D warnings`
88

99
error: replacing an `Option` with `None`
10-
--> $DIR/mem_replace.rs:20:13
10+
--> $DIR/mem_replace.rs:25:13
1111
|
1212
LL | let _ = mem::replace(an_option, None);
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Option::take()` instead: `an_option.take()`
1414

15-
error: aborting due to 2 previous errors
15+
error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take`
16+
--> $DIR/mem_replace.rs:30:13
17+
|
18+
LL | let _ = std::mem::replace(&mut s, String::default());
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut s)`
20+
|
21+
= note: `-D clippy::mem-replace-with-default` implied by `-D warnings`
22+
23+
error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take`
24+
--> $DIR/mem_replace.rs:32:13
25+
|
26+
LL | let _ = std::mem::replace(s, String::default());
27+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(s)`
28+
29+
error: aborting due to 4 previous errors
1630

0 commit comments

Comments
 (0)