Skip to content

Commit 6de91d5

Browse files
committed
New lint: mem_replace_option_with_some
`mem::replace(opt, Some(v))` can be replaced by `opt.replace(v)`.
1 parent ff87bea commit 6de91d5

File tree

9 files changed

+125
-3
lines changed

9 files changed

+125
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5812,6 +5812,7 @@ Released 2018-09-13
58125812
[`mem_discriminant_non_enum`]: https://rust-lang.github.io/rust-clippy/master/index.html#mem_discriminant_non_enum
58135813
[`mem_forget`]: https://rust-lang.github.io/rust-clippy/master/index.html#mem_forget
58145814
[`mem_replace_option_with_none`]: https://rust-lang.github.io/rust-clippy/master/index.html#mem_replace_option_with_none
5815+
[`mem_replace_option_with_some`]: https://rust-lang.github.io/rust-clippy/master/index.html#mem_replace_option_with_some
58155816
[`mem_replace_with_default`]: https://rust-lang.github.io/rust-clippy/master/index.html#mem_replace_with_default
58165817
[`mem_replace_with_uninit`]: https://rust-lang.github.io/rust-clippy/master/index.html#mem_replace_with_uninit
58175818
[`min_ident_chars`]: https://rust-lang.github.io/rust-clippy/master/index.html#min_ident_chars

book/src/lint_configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,7 @@ The minimum rust version that the project supports. Defaults to the `rust-versio
764764
* [`map_unwrap_or`](https://rust-lang.github.io/rust-clippy/master/index.html#map_unwrap_or)
765765
* [`map_with_unused_argument_over_ranges`](https://rust-lang.github.io/rust-clippy/master/index.html#map_with_unused_argument_over_ranges)
766766
* [`match_like_matches_macro`](https://rust-lang.github.io/rust-clippy/master/index.html#match_like_matches_macro)
767+
* [`mem_replace_option_with_some`](https://rust-lang.github.io/rust-clippy/master/index.html#mem_replace_option_with_some)
767768
* [`mem_replace_with_default`](https://rust-lang.github.io/rust-clippy/master/index.html#mem_replace_with_default)
768769
* [`missing_const_for_fn`](https://rust-lang.github.io/rust-clippy/master/index.html#missing_const_for_fn)
769770
* [`needless_borrow`](https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow)

clippy_config/src/conf.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,7 @@ define_Conf! {
633633
map_unwrap_or,
634634
map_with_unused_argument_over_ranges,
635635
match_like_matches_macro,
636+
mem_replace_option_with_some,
636637
mem_replace_with_default,
637638
missing_const_for_fn,
638639
needless_borrow,

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ pub static LINTS: &[&crate::LintInfo] = &[
362362
crate::matches::WILDCARD_ENUM_MATCH_ARM_INFO,
363363
crate::matches::WILDCARD_IN_OR_PATTERNS_INFO,
364364
crate::mem_replace::MEM_REPLACE_OPTION_WITH_NONE_INFO,
365+
crate::mem_replace::MEM_REPLACE_OPTION_WITH_SOME_INFO,
365366
crate::mem_replace::MEM_REPLACE_WITH_DEFAULT_INFO,
366367
crate::mem_replace::MEM_REPLACE_WITH_UNINIT_INFO,
367368
crate::methods::BIND_INSTEAD_OF_MAP_INFO,

clippy_lints/src/mem_replace.rs

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use clippy_utils::{
88
is_default_equivalent, is_expr_used_or_unified, is_res_lang_ctor, path_res, peel_ref_operators, std_or_core,
99
};
1010
use rustc_errors::Applicability;
11-
use rustc_hir::LangItem::OptionNone;
11+
use rustc_hir::LangItem::{OptionNone, OptionSome};
1212
use rustc_hir::{Expr, ExprKind};
1313
use rustc_lint::{LateContext, LateLintPass};
1414
use rustc_session::impl_lint_pass;
@@ -43,6 +43,33 @@ declare_clippy_lint! {
4343
"replacing an `Option` with `None` instead of `take()`"
4444
}
4545

46+
declare_clippy_lint! {
47+
/// ### What it does
48+
/// Checks for `mem::replace()` on an `Option` with `Some(…)`.
49+
///
50+
/// ### Why is this bad?
51+
/// `Option` already has the method `replace()` for
52+
/// taking its current value (Some(…) or None) and replacing it with
53+
/// `Some(…)`.
54+
///
55+
/// ### Example
56+
/// ```no_run
57+
/// use std::mem;
58+
///
59+
/// let mut an_option = Some(0);
60+
/// let replaced = mem::replace(&mut an_option, Some(1));
61+
/// ```
62+
/// Is better expressed with:
63+
/// ```no_run
64+
/// let mut an_option = Some(0);
65+
/// let taken = an_option.replace(Some(1));
66+
/// ```
67+
#[clippy::version = "1.86.0"]
68+
pub MEM_REPLACE_OPTION_WITH_SOME,
69+
style,
70+
"replacing an `Option` with `Some` instead of `replace()`"
71+
}
72+
4673
declare_clippy_lint! {
4774
/// ### What it does
4875
/// Checks for `mem::replace(&mut _, mem::uninitialized())`
@@ -101,7 +128,7 @@ declare_clippy_lint! {
101128
}
102129

103130
impl_lint_pass!(MemReplace =>
104-
[MEM_REPLACE_OPTION_WITH_NONE, MEM_REPLACE_WITH_UNINIT, MEM_REPLACE_WITH_DEFAULT]);
131+
[MEM_REPLACE_OPTION_WITH_NONE, MEM_REPLACE_OPTION_WITH_SOME, MEM_REPLACE_WITH_UNINIT, MEM_REPLACE_WITH_DEFAULT]);
105132

