Skip to content

Commit f985261

Browse files
committed
fix: mem_replace_with_default wrongly unmangled macros
1 parent 1a415e6 commit f985261

File tree

4 files changed

+24
-9
lines changed

4 files changed

+24
-9
lines changed

clippy_lints/src/mem_replace.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_config::Conf;
22
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg, span_lint_and_then};
33
use clippy_utils::msrvs::{self, Msrv};
4-
use clippy_utils::source::{snippet, snippet_with_applicability};
4+
use clippy_utils::source::{snippet_with_applicability, snippet_with_context};
55
use clippy_utils::sugg::Sugg;
66
use clippy_utils::ty::is_non_aggregate_primitive_type;
77
use clippy_utils::{
@@ -269,14 +269,11 @@ fn check_replace_with_default(
269269
),
270270
|diag| {
271271
if !expr.span.from_expansion() {
272-
let suggestion = format!("{top_crate}::mem::take({})", snippet(cx, dest.span, ""));
272+
let mut applicability = Applicability::MachineApplicable;
273+
let (dest_snip, _) = snippet_with_context(cx, dest.span, expr.span.ctxt(), "", &mut applicability);
274+
let suggestion = format!("{top_crate}::mem::take({dest_snip})");
273275

274-
diag.span_suggestion(
275-
expr.span,
276-
"consider using",
277-
suggestion,
278-
Applicability::MachineApplicable,
279-
);
276+
diag.span_suggestion(expr.span, "consider using", suggestion, applicability);
280277
}
281278
},
282279
);

tests/ui/mem_replace.fixed

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,9 @@ fn mem_replace_option_with_some_bad_msrv() {
179179
let mut an_option = Some(0);
180180
let replaced = mem::replace(&mut an_option, Some(1));
181181
}
182+
183+
fn issue15785() {
184+
let mut text = String::from("foo");
185+
let replaced = std::mem::take(dbg!(&mut text));
186+
//~^ mem_replace_with_default
187+
}

tests/ui/mem_replace.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,9 @@ fn mem_replace_option_with_some_bad_msrv() {
179179
let mut an_option = Some(0);
180180
let replaced = mem::replace(&mut an_option, Some(1));
181181
}
182+
183+
fn issue15785() {
184+
let mut text = String::from("foo");
185+
let replaced = std::mem::replace(dbg!(&mut text), String::default());
186+
//~^ mem_replace_with_default
187+
}

tests/ui/mem_replace.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,5 +181,11 @@ error: replacing an `Option` with `Some(..)`
181181
LL | let replaced = mem::replace(if b { &mut opt1 } else { &mut opt2 }, Some(1));
182182
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Option::replace()` instead: `(if b { &mut opt1 } else { &mut opt2 }).replace(1)`
183183

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

0 commit comments

Comments
 (0)