Skip to content

Commit 75bff05

Browse files
mem_replace: clean-up (#17190)
This ended up being bigger than I expected... This PR mainly does the following: - Commit 2 expands on #17096 by cleaning up the attributes, and cleans up some more. - `mem_replace.rs` used to test `mem_replace_{option_with_none,option_with_some,default}`, which resulted in a rather big and messy file. Commits 4-6 split it into three. - Meanwhile, `mem_replace_with_uninit` was tested in a completely separate file, called `repl_uninit` (?!). Commit 3 renames it and cleans it up. As always, see the individual commits for more details. changelog: [`mem_replace_option_with_some`]: clean-up the lint message changelog: [`mem_replace_option_with_none`]: clean-up the lint message changelog: [`mem_replace_with_uninit`]: clean-up the lint message changelog: [`mem_replace_with_default`]: split off the suggestion from the main message
2 parents 53308b5 + 2ae2877 commit 75bff05

30 files changed

Lines changed: 611 additions & 574 deletions

clippy_lints/src/mem_replace.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ fn check_replace_option_with_none(cx: &LateContext<'_>, src: &Expr<'_>, dest: &E
145145
MEM_REPLACE_OPTION_WITH_NONE,
146146
expr_span,
147147
"replacing an `Option` with `None`",
148-
"consider `Option::take()` instead",
148+
"consider using `Option::take()` instead",
149149
format!(
150150
"{}.take()",
151151
Sugg::hir_with_context(cx, sugg_expr, expr_span.ctxt(), "", &mut applicability).maybe_paren()
@@ -177,7 +177,7 @@ fn check_replace_option_with_some(
177177
MEM_REPLACE_OPTION_WITH_SOME,
178178
expr_span,
179179
"replacing an `Option` with `Some(..)`",
180-
"consider `Option::replace()` instead",
180+
"consider using `Option::replace()` instead",
181181
format!(
182182
"{}.replace({})",
183183
Sugg::hir_with_context(cx, sugg_expr, expr_span.ctxt(), "_", &mut applicability).maybe_paren(),
@@ -203,7 +203,7 @@ fn check_replace_with_uninit(cx: &LateContext<'_>, src: &Expr<'_>, dest: &Expr<'
203203
MEM_REPLACE_WITH_UNINIT,
204204
expr_span,
205205
"replacing with `mem::MaybeUninit::uninit().assume_init()`",
206-
"consider using",
206+
format!("consider using `{top_crate}::ptr::read` instead"),
207207
format!(
208208
"{top_crate}::ptr::read({})",
209209
snippet_with_applicability(cx, dest.span, "", &mut applicability)
@@ -226,7 +226,7 @@ fn check_replace_with_uninit(cx: &LateContext<'_>, src: &Expr<'_>, dest: &Expr<'
226226
MEM_REPLACE_WITH_UNINIT,
227227
expr_span,
228228
"replacing with `mem::uninitialized()`",
229-
"consider using",
229+
format!("consider using `{top_crate}::ptr::read` instead"),
230230
format!(
231231
"{top_crate}::ptr::read({})",
232232
snippet_with_applicability(cx, dest.span, "", &mut applicability)
@@ -266,16 +266,19 @@ fn check_replace_with_default(
266266
cx,
267267
MEM_REPLACE_WITH_DEFAULT,
268268
expr.span,
269-
format!(
270-
"replacing a value of type `T` with `T::default()` is better expressed using `{top_crate}::mem::take`"
271-
),
269+
"replacing a value of type `T` with `T::default()`",
272270
|diag| {
273271
if !expr.span.from_expansion() {
274272
let mut applicability = Applicability::MachineApplicable;
275273
let (dest_snip, _) = snippet_with_context(cx, dest.span, expr.span.ctxt(), "", &mut applicability);
276274
let suggestion = format!("{top_crate}::mem::take({dest_snip})");
277275

278-
diag.span_suggestion(expr.span, "consider using", suggestion, applicability);
276+
diag.span_suggestion(
277+
expr.span,
278+
format!("consider using `{top_crate}::mem::take` instead"),
279+
suggestion,
280+
applicability,
281+
);
279282
}
280283
},
281284
);

tests/ui/mem_replace.stderr

Lines changed: 0 additions & 191 deletions
This file was deleted.

tests/ui/mem_replace_no_std.fixed

Lines changed: 0 additions & 84 deletions
This file was deleted.

0 commit comments

Comments
 (0)