106133
fn check_replace_option_with_none(cx: &LateContext<'_>, dest: &Expr<'_>, expr_span: Span) {
107134
// Since this is a late pass (already type-checked),
@@ -125,6 +152,24 @@ fn check_replace_option_with_none(cx: &LateContext<'_>, dest: &Expr<'_>, expr_sp
125152
);
126153
}
127154

155+
fn check_replace_option_with_some(cx: &LateContext<'_>, src: &Expr<'_>, dest: &Expr<'_>, expr_span: Span) {
156+
let sugg_expr = peel_ref_operators(cx, dest);
157+
let mut applicability = Applicability::MachineApplicable;
158+
span_lint_and_sugg(
159+
cx,
160+
MEM_REPLACE_OPTION_WITH_SOME,
161+
expr_span,
162+
"replacing an `Option` with `Some(..)`",
163+
"consider `Option::replace()` instead",
164+
format!(
165+
"{}.replace({})",
166+
Sugg::hir_with_context(cx, sugg_expr, expr_span.ctxt(), "_", &mut applicability).maybe_par(),
167+
snippet_with_applicability(cx, src.span, "_", &mut applicability)
168+
),
169+
applicability,
170+
);
171+
}
172+
128173
fn check_replace_with_uninit(cx: &LateContext<'_>, src: &Expr<'_>, dest: &Expr<'_>, expr_span: Span) {
129174
if let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(src.hir_id)
130175
// check if replacement is mem::MaybeUninit::uninit().assume_init()
@@ -238,6 +283,13 @@ impl<'tcx> LateLintPass<'tcx> for MemReplace {
238283
} else if self.msrv.meets(msrvs::MEM_TAKE) && is_expr_used_or_unified(cx.tcx, expr) {
239284
check_replace_with_default(cx, src, dest, expr.span);
240285
}
286+
// Check that second argument is `Option::Some`
287+
if self.msrv.meets(msrvs::OPTION_REPLACE)
288+
&& let ExprKind::Call(src_func, [src_arg]) = src.kind
289+
&& is_res_lang_ctor(cx, path_res(cx, src_func), OptionSome)
290+
{
291+
check_replace_option_with_some(cx, src_arg, dest, expr.span);
292+
}
241293
check_replace_with_uninit(cx, src, dest, expr.span);
242294
}
243295
}

