Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions clippy_lints/src/mem_replace.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clippy_config::Conf;
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg, span_lint_and_then};
use clippy_utils::msrvs::{self, Msrv};
use clippy_utils::source::{snippet, snippet_with_applicability};
use clippy_utils::source::{snippet_with_applicability, snippet_with_context};
use clippy_utils::sugg::Sugg;
use clippy_utils::ty::is_non_aggregate_primitive_type;
use clippy_utils::{
Expand Down Expand Up @@ -269,14 +269,11 @@ fn check_replace_with_default(
),
|diag| {
if !expr.span.from_expansion() {
let suggestion = format!("{top_crate}::mem::take({})", snippet(cx, dest.span, ""));
let mut applicability = Applicability::MachineApplicable;
let (dest_snip, _) = snippet_with_context(cx, dest.span, expr.span.ctxt(), "", &mut applicability);
let suggestion = format!("{top_crate}::mem::take({dest_snip})");

diag.span_suggestion(
expr.span,
"consider using",
suggestion,
Applicability::MachineApplicable,
);
diag.span_suggestion(expr.span, "consider using", suggestion, applicability);
}
},
);
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/mem_replace.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,9 @@ fn mem_replace_option_with_some_bad_msrv() {
let mut an_option = Some(0);
let replaced = mem::replace(&mut an_option, Some(1));
}

fn issue15785() {
let mut text = String::from("foo");
let replaced = std::mem::take(dbg!(&mut text));
//~^ mem_replace_with_default
}
6 changes: 6 additions & 0 deletions tests/ui/mem_replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,9 @@ fn mem_replace_option_with_some_bad_msrv() {
let mut an_option = Some(0);
let replaced = mem::replace(&mut an_option, Some(1));
}

fn issue15785() {
let mut text = String::from("foo");
let replaced = std::mem::replace(dbg!(&mut text), String::default());
//~^ mem_replace_with_default
}
8 changes: 7 additions & 1 deletion tests/ui/mem_replace.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -181,5 +181,11 @@ error: replacing an `Option` with `Some(..)`
LL | let replaced = mem::replace(if b { &mut opt1 } else { &mut opt2 }, Some(1));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Option::replace()` instead: `(if b { &mut opt1 } else { &mut opt2 }).replace(1)`

error: aborting due to 29 previous errors
error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take`
--> tests/ui/mem_replace.rs:185:20
|
LL | let replaced = std::mem::replace(dbg!(&mut text), String::default());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(dbg!(&mut text))`

error: aborting due to 30 previous errors