clippy_utils/src/msrvs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ msrv_aliases! {
5858
1,35,0 { OPTION_COPIED, RANGE_CONTAINS }
5959
1,34,0 { TRY_FROM }
6060
1,33,0 { UNDERSCORE_IMPORTS }
61+
1,31,0 { OPTION_REPLACE }
6162
1,30,0 { ITERATOR_FIND_MAP, TOOL_ATTRIBUTES }
6263
1,29,0 { ITER_FLATTEN }
6364
1,28,0 { FROM_BOOL, REPEAT_WITH }

tests/ui/mem_replace.fixed

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,25 @@ fn issue9824() {
131131
// replace with default
132132
let _ = std::mem::take(&mut b.val);
133133
}
134+
135+
#[clippy::msrv = "1.31"]
136+
fn mem_replace_option_with_some() {
137+
let mut an_option = Some(0);
138+
let replaced = an_option.replace(1);
139+
//~^ ERROR: replacing an `Option` with `Some(..)`
140+
141+
let mut an_option = &mut Some(0);
142+
let replaced = an_option.replace(1);
143+
//~^ ERROR: replacing an `Option` with `Some(..)`
144+
145+
let (mut opt1, mut opt2) = (Some(0), Some(0));
146+
let b = true;
147+
let replaced = (if b { &mut opt1 } else { &mut opt2 }).replace(1);
148+
//~^ ERROR: replacing an `Option` with `Some(..)`
149+
}
150+
151+
#[clippy::msrv = "1.30"]
152+
fn mem_replace_option_with_some_bad_msrv() {
153+
let mut an_option = Some(0);
154+
let replaced = mem::replace(&mut an_option, Some(1));
155+
}

tests/ui/mem_replace.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,25 @@ fn issue9824() {
131131
// replace with default
132132
let _ = std::mem::replace(&mut b.val, String::default());
133133
}
134+
135+
#[clippy::msrv = "1.31"]
136+
fn mem_replace_option_with_some() {
137+
let mut an_option = Some(0);
138+
let replaced = mem::replace(&mut an_option, Some(1));
139+
//~^ ERROR: replacing an `Option` with `Some(..)`
140+
141+
let mut an_option = &mut Some(0);
142+
let replaced = mem::replace(an_option, Some(1));
143+
//~^ ERROR: replacing an `Option` with `Some(..)`
144+
145+
let (mut opt1, mut opt2) = (Some(0), Some(0));
146+
let b = true;
147+
let replaced = mem::replace(if b { &mut opt1 } else { &mut opt2 }, Some(1));
148+
//~^ ERROR: replacing an `Option` with `Some(..)`
149+
}
150+
151+
#[clippy::msrv = "1.30"]
152+
fn mem_replace_option_with_some_bad_msrv() {
153+
let mut an_option = Some(0);
154+
let replaced = mem::replace(&mut an_option, Some(1));
155+
}

tests/ui/mem_replace.stderr

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,5 +160,26 @@ error: replacing a value of type `T` with `T::default()` is better expressed usi
160160
LL | let _ = std::mem::replace(&mut b.val, String::default());
161161
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut b.val)`
162162

163-
error: aborting due to 26 previous errors
163+
error: replacing an `Option` with `Some(..)`
164+
--> tests/ui/mem_replace.rs:138:20
165+
|
166+
LL | let replaced = mem::replace(&mut an_option, Some(1));
167+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Option::replace()` instead: `an_option.replace(1)`
168+
|
169+
= note: `-D clippy::mem-replace-option-with-some` implied by `-D warnings`
170+
= help: to override `-D warnings` add `#[allow(clippy::mem_replace_option_with_some)]`
171+
172+
error: replacing an `Option` with `Some(..)`
173+
--> tests/ui/mem_replace.rs:142:20
174+
|
175+
LL | let replaced = mem::replace(an_option, Some(1));
176+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Option::replace()` instead: `an_option.replace(1)`
177+
178+
error: replacing an `Option` with `Some(..)`
179+
--> tests/ui/mem_replace.rs:147:20
180+
|
181+
LL | let replaced = mem::replace(if b { &mut opt1 } else { &mut opt2 }, Some(1));
182+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Option::replace()` instead: `(if b { &mut opt1 } else { &mut opt2 }).replace(1)`
183+
184+
error: aborting due to 29 previous errors
164185

0 commit comments

Comments
 (0)