From 1389f57858cf8c214c89952d7bc8419de1b6b7c5 Mon Sep 17 00:00:00 2001 From: Ada Alakbarova Date: Mon, 8 Jun 2026 13:30:47 +0200 Subject: [PATCH 1/6] clean-up suggestion messages - unify wording - `mem_replace_with_default`: split off the suggestion message from the main message - mention the method to use in the suggestion message as well -- the suggestion message should be understandable on its own, without the diff --- clippy_lints/src/mem_replace.rs | 19 +++--- tests/ui/mem_replace.stderr | 104 ++++++++++++++--------------- tests/ui/mem_replace_macro.stderr | 2 +- tests/ui/mem_replace_no_std.stderr | 18 ++--- tests/ui/repl_uninit.stderr | 6 +- 5 files changed, 76 insertions(+), 73 deletions(-) diff --git a/clippy_lints/src/mem_replace.rs b/clippy_lints/src/mem_replace.rs index 43f0e5f8b639..fbbeec93c66b 100644 --- a/clippy_lints/src/mem_replace.rs +++ b/clippy_lints/src/mem_replace.rs @@ -145,7 +145,7 @@ fn check_replace_option_with_none(cx: &LateContext<'_>, src: &Expr<'_>, dest: &E MEM_REPLACE_OPTION_WITH_NONE, expr_span, "replacing an `Option` with `None`", - "consider `Option::take()` instead", + "consider using `Option::take()` instead", format!( "{}.take()", Sugg::hir_with_context(cx, sugg_expr, expr_span.ctxt(), "", &mut applicability).maybe_paren() @@ -177,7 +177,7 @@ fn check_replace_option_with_some( MEM_REPLACE_OPTION_WITH_SOME, expr_span, "replacing an `Option` with `Some(..)`", - "consider `Option::replace()` instead", + "consider using `Option::replace()` instead", format!( "{}.replace({})", 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<' MEM_REPLACE_WITH_UNINIT, expr_span, "replacing with `mem::MaybeUninit::uninit().assume_init()`", - "consider using", + format!("consider using `{top_crate}::ptr::read` instead"), format!( "{top_crate}::ptr::read({})", snippet_with_applicability(cx, dest.span, "", &mut applicability) @@ -226,7 +226,7 @@ fn check_replace_with_uninit(cx: &LateContext<'_>, src: &Expr<'_>, dest: &Expr<' MEM_REPLACE_WITH_UNINIT, expr_span, "replacing with `mem::uninitialized()`", - "consider using", + format!("consider using `{top_crate}::ptr::read` instead"), format!( "{top_crate}::ptr::read({})", snippet_with_applicability(cx, dest.span, "", &mut applicability) @@ -266,16 +266,19 @@ fn check_replace_with_default( cx, MEM_REPLACE_WITH_DEFAULT, expr.span, - format!( - "replacing a value of type `T` with `T::default()` is better expressed using `{top_crate}::mem::take`" - ), + "replacing a value of type `T` with `T::default()`", |diag| { if !expr.span.from_expansion() { 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); + diag.span_suggestion( + expr.span, + format!("consider using `{top_crate}::mem::take` instead"), + suggestion, + applicability, + ); } }, ); diff --git a/tests/ui/mem_replace.stderr b/tests/ui/mem_replace.stderr index bc374930cf0f..9424c32f5f15 100644 --- a/tests/ui/mem_replace.stderr +++ b/tests/ui/mem_replace.stderr @@ -2,7 +2,7 @@ error: replacing an `Option` with `None` --> tests/ui/mem_replace.rs:13:13 | LL | let _ = mem::replace(&mut an_option, None); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Option::take()` instead: `an_option.take()` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::take()` instead: `an_option.take()` | = note: `-D clippy::mem-replace-option-with-none` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::mem_replace_option_with_none)]` @@ -11,160 +11,160 @@ error: replacing an `Option` with `None` --> tests/ui/mem_replace.rs:16:13 | LL | let _ = mem::replace(an_option, None); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Option::take()` instead: `an_option.take()` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::take()` instead: `an_option.take()` -error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` +error: replacing a value of type `T` with `T::default()` --> tests/ui/mem_replace.rs:22:13 | LL | let _ = std::mem::replace(&mut s, String::default()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut s)` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut s)` | = note: `-D clippy::mem-replace-with-default` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::mem_replace_with_default)]` -error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` +error: replacing a value of type `T` with `T::default()` --> tests/ui/mem_replace.rs:24:13 | LL | let _ = std::mem::replace(&mut s, String::new()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut s)` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut s)` -error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` +error: replacing a value of type `T` with `T::default()` --> tests/ui/mem_replace.rs:28:13 | LL | let _ = std::mem::replace(s, String::default()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(s)` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(s)` -error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` +error: replacing a value of type `T` with `T::default()` --> tests/ui/mem_replace.rs:30:13 | LL | let _ = std::mem::replace(s, String::new()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(s)` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(s)` -error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` +error: replacing a value of type `T` with `T::default()` --> tests/ui/mem_replace.rs:32:13 | LL | let _ = std::mem::replace(s, Default::default()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(s)` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(s)` -error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` +error: replacing a value of type `T` with `T::default()` --> tests/ui/mem_replace.rs:36:13 | LL | let _ = std::mem::replace(&mut v, Vec::default()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut v)` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut v)` -error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` +error: replacing a value of type `T` with `T::default()` --> tests/ui/mem_replace.rs:38:13 | LL | let _ = std::mem::replace(&mut v, Default::default()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut v)` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut v)` -error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` +error: replacing a value of type `T` with `T::default()` --> tests/ui/mem_replace.rs:40:13 | LL | let _ = std::mem::replace(&mut v, Vec::new()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut v)` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut v)` -error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` +error: replacing a value of type `T` with `T::default()` --> tests/ui/mem_replace.rs:42:13 | LL | let _ = std::mem::replace(&mut v, vec![]); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut v)` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut v)` -error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` +error: replacing a value of type `T` with `T::default()` --> tests/ui/mem_replace.rs:46:13 | LL | let _ = std::mem::replace(&mut hash_map, HashMap::new()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut hash_map)` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut hash_map)` -error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` +error: replacing a value of type `T` with `T::default()` --> tests/ui/mem_replace.rs:50:13 | LL | let _ = std::mem::replace(&mut btree_map, BTreeMap::new()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut btree_map)` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut btree_map)` -error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` +error: replacing a value of type `T` with `T::default()` --> tests/ui/mem_replace.rs:54:13 | LL | let _ = std::mem::replace(&mut vd, VecDeque::new()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut vd)` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut vd)` -error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` +error: replacing a value of type `T` with `T::default()` --> tests/ui/mem_replace.rs:58:13 | LL | let _ = std::mem::replace(&mut hash_set, HashSet::new()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut hash_set)` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut hash_set)` -error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` +error: replacing a value of type `T` with `T::default()` --> tests/ui/mem_replace.rs:62:13 | LL | let _ = std::mem::replace(&mut btree_set, BTreeSet::new()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut btree_set)` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut btree_set)` -error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` +error: replacing a value of type `T` with `T::default()` --> tests/ui/mem_replace.rs:66:13 | LL | let _ = std::mem::replace(&mut list, LinkedList::new()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut list)` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut list)` -error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` +error: replacing a value of type `T` with `T::default()` --> tests/ui/mem_replace.rs:70:13 | LL | let _ = std::mem::replace(&mut binary_heap, BinaryHeap::new()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut binary_heap)` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut binary_heap)` -error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` +error: replacing a value of type `T` with `T::default()` --> tests/ui/mem_replace.rs:74:13 | LL | let _ = std::mem::replace(&mut tuple, (vec![], BinaryHeap::new())); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut tuple)` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut tuple)` -error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` +error: replacing a value of type `T` with `T::default()` --> tests/ui/mem_replace.rs:78:13 | LL | let _ = std::mem::replace(&mut refstr, ""); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut refstr)` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut refstr)` -error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` +error: replacing a value of type `T` with `T::default()` --> tests/ui/mem_replace.rs:82:13 | LL | let _ = std::mem::replace(&mut slice, &[]); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut slice)` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut slice)` -error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` +error: replacing a value of type `T` with `T::default()` --> tests/ui/mem_replace.rs:115:13 | LL | let _ = std::mem::replace(&mut s, String::default()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut s)` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut s)` error: replacing an `Option` with `None` --> tests/ui/mem_replace.rs:146:13 | LL | let _ = std::mem::replace(&mut f.0, None); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Option::take()` instead: `f.0.take()` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::take()` instead: `f.0.take()` error: replacing an `Option` with `None` --> tests/ui/mem_replace.rs:148:13 | LL | let _ = std::mem::replace(&mut *f, None); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Option::take()` instead: `(*f).take()` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::take()` instead: `(*f).take()` error: replacing an `Option` with `None` --> tests/ui/mem_replace.rs:150:13 | LL | let _ = std::mem::replace(&mut b.opt, None); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Option::take()` instead: `b.opt.take()` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::take()` instead: `b.opt.take()` -error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` +error: replacing a value of type `T` with `T::default()` --> tests/ui/mem_replace.rs:153:13 | LL | let _ = std::mem::replace(&mut b.val, String::default()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(&mut b.val)` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut b.val)` error: replacing an `Option` with `Some(..)` --> tests/ui/mem_replace.rs:160:20 | LL | let replaced = mem::replace(&mut an_option, Some(1)); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Option::replace()` instead: `an_option.replace(1)` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::replace()` instead: `an_option.replace(1)` | = note: `-D clippy::mem-replace-option-with-some` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::mem_replace_option_with_some)]` @@ -173,19 +173,19 @@ error: replacing an `Option` with `Some(..)` --> tests/ui/mem_replace.rs:164:20 | LL | let replaced = mem::replace(an_option, Some(1)); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Option::replace()` instead: `an_option.replace(1)` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::replace()` instead: `an_option.replace(1)` error: replacing an `Option` with `Some(..)` --> tests/ui/mem_replace.rs:169:20 | 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)` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::replace()` instead: `(if b { &mut opt1 } else { &mut opt2 }).replace(1)` -error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` +error: replacing a value of type `T` with `T::default()` --> tests/ui/mem_replace.rs:181:20 | LL | let replaced = std::mem::replace(dbg!(&mut text), String::default()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::mem::take(dbg!(&mut text))` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(dbg!(&mut text))` error: aborting due to 30 previous errors diff --git a/tests/ui/mem_replace_macro.stderr b/tests/ui/mem_replace_macro.stderr index 0c98200bf04e..b03853a49ec4 100644 --- a/tests/ui/mem_replace_macro.stderr +++ b/tests/ui/mem_replace_macro.stderr @@ -1,4 +1,4 @@ -error: replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take` +error: replacing a value of type `T` with `T::default()` --> tests/ui/mem_replace_macro.rs:10:21 | LL | let _ = inline!(std::mem::replace($s, Default::default())); diff --git a/tests/ui/mem_replace_no_std.stderr b/tests/ui/mem_replace_no_std.stderr index 34e81a9f0750..ae4f4a7d50cf 100644 --- a/tests/ui/mem_replace_no_std.stderr +++ b/tests/ui/mem_replace_no_std.stderr @@ -2,7 +2,7 @@ error: replacing an `Option` with `None` --> tests/ui/mem_replace_no_std.rs:23:13 | LL | let _ = mem::replace(&mut an_option, None); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Option::take()` instead: `an_option.take()` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::take()` instead: `an_option.take()` | = note: `-D clippy::mem-replace-option-with-none` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::mem_replace_option_with_none)]` @@ -11,40 +11,40 @@ error: replacing an `Option` with `None` --> tests/ui/mem_replace_no_std.rs:26:13 | LL | let _ = mem::replace(an_option, None); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Option::take()` instead: `an_option.take()` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::take()` instead: `an_option.take()` -error: replacing a value of type `T` with `T::default()` is better expressed using `core::mem::take` +error: replacing a value of type `T` with `T::default()` --> tests/ui/mem_replace_no_std.rs:32:13 | LL | let _ = mem::replace(&mut refstr, ""); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `core::mem::take(&mut refstr)` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `core::mem::take` instead: `core::mem::take(&mut refstr)` | = note: `-D clippy::mem-replace-with-default` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::mem_replace_with_default)]` -error: replacing a value of type `T` with `T::default()` is better expressed using `core::mem::take` +error: replacing a value of type `T` with `T::default()` --> tests/ui/mem_replace_no_std.rs:36:13 | LL | let _ = mem::replace(&mut slice, &[]); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `core::mem::take(&mut slice)` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `core::mem::take` instead: `core::mem::take(&mut slice)` error: replacing an `Option` with `None` --> tests/ui/mem_replace_no_std.rs:76:13 | LL | let _ = mem::replace(&mut f.0, None); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Option::take()` instead: `f.0.take()` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::take()` instead: `f.0.take()` error: replacing an `Option` with `None` --> tests/ui/mem_replace_no_std.rs:78:13 | LL | let _ = mem::replace(&mut *f, None); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Option::take()` instead: `(*f).take()` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::take()` instead: `(*f).take()` error: replacing an `Option` with `None` --> tests/ui/mem_replace_no_std.rs:80:13 | LL | let _ = mem::replace(&mut b.opt, None); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider `Option::take()` instead: `b.opt.take()` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::take()` instead: `b.opt.take()` error: aborting due to 7 previous errors diff --git a/tests/ui/repl_uninit.stderr b/tests/ui/repl_uninit.stderr index 08b0b265942d..602b96ec6e0a 100644 --- a/tests/ui/repl_uninit.stderr +++ b/tests/ui/repl_uninit.stderr @@ -2,7 +2,7 @@ error: replacing with `mem::uninitialized()` --> tests/ui/repl_uninit.rs:15:23 | LL | let taken_v = mem::replace(&mut v, mem::uninitialized()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::ptr::read(&mut v)` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::ptr::read` instead: `std::ptr::read(&mut v)` | = note: `-D clippy::mem-replace-with-uninit` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::mem_replace_with_uninit)]` @@ -11,7 +11,7 @@ error: replacing with `mem::MaybeUninit::uninit().assume_init()` --> tests/ui/repl_uninit.rs:23:23 | LL | let taken_v = mem::replace(&mut v, mem::MaybeUninit::uninit().assume_init()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::ptr::read(&mut v)` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::ptr::read` instead: `std::ptr::read(&mut v)` error: replacing with `mem::zeroed()` --> tests/ui/repl_uninit.rs:31:23 @@ -25,7 +25,7 @@ error: replacing with `mem::uninitialized()` --> tests/ui/repl_uninit.rs:45:28 | LL | let taken_u = unsafe { mem::replace(uref, mem::uninitialized()) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::ptr::read(uref)` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::ptr::read` instead: `std::ptr::read(uref)` error: aborting due to 4 previous errors From ef6e711dc1c8feaa1f00a41bc5b0ca9fcbce1fb3 Mon Sep 17 00:00:00 2001 From: Ada Alakbarova Date: Mon, 8 Jun 2026 13:26:24 +0200 Subject: [PATCH 2/6] clean-up tests, first pass - remove unused attributes - add `warn`s for _all_ the lints tested - remove `fn main` from the no-std test, so that it's considered a lib and thus doesn't need the no-std boilerplate - use shorthand error patterns --- tests/ui/mem_replace.fixed | 12 +++--- tests/ui/mem_replace.rs | 12 +++--- tests/ui/mem_replace.stderr | 60 +++++++++++++++--------------- tests/ui/mem_replace_no_std.fixed | 15 +------- tests/ui/mem_replace_no_std.rs | 15 +------- tests/ui/mem_replace_no_std.stderr | 14 +++---- 6 files changed, 49 insertions(+), 79 deletions(-) diff --git a/tests/ui/mem_replace.fixed b/tests/ui/mem_replace.fixed index 26aa7aa1b270..df2d36a39d9a 100644 --- a/tests/ui/mem_replace.fixed +++ b/tests/ui/mem_replace.fixed @@ -1,7 +1,6 @@ -#![allow(unused, clippy::needless_lifetimes)] #![warn( - clippy::style, clippy::mem_replace_option_with_none, + clippy::mem_replace_option_with_some, clippy::mem_replace_with_default )] @@ -142,14 +141,13 @@ fn issue9824() { val: String::from("bar"), }; - // replace option with none let _ = f.0.take(); //~^ mem_replace_option_with_none let _ = (*f).take(); //~^ mem_replace_option_with_none let _ = b.opt.take(); //~^ mem_replace_option_with_none - // replace with default + let _ = std::mem::take(&mut b.val); //~^ mem_replace_with_default } @@ -158,16 +156,16 @@ fn issue9824() { fn mem_replace_option_with_some() { let mut an_option = Some(0); let replaced = an_option.replace(1); - //~^ ERROR: replacing an `Option` with `Some(..)` + //~^ mem_replace_option_with_some let mut an_option = &mut Some(0); let replaced = an_option.replace(1); - //~^ ERROR: replacing an `Option` with `Some(..)` + //~^ mem_replace_option_with_some let (mut opt1, mut opt2) = (Some(0), Some(0)); let b = true; let replaced = (if b { &mut opt1 } else { &mut opt2 }).replace(1); - //~^ ERROR: replacing an `Option` with `Some(..)` + //~^ mem_replace_option_with_some } #[clippy::msrv = "1.30"] diff --git a/tests/ui/mem_replace.rs b/tests/ui/mem_replace.rs index cd675f5735a1..970bd6197e67 100644 --- a/tests/ui/mem_replace.rs +++ b/tests/ui/mem_replace.rs @@ -1,7 +1,6 @@ -#![allow(unused, clippy::needless_lifetimes)] #![warn( - clippy::style, clippy::mem_replace_option_with_none, + clippy::mem_replace_option_with_some, clippy::mem_replace_with_default )] @@ -142,14 +141,13 @@ fn issue9824() { val: String::from("bar"), }; - // replace option with none let _ = std::mem::replace(&mut f.0, None); //~^ mem_replace_option_with_none let _ = std::mem::replace(&mut *f, None); //~^ mem_replace_option_with_none let _ = std::mem::replace(&mut b.opt, None); //~^ mem_replace_option_with_none - // replace with default + let _ = std::mem::replace(&mut b.val, String::default()); //~^ mem_replace_with_default } @@ -158,16 +156,16 @@ fn issue9824() { fn mem_replace_option_with_some() { let mut an_option = Some(0); let replaced = mem::replace(&mut an_option, Some(1)); - //~^ ERROR: replacing an `Option` with `Some(..)` + //~^ mem_replace_option_with_some let mut an_option = &mut Some(0); let replaced = mem::replace(an_option, Some(1)); - //~^ ERROR: replacing an `Option` with `Some(..)` + //~^ mem_replace_option_with_some let (mut opt1, mut opt2) = (Some(0), Some(0)); let b = true; let replaced = mem::replace(if b { &mut opt1 } else { &mut opt2 }, Some(1)); - //~^ ERROR: replacing an `Option` with `Some(..)` + //~^ mem_replace_option_with_some } #[clippy::msrv = "1.30"] diff --git a/tests/ui/mem_replace.stderr b/tests/ui/mem_replace.stderr index 9424c32f5f15..04dcd0422435 100644 --- a/tests/ui/mem_replace.stderr +++ b/tests/ui/mem_replace.stderr @@ -1,5 +1,5 @@ error: replacing an `Option` with `None` - --> tests/ui/mem_replace.rs:13:13 + --> tests/ui/mem_replace.rs:12:13 | LL | let _ = mem::replace(&mut an_option, None); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::take()` instead: `an_option.take()` @@ -8,13 +8,13 @@ LL | let _ = mem::replace(&mut an_option, None); = help: to override `-D warnings` add `#[allow(clippy::mem_replace_option_with_none)]` error: replacing an `Option` with `None` - --> tests/ui/mem_replace.rs:16:13 + --> tests/ui/mem_replace.rs:15:13 | LL | let _ = mem::replace(an_option, None); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::take()` instead: `an_option.take()` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:22:13 + --> tests/ui/mem_replace.rs:21:13 | LL | let _ = std::mem::replace(&mut s, String::default()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut s)` @@ -23,145 +23,145 @@ LL | let _ = std::mem::replace(&mut s, String::default()); = help: to override `-D warnings` add `#[allow(clippy::mem_replace_with_default)]` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:24:13 + --> tests/ui/mem_replace.rs:23:13 | LL | let _ = std::mem::replace(&mut s, String::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut s)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:28:13 + --> tests/ui/mem_replace.rs:27:13 | LL | let _ = std::mem::replace(s, String::default()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(s)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:30:13 + --> tests/ui/mem_replace.rs:29:13 | LL | let _ = std::mem::replace(s, String::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(s)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:32:13 + --> tests/ui/mem_replace.rs:31:13 | LL | let _ = std::mem::replace(s, Default::default()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(s)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:36:13 + --> tests/ui/mem_replace.rs:35:13 | LL | let _ = std::mem::replace(&mut v, Vec::default()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut v)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:38:13 + --> tests/ui/mem_replace.rs:37:13 | LL | let _ = std::mem::replace(&mut v, Default::default()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut v)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:40:13 + --> tests/ui/mem_replace.rs:39:13 | LL | let _ = std::mem::replace(&mut v, Vec::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut v)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:42:13 + --> tests/ui/mem_replace.rs:41:13 | LL | let _ = std::mem::replace(&mut v, vec![]); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut v)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:46:13 + --> tests/ui/mem_replace.rs:45:13 | LL | let _ = std::mem::replace(&mut hash_map, HashMap::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut hash_map)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:50:13 + --> tests/ui/mem_replace.rs:49:13 | LL | let _ = std::mem::replace(&mut btree_map, BTreeMap::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut btree_map)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:54:13 + --> tests/ui/mem_replace.rs:53:13 | LL | let _ = std::mem::replace(&mut vd, VecDeque::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut vd)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:58:13 + --> tests/ui/mem_replace.rs:57:13 | LL | let _ = std::mem::replace(&mut hash_set, HashSet::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut hash_set)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:62:13 + --> tests/ui/mem_replace.rs:61:13 | LL | let _ = std::mem::replace(&mut btree_set, BTreeSet::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut btree_set)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:66:13 + --> tests/ui/mem_replace.rs:65:13 | LL | let _ = std::mem::replace(&mut list, LinkedList::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut list)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:70:13 + --> tests/ui/mem_replace.rs:69:13 | LL | let _ = std::mem::replace(&mut binary_heap, BinaryHeap::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut binary_heap)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:74:13 + --> tests/ui/mem_replace.rs:73:13 | LL | let _ = std::mem::replace(&mut tuple, (vec![], BinaryHeap::new())); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut tuple)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:78:13 + --> tests/ui/mem_replace.rs:77:13 | LL | let _ = std::mem::replace(&mut refstr, ""); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut refstr)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:82:13 + --> tests/ui/mem_replace.rs:81:13 | LL | let _ = std::mem::replace(&mut slice, &[]); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut slice)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:115:13 + --> tests/ui/mem_replace.rs:114:13 | LL | let _ = std::mem::replace(&mut s, String::default()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut s)` error: replacing an `Option` with `None` - --> tests/ui/mem_replace.rs:146:13 + --> tests/ui/mem_replace.rs:144:13 | LL | let _ = std::mem::replace(&mut f.0, None); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::take()` instead: `f.0.take()` error: replacing an `Option` with `None` - --> tests/ui/mem_replace.rs:148:13 + --> tests/ui/mem_replace.rs:146:13 | LL | let _ = std::mem::replace(&mut *f, None); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::take()` instead: `(*f).take()` error: replacing an `Option` with `None` - --> tests/ui/mem_replace.rs:150:13 + --> tests/ui/mem_replace.rs:148:13 | LL | let _ = std::mem::replace(&mut b.opt, None); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::take()` instead: `b.opt.take()` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:153:13 + --> tests/ui/mem_replace.rs:151:13 | LL | let _ = std::mem::replace(&mut b.val, String::default()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut b.val)` error: replacing an `Option` with `Some(..)` - --> tests/ui/mem_replace.rs:160:20 + --> tests/ui/mem_replace.rs:158:20 | LL | let replaced = mem::replace(&mut an_option, Some(1)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::replace()` instead: `an_option.replace(1)` @@ -170,19 +170,19 @@ LL | let replaced = mem::replace(&mut an_option, Some(1)); = help: to override `-D warnings` add `#[allow(clippy::mem_replace_option_with_some)]` error: replacing an `Option` with `Some(..)` - --> tests/ui/mem_replace.rs:164:20 + --> tests/ui/mem_replace.rs:162:20 | LL | let replaced = mem::replace(an_option, Some(1)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::replace()` instead: `an_option.replace(1)` error: replacing an `Option` with `Some(..)` - --> tests/ui/mem_replace.rs:169:20 + --> tests/ui/mem_replace.rs:167:20 | LL | let replaced = mem::replace(if b { &mut opt1 } else { &mut opt2 }, Some(1)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::replace()` instead: `(if b { &mut opt1 } else { &mut opt2 }).replace(1)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:181:20 + --> tests/ui/mem_replace.rs:179:20 | LL | let replaced = std::mem::replace(dbg!(&mut text), String::default()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(dbg!(&mut text))` diff --git a/tests/ui/mem_replace_no_std.fixed b/tests/ui/mem_replace_no_std.fixed index 4e2d413af6cd..3cb22ad53416 100644 --- a/tests/ui/mem_replace_no_std.fixed +++ b/tests/ui/mem_replace_no_std.fixed @@ -1,22 +1,11 @@ -#![allow(unused, clippy::needless_lifetimes)] #![warn( - clippy::style, clippy::mem_replace_option_with_none, + clippy::mem_replace_option_with_some, clippy::mem_replace_with_default )] -#![feature(lang_items)] #![no_std] use core::mem; -use core::panic::PanicInfo; - -#[lang = "eh_personality"] -extern "C" fn eh_personality() {} - -#[panic_handler] -fn panic(info: &PanicInfo) -> ! { - loop {} -} fn replace_option_with_none() { let mut an_option = Some(1); @@ -47,8 +36,6 @@ fn dont_lint_primitive() { let _ = mem::replace(&mut pint, 0); } -fn main() {} - fn issue9824() { struct Foo<'a>(Option<&'a str>); impl<'a> core::ops::Deref for Foo<'a> { diff --git a/tests/ui/mem_replace_no_std.rs b/tests/ui/mem_replace_no_std.rs index c0892304aba8..75f24725b51e 100644 --- a/tests/ui/mem_replace_no_std.rs +++ b/tests/ui/mem_replace_no_std.rs @@ -1,22 +1,11 @@ -#![allow(unused, clippy::needless_lifetimes)] #![warn( - clippy::style, clippy::mem_replace_option_with_none, + clippy::mem_replace_option_with_some, clippy::mem_replace_with_default )] -#![feature(lang_items)] #![no_std] use core::mem; -use core::panic::PanicInfo; - -#[lang = "eh_personality"] -extern "C" fn eh_personality() {} - -#[panic_handler] -fn panic(info: &PanicInfo) -> ! { - loop {} -} fn replace_option_with_none() { let mut an_option = Some(1); @@ -47,8 +36,6 @@ fn dont_lint_primitive() { let _ = mem::replace(&mut pint, 0); } -fn main() {} - fn issue9824() { struct Foo<'a>(Option<&'a str>); impl<'a> core::ops::Deref for Foo<'a> { diff --git a/tests/ui/mem_replace_no_std.stderr b/tests/ui/mem_replace_no_std.stderr index ae4f4a7d50cf..bbb5fb9d7f1c 100644 --- a/tests/ui/mem_replace_no_std.stderr +++ b/tests/ui/mem_replace_no_std.stderr @@ -1,5 +1,5 @@ error: replacing an `Option` with `None` - --> tests/ui/mem_replace_no_std.rs:23:13 + --> tests/ui/mem_replace_no_std.rs:12:13 | LL | let _ = mem::replace(&mut an_option, None); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::take()` instead: `an_option.take()` @@ -8,13 +8,13 @@ LL | let _ = mem::replace(&mut an_option, None); = help: to override `-D warnings` add `#[allow(clippy::mem_replace_option_with_none)]` error: replacing an `Option` with `None` - --> tests/ui/mem_replace_no_std.rs:26:13 + --> tests/ui/mem_replace_no_std.rs:15:13 | LL | let _ = mem::replace(an_option, None); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::take()` instead: `an_option.take()` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace_no_std.rs:32:13 + --> tests/ui/mem_replace_no_std.rs:21:13 | LL | let _ = mem::replace(&mut refstr, ""); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `core::mem::take` instead: `core::mem::take(&mut refstr)` @@ -23,25 +23,25 @@ LL | let _ = mem::replace(&mut refstr, ""); = help: to override `-D warnings` add `#[allow(clippy::mem_replace_with_default)]` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace_no_std.rs:36:13 + --> tests/ui/mem_replace_no_std.rs:25:13 | LL | let _ = mem::replace(&mut slice, &[]); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `core::mem::take` instead: `core::mem::take(&mut slice)` error: replacing an `Option` with `None` - --> tests/ui/mem_replace_no_std.rs:76:13 + --> tests/ui/mem_replace_no_std.rs:63:13 | LL | let _ = mem::replace(&mut f.0, None); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::take()` instead: `f.0.take()` error: replacing an `Option` with `None` - --> tests/ui/mem_replace_no_std.rs:78:13 + --> tests/ui/mem_replace_no_std.rs:65:13 | LL | let _ = mem::replace(&mut *f, None); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::take()` instead: `(*f).take()` error: replacing an `Option` with `None` - --> tests/ui/mem_replace_no_std.rs:80:13 + --> tests/ui/mem_replace_no_std.rs:67:13 | LL | let _ = mem::replace(&mut b.opt, None); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::take()` instead: `b.opt.take()` From a78f6678e4a85014b1a6f006d920373aa7378ecd Mon Sep 17 00:00:00 2001 From: Ada Alakbarova Date: Mon, 8 Jun 2026 22:49:08 +0200 Subject: [PATCH 3/6] rename `repl_uninit.rs` to `mem_replace_with_uninit.rs`, clean-up - Turns out to be a test for `mem_replace_with_uninit` - Extract the one unfixable case into a separate file - Clean-up attributes, now with comments! --- tests/ui/mem_replace_with_uninit.fixed | 50 +++++++++++++++++++ ...l_uninit.rs => mem_replace_with_uninit.rs} | 21 ++++---- ....stderr => mem_replace_with_uninit.stderr} | 16 ++---- tests/ui/mem_replace_with_uninit_unfixable.rs | 22 ++++++++ .../mem_replace_with_uninit_unfixable.stderr | 12 +++++ 5 files changed, 99 insertions(+), 22 deletions(-) create mode 100644 tests/ui/mem_replace_with_uninit.fixed rename tests/ui/{repl_uninit.rs => mem_replace_with_uninit.rs} (78%) rename tests/ui/{repl_uninit.stderr => mem_replace_with_uninit.stderr} (70%) create mode 100644 tests/ui/mem_replace_with_uninit_unfixable.rs create mode 100644 tests/ui/mem_replace_with_uninit_unfixable.stderr diff --git a/tests/ui/mem_replace_with_uninit.fixed b/tests/ui/mem_replace_with_uninit.fixed new file mode 100644 index 000000000000..d55ac85dfc71 --- /dev/null +++ b/tests/ui/mem_replace_with_uninit.fixed @@ -0,0 +1,50 @@ +#![warn(clippy::mem_replace_with_uninit)] +#![allow( + // These get removed by the suggestion + deprecated, // for `std::mem::uninitialized` + invalid_value, + clippy::uninit_assumed_init, + + // Added because the suggestion is `std::ptr::read(&mut v)` + // (which might be considered a bug) + clippy::unnecessary_mut_passed, +)] + +use std::mem; + +fn might_panic(x: X) -> X { + // in practice this would be a possibly-panicky operation + x +} + +fn main() { + let mut v = vec![0i32; 4]; + // the following is UB if `might_panic` panics + unsafe { + let taken_v = std::ptr::read(&mut v); + //~^ mem_replace_with_uninit + + let new_v = might_panic(taken_v); + std::mem::forget(mem::replace(&mut v, new_v)); + } + + unsafe { + let taken_v = std::ptr::read(&mut v); + //~^ mem_replace_with_uninit + + let new_v = might_panic(taken_v); + std::mem::forget(mem::replace(&mut v, new_v)); + } + + // this is silly but OK, because usize is a primitive type + let mut u: usize = 42; + let uref = &mut u; + let taken_u = unsafe { mem::replace(uref, mem::zeroed()) }; + *uref = taken_u + 1; + + // this is still not OK, because uninit + let taken_u = unsafe { std::ptr::read(uref) }; + //~^ mem_replace_with_uninit + + *uref = taken_u + 1; +} diff --git a/tests/ui/repl_uninit.rs b/tests/ui/mem_replace_with_uninit.rs similarity index 78% rename from tests/ui/repl_uninit.rs rename to tests/ui/mem_replace_with_uninit.rs index e9469d4c5e2f..0e6ede307f02 100644 --- a/tests/ui/repl_uninit.rs +++ b/tests/ui/mem_replace_with_uninit.rs @@ -1,6 +1,15 @@ -#![allow(deprecated, invalid_value, clippy::uninit_assumed_init)] #![warn(clippy::mem_replace_with_uninit)] -//@no-rustfix +#![allow( + // These get removed by the suggestion + deprecated, // for `std::mem::uninitialized` + invalid_value, + clippy::uninit_assumed_init, + + // Added because the suggestion is `std::ptr::read(&mut v)` + // (which might be considered a bug) + clippy::unnecessary_mut_passed, +)] + use std::mem; fn might_panic(x: X) -> X { @@ -27,14 +36,6 @@ fn main() { std::mem::forget(mem::replace(&mut v, new_v)); } - unsafe { - let taken_v = mem::replace(&mut v, mem::zeroed()); - //~^ mem_replace_with_uninit - - let new_v = might_panic(taken_v); - std::mem::forget(mem::replace(&mut v, new_v)); - } - // this is silly but OK, because usize is a primitive type let mut u: usize = 42; let uref = &mut u; diff --git a/tests/ui/repl_uninit.stderr b/tests/ui/mem_replace_with_uninit.stderr similarity index 70% rename from tests/ui/repl_uninit.stderr rename to tests/ui/mem_replace_with_uninit.stderr index 602b96ec6e0a..b5616ab72226 100644 --- a/tests/ui/repl_uninit.stderr +++ b/tests/ui/mem_replace_with_uninit.stderr @@ -1,5 +1,5 @@ error: replacing with `mem::uninitialized()` - --> tests/ui/repl_uninit.rs:15:23 + --> tests/ui/mem_replace_with_uninit.rs:24:23 | LL | let taken_v = mem::replace(&mut v, mem::uninitialized()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::ptr::read` instead: `std::ptr::read(&mut v)` @@ -8,24 +8,16 @@ LL | let taken_v = mem::replace(&mut v, mem::uninitialized()); = help: to override `-D warnings` add `#[allow(clippy::mem_replace_with_uninit)]` error: replacing with `mem::MaybeUninit::uninit().assume_init()` - --> tests/ui/repl_uninit.rs:23:23 + --> tests/ui/mem_replace_with_uninit.rs:32:23 | LL | let taken_v = mem::replace(&mut v, mem::MaybeUninit::uninit().assume_init()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::ptr::read` instead: `std::ptr::read(&mut v)` -error: replacing with `mem::zeroed()` - --> tests/ui/repl_uninit.rs:31:23 - | -LL | let taken_v = mem::replace(&mut v, mem::zeroed()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = help: consider using a default value or the `take_mut` crate instead - error: replacing with `mem::uninitialized()` - --> tests/ui/repl_uninit.rs:45:28 + --> tests/ui/mem_replace_with_uninit.rs:46:28 | LL | let taken_u = unsafe { mem::replace(uref, mem::uninitialized()) }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::ptr::read` instead: `std::ptr::read(uref)` -error: aborting due to 4 previous errors +error: aborting due to 3 previous errors diff --git a/tests/ui/mem_replace_with_uninit_unfixable.rs b/tests/ui/mem_replace_with_uninit_unfixable.rs new file mode 100644 index 000000000000..7abb7f2c5dc3 --- /dev/null +++ b/tests/ui/mem_replace_with_uninit_unfixable.rs @@ -0,0 +1,22 @@ +// The lint does not offer a suggestion for the `zeroed` case +//@ no-rustfix +#![warn(clippy::mem_replace_with_uninit)] +#![expect(invalid_value)] + +use std::mem; + +fn might_panic(x: X) -> X { + // in practice this would be a possibly-panicky operation + x +} + +fn main() { + let mut v = vec![0i32; 4]; + unsafe { + let taken_v = mem::replace(&mut v, mem::zeroed()); + //~^ mem_replace_with_uninit + + let new_v = might_panic(taken_v); + std::mem::forget(mem::replace(&mut v, new_v)); + } +} diff --git a/tests/ui/mem_replace_with_uninit_unfixable.stderr b/tests/ui/mem_replace_with_uninit_unfixable.stderr new file mode 100644 index 000000000000..166d9fdd2ea8 --- /dev/null +++ b/tests/ui/mem_replace_with_uninit_unfixable.stderr @@ -0,0 +1,12 @@ +error: replacing with `mem::zeroed()` + --> tests/ui/mem_replace_with_uninit_unfixable.rs:16:23 + | +LL | let taken_v = mem::replace(&mut v, mem::zeroed()); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: consider using a default value or the `take_mut` crate instead + = note: `-D clippy::mem-replace-with-uninit` implied by `-D warnings` + = help: to override `-D warnings` add `#[allow(clippy::mem_replace_with_uninit)]` + +error: aborting due to 1 previous error + From a5b5cefba43a4b8c58ced2724151dd58ffa407b3 Mon Sep 17 00:00:00 2001 From: Ada Alakbarova Date: Mon, 8 Jun 2026 23:31:38 +0200 Subject: [PATCH 4/6] extract `mem_replace_option_with_none` tests into their own file Note that this doesn't migrate the no-std test for issue9824. The motivation is that the no-std file is usually supposed to just test that the basic functionality of the lint works in no-std, which boils down to `std` being properly replaced with `core`. Bringing a reproducer for a separate issue thus doesn't add much value. --- tests/ui/mem_replace.fixed | 39 +-------- tests/ui/mem_replace.rs | 39 +-------- tests/ui/mem_replace.stderr | 85 ++++++------------- tests/ui/mem_replace_no_std.fixed | 41 +-------- tests/ui/mem_replace_no_std.rs | 41 +-------- tests/ui/mem_replace_no_std.stderr | 39 +-------- tests/ui/mem_replace_option_with_none.fixed | 42 +++++++++ tests/ui/mem_replace_option_with_none.rs | 42 +++++++++ tests/ui/mem_replace_option_with_none.stderr | 35 ++++++++ .../mem_replace_option_with_none_no_std.fixed | 13 +++ .../ui/mem_replace_option_with_none_no_std.rs | 13 +++ ...mem_replace_option_with_none_no_std.stderr | 17 ++++ 12 files changed, 197 insertions(+), 249 deletions(-) create mode 100644 tests/ui/mem_replace_option_with_none.fixed create mode 100644 tests/ui/mem_replace_option_with_none.rs create mode 100644 tests/ui/mem_replace_option_with_none.stderr create mode 100644 tests/ui/mem_replace_option_with_none_no_std.fixed create mode 100644 tests/ui/mem_replace_option_with_none_no_std.rs create mode 100644 tests/ui/mem_replace_option_with_none_no_std.stderr diff --git a/tests/ui/mem_replace.fixed b/tests/ui/mem_replace.fixed index df2d36a39d9a..1c5cd64f5ff3 100644 --- a/tests/ui/mem_replace.fixed +++ b/tests/ui/mem_replace.fixed @@ -1,21 +1,8 @@ -#![warn( - clippy::mem_replace_option_with_none, - clippy::mem_replace_option_with_some, - clippy::mem_replace_with_default -)] +#![warn(clippy::mem_replace_option_with_some, clippy::mem_replace_with_default)] use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList, VecDeque}; use std::mem; -fn replace_option_with_none() { - let mut an_option = Some(1); - let _ = an_option.take(); - //~^ mem_replace_option_with_none - let an_option = &mut Some(1); - let _ = an_option.take(); - //~^ mem_replace_option_with_none -} - fn replace_with_default() { let mut s = String::from("foo"); let _ = std::mem::take(&mut s); @@ -116,38 +103,14 @@ fn msrv_1_40() { } fn issue9824() { - struct Foo<'a>(Option<&'a str>); - impl<'a> std::ops::Deref for Foo<'a> { - type Target = Option<&'a str>; - - fn deref(&self) -> &Self::Target { - &self.0 - } - } - impl<'a> std::ops::DerefMut for Foo<'a> { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.0 - } - } - struct Bar { - opt: Option, val: String, } - let mut f = Foo(Some("foo")); let mut b = Bar { - opt: Some(1), val: String::from("bar"), }; - let _ = f.0.take(); - //~^ mem_replace_option_with_none - let _ = (*f).take(); - //~^ mem_replace_option_with_none - let _ = b.opt.take(); - //~^ mem_replace_option_with_none - let _ = std::mem::take(&mut b.val); //~^ mem_replace_with_default } diff --git a/tests/ui/mem_replace.rs b/tests/ui/mem_replace.rs index 970bd6197e67..4d966fb58038 100644 --- a/tests/ui/mem_replace.rs +++ b/tests/ui/mem_replace.rs @@ -1,21 +1,8 @@ -#![warn( - clippy::mem_replace_option_with_none, - clippy::mem_replace_option_with_some, - clippy::mem_replace_with_default -)] +#![warn(clippy::mem_replace_option_with_some, clippy::mem_replace_with_default)] use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList, VecDeque}; use std::mem; -fn replace_option_with_none() { - let mut an_option = Some(1); - let _ = mem::replace(&mut an_option, None); - //~^ mem_replace_option_with_none - let an_option = &mut Some(1); - let _ = mem::replace(an_option, None); - //~^ mem_replace_option_with_none -} - fn replace_with_default() { let mut s = String::from("foo"); let _ = std::mem::replace(&mut s, String::default()); @@ -116,38 +103,14 @@ fn msrv_1_40() { } fn issue9824() { - struct Foo<'a>(Option<&'a str>); - impl<'a> std::ops::Deref for Foo<'a> { - type Target = Option<&'a str>; - - fn deref(&self) -> &Self::Target { - &self.0 - } - } - impl<'a> std::ops::DerefMut for Foo<'a> { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.0 - } - } - struct Bar { - opt: Option, val: String, } - let mut f = Foo(Some("foo")); let mut b = Bar { - opt: Some(1), val: String::from("bar"), }; - let _ = std::mem::replace(&mut f.0, None); - //~^ mem_replace_option_with_none - let _ = std::mem::replace(&mut *f, None); - //~^ mem_replace_option_with_none - let _ = std::mem::replace(&mut b.opt, None); - //~^ mem_replace_option_with_none - let _ = std::mem::replace(&mut b.val, String::default()); //~^ mem_replace_with_default } diff --git a/tests/ui/mem_replace.stderr b/tests/ui/mem_replace.stderr index 04dcd0422435..497144c67696 100644 --- a/tests/ui/mem_replace.stderr +++ b/tests/ui/mem_replace.stderr @@ -1,20 +1,5 @@ -error: replacing an `Option` with `None` - --> tests/ui/mem_replace.rs:12:13 - | -LL | let _ = mem::replace(&mut an_option, None); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::take()` instead: `an_option.take()` - | - = note: `-D clippy::mem-replace-option-with-none` implied by `-D warnings` - = help: to override `-D warnings` add `#[allow(clippy::mem_replace_option_with_none)]` - -error: replacing an `Option` with `None` - --> tests/ui/mem_replace.rs:15:13 - | -LL | let _ = mem::replace(an_option, None); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::take()` instead: `an_option.take()` - error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:21:13 + --> tests/ui/mem_replace.rs:8:13 | LL | let _ = std::mem::replace(&mut s, String::default()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut s)` @@ -23,145 +8,127 @@ LL | let _ = std::mem::replace(&mut s, String::default()); = help: to override `-D warnings` add `#[allow(clippy::mem_replace_with_default)]` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:23:13 + --> tests/ui/mem_replace.rs:10:13 | LL | let _ = std::mem::replace(&mut s, String::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut s)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:27:13 + --> tests/ui/mem_replace.rs:14:13 | LL | let _ = std::mem::replace(s, String::default()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(s)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:29:13 + --> tests/ui/mem_replace.rs:16:13 | LL | let _ = std::mem::replace(s, String::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(s)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:31:13 + --> tests/ui/mem_replace.rs:18:13 | LL | let _ = std::mem::replace(s, Default::default()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(s)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:35:13 + --> tests/ui/mem_replace.rs:22:13 | LL | let _ = std::mem::replace(&mut v, Vec::default()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut v)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:37:13 + --> tests/ui/mem_replace.rs:24:13 | LL | let _ = std::mem::replace(&mut v, Default::default()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut v)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:39:13 + --> tests/ui/mem_replace.rs:26:13 | LL | let _ = std::mem::replace(&mut v, Vec::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut v)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:41:13 + --> tests/ui/mem_replace.rs:28:13 | LL | let _ = std::mem::replace(&mut v, vec![]); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut v)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:45:13 + --> tests/ui/mem_replace.rs:32:13 | LL | let _ = std::mem::replace(&mut hash_map, HashMap::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut hash_map)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:49:13 + --> tests/ui/mem_replace.rs:36:13 | LL | let _ = std::mem::replace(&mut btree_map, BTreeMap::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut btree_map)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:53:13 + --> tests/ui/mem_replace.rs:40:13 | LL | let _ = std::mem::replace(&mut vd, VecDeque::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut vd)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:57:13 + --> tests/ui/mem_replace.rs:44:13 | LL | let _ = std::mem::replace(&mut hash_set, HashSet::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut hash_set)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:61:13 + --> tests/ui/mem_replace.rs:48:13 | LL | let _ = std::mem::replace(&mut btree_set, BTreeSet::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut btree_set)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:65:13 + --> tests/ui/mem_replace.rs:52:13 | LL | let _ = std::mem::replace(&mut list, LinkedList::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut list)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:69:13 + --> tests/ui/mem_replace.rs:56:13 | LL | let _ = std::mem::replace(&mut binary_heap, BinaryHeap::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut binary_heap)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:73:13 + --> tests/ui/mem_replace.rs:60:13 | LL | let _ = std::mem::replace(&mut tuple, (vec![], BinaryHeap::new())); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut tuple)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:77:13 + --> tests/ui/mem_replace.rs:64:13 | LL | let _ = std::mem::replace(&mut refstr, ""); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut refstr)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:81:13 + --> tests/ui/mem_replace.rs:68:13 | LL | let _ = std::mem::replace(&mut slice, &[]); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut slice)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:114:13 + --> tests/ui/mem_replace.rs:101:13 | LL | let _ = std::mem::replace(&mut s, String::default()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut s)` -error: replacing an `Option` with `None` - --> tests/ui/mem_replace.rs:144:13 - | -LL | let _ = std::mem::replace(&mut f.0, None); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::take()` instead: `f.0.take()` - -error: replacing an `Option` with `None` - --> tests/ui/mem_replace.rs:146:13 - | -LL | let _ = std::mem::replace(&mut *f, None); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::take()` instead: `(*f).take()` - -error: replacing an `Option` with `None` - --> tests/ui/mem_replace.rs:148:13 - | -LL | let _ = std::mem::replace(&mut b.opt, None); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::take()` instead: `b.opt.take()` - error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:151:13 + --> tests/ui/mem_replace.rs:114:13 | LL | let _ = std::mem::replace(&mut b.val, String::default()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut b.val)` error: replacing an `Option` with `Some(..)` - --> tests/ui/mem_replace.rs:158:20 + --> tests/ui/mem_replace.rs:121:20 | LL | let replaced = mem::replace(&mut an_option, Some(1)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::replace()` instead: `an_option.replace(1)` @@ -170,22 +137,22 @@ LL | let replaced = mem::replace(&mut an_option, Some(1)); = help: to override `-D warnings` add `#[allow(clippy::mem_replace_option_with_some)]` error: replacing an `Option` with `Some(..)` - --> tests/ui/mem_replace.rs:162:20 + --> tests/ui/mem_replace.rs:125:20 | LL | let replaced = mem::replace(an_option, Some(1)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::replace()` instead: `an_option.replace(1)` error: replacing an `Option` with `Some(..)` - --> tests/ui/mem_replace.rs:167:20 + --> tests/ui/mem_replace.rs:130:20 | LL | let replaced = mem::replace(if b { &mut opt1 } else { &mut opt2 }, Some(1)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::replace()` instead: `(if b { &mut opt1 } else { &mut opt2 }).replace(1)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:179:20 + --> tests/ui/mem_replace.rs:142:20 | LL | let replaced = std::mem::replace(dbg!(&mut text), String::default()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(dbg!(&mut text))` -error: aborting due to 30 previous errors +error: aborting due to 25 previous errors diff --git a/tests/ui/mem_replace_no_std.fixed b/tests/ui/mem_replace_no_std.fixed index 3cb22ad53416..419a9ba864b6 100644 --- a/tests/ui/mem_replace_no_std.fixed +++ b/tests/ui/mem_replace_no_std.fixed @@ -1,21 +1,8 @@ -#![warn( - clippy::mem_replace_option_with_none, - clippy::mem_replace_option_with_some, - clippy::mem_replace_with_default -)] +#![warn(clippy::mem_replace_option_with_some, clippy::mem_replace_with_default)] #![no_std] use core::mem; -fn replace_option_with_none() { - let mut an_option = Some(1); - let _ = an_option.take(); - //~^ mem_replace_option_with_none - let an_option = &mut Some(1); - let _ = an_option.take(); - //~^ mem_replace_option_with_none -} - fn replace_with_default() { let mut refstr = "hello"; let _ = core::mem::take(&mut refstr); @@ -37,35 +24,11 @@ fn dont_lint_primitive() { } fn issue9824() { - struct Foo<'a>(Option<&'a str>); - impl<'a> core::ops::Deref for Foo<'a> { - type Target = Option<&'a str>; - - fn deref(&self) -> &Self::Target { - &self.0 - } - } - impl<'a> core::ops::DerefMut for Foo<'a> { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.0 - } - } - struct Bar { - opt: Option, val: u8, } - let mut f = Foo(Some("foo")); - let mut b = Bar { opt: Some(1), val: 12 }; - - // replace option with none - let _ = f.0.take(); - //~^ mem_replace_option_with_none - let _ = (*f).take(); - //~^ mem_replace_option_with_none - let _ = b.opt.take(); - //~^ mem_replace_option_with_none + let mut b = Bar { val: 12 }; // replace with default let _ = mem::replace(&mut b.val, u8::default()); } diff --git a/tests/ui/mem_replace_no_std.rs b/tests/ui/mem_replace_no_std.rs index 75f24725b51e..775f3af9433b 100644 --- a/tests/ui/mem_replace_no_std.rs +++ b/tests/ui/mem_replace_no_std.rs @@ -1,21 +1,8 @@ -#![warn( - clippy::mem_replace_option_with_none, - clippy::mem_replace_option_with_some, - clippy::mem_replace_with_default -)] +#![warn(clippy::mem_replace_option_with_some, clippy::mem_replace_with_default)] #![no_std] use core::mem; -fn replace_option_with_none() { - let mut an_option = Some(1); - let _ = mem::replace(&mut an_option, None); - //~^ mem_replace_option_with_none - let an_option = &mut Some(1); - let _ = mem::replace(an_option, None); - //~^ mem_replace_option_with_none -} - fn replace_with_default() { let mut refstr = "hello"; let _ = mem::replace(&mut refstr, ""); @@ -37,35 +24,11 @@ fn dont_lint_primitive() { } fn issue9824() { - struct Foo<'a>(Option<&'a str>); - impl<'a> core::ops::Deref for Foo<'a> { - type Target = Option<&'a str>; - - fn deref(&self) -> &Self::Target { - &self.0 - } - } - impl<'a> core::ops::DerefMut for Foo<'a> { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.0 - } - } - struct Bar { - opt: Option, val: u8, } - let mut f = Foo(Some("foo")); - let mut b = Bar { opt: Some(1), val: 12 }; - - // replace option with none - let _ = mem::replace(&mut f.0, None); - //~^ mem_replace_option_with_none - let _ = mem::replace(&mut *f, None); - //~^ mem_replace_option_with_none - let _ = mem::replace(&mut b.opt, None); - //~^ mem_replace_option_with_none + let mut b = Bar { val: 12 }; // replace with default let _ = mem::replace(&mut b.val, u8::default()); } diff --git a/tests/ui/mem_replace_no_std.stderr b/tests/ui/mem_replace_no_std.stderr index bbb5fb9d7f1c..90e8ce3f5e48 100644 --- a/tests/ui/mem_replace_no_std.stderr +++ b/tests/ui/mem_replace_no_std.stderr @@ -1,20 +1,5 @@ -error: replacing an `Option` with `None` - --> tests/ui/mem_replace_no_std.rs:12:13 - | -LL | let _ = mem::replace(&mut an_option, None); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::take()` instead: `an_option.take()` - | - = note: `-D clippy::mem-replace-option-with-none` implied by `-D warnings` - = help: to override `-D warnings` add `#[allow(clippy::mem_replace_option_with_none)]` - -error: replacing an `Option` with `None` - --> tests/ui/mem_replace_no_std.rs:15:13 - | -LL | let _ = mem::replace(an_option, None); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::take()` instead: `an_option.take()` - error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace_no_std.rs:21:13 + --> tests/ui/mem_replace_no_std.rs:8:13 | LL | let _ = mem::replace(&mut refstr, ""); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `core::mem::take` instead: `core::mem::take(&mut refstr)` @@ -23,28 +8,10 @@ LL | let _ = mem::replace(&mut refstr, ""); = help: to override `-D warnings` add `#[allow(clippy::mem_replace_with_default)]` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace_no_std.rs:25:13 + --> tests/ui/mem_replace_no_std.rs:12:13 | LL | let _ = mem::replace(&mut slice, &[]); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `core::mem::take` instead: `core::mem::take(&mut slice)` -error: replacing an `Option` with `None` - --> tests/ui/mem_replace_no_std.rs:63:13 - | -LL | let _ = mem::replace(&mut f.0, None); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::take()` instead: `f.0.take()` - -error: replacing an `Option` with `None` - --> tests/ui/mem_replace_no_std.rs:65:13 - | -LL | let _ = mem::replace(&mut *f, None); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::take()` instead: `(*f).take()` - -error: replacing an `Option` with `None` - --> tests/ui/mem_replace_no_std.rs:67:13 - | -LL | let _ = mem::replace(&mut b.opt, None); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::take()` instead: `b.opt.take()` - -error: aborting due to 7 previous errors +error: aborting due to 2 previous errors diff --git a/tests/ui/mem_replace_option_with_none.fixed b/tests/ui/mem_replace_option_with_none.fixed new file mode 100644 index 000000000000..9041e8595d6d --- /dev/null +++ b/tests/ui/mem_replace_option_with_none.fixed @@ -0,0 +1,42 @@ +#![warn(clippy::mem_replace_option_with_none)] + +use std::mem; + +fn main() { + let mut an_option = Some(1); + let _ = an_option.take(); + //~^ mem_replace_option_with_none + let an_option = &mut Some(1); + let _ = an_option.take(); + //~^ mem_replace_option_with_none +} + +fn issue9824() { + struct Foo<'a>(Option<&'a str>); + impl<'a> std::ops::Deref for Foo<'a> { + type Target = Option<&'a str>; + + fn deref(&self) -> &Self::Target { + &self.0 + } + } + impl<'a> std::ops::DerefMut for Foo<'a> { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } + } + + struct Bar { + opt: Option, + } + + let mut f = Foo(Some("foo")); + let mut b = Bar { opt: Some(1) }; + + let _ = f.0.take(); + //~^ mem_replace_option_with_none + let _ = (*f).take(); + //~^ mem_replace_option_with_none + let _ = b.opt.take(); + //~^ mem_replace_option_with_none +} diff --git a/tests/ui/mem_replace_option_with_none.rs b/tests/ui/mem_replace_option_with_none.rs new file mode 100644 index 000000000000..b51115f02e1b --- /dev/null +++ b/tests/ui/mem_replace_option_with_none.rs @@ -0,0 +1,42 @@ +#![warn(clippy::mem_replace_option_with_none)] + +use std::mem; + +fn main() { + let mut an_option = Some(1); + let _ = mem::replace(&mut an_option, None); + //~^ mem_replace_option_with_none + let an_option = &mut Some(1); + let _ = mem::replace(an_option, None); + //~^ mem_replace_option_with_none +} + +fn issue9824() { + struct Foo<'a>(Option<&'a str>); + impl<'a> std::ops::Deref for Foo<'a> { + type Target = Option<&'a str>; + + fn deref(&self) -> &Self::Target { + &self.0 + } + } + impl<'a> std::ops::DerefMut for Foo<'a> { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } + } + + struct Bar { + opt: Option, + } + + let mut f = Foo(Some("foo")); + let mut b = Bar { opt: Some(1) }; + + let _ = std::mem::replace(&mut f.0, None); + //~^ mem_replace_option_with_none + let _ = std::mem::replace(&mut *f, None); + //~^ mem_replace_option_with_none + let _ = std::mem::replace(&mut b.opt, None); + //~^ mem_replace_option_with_none +} diff --git a/tests/ui/mem_replace_option_with_none.stderr b/tests/ui/mem_replace_option_with_none.stderr new file mode 100644 index 000000000000..d72aef719553 --- /dev/null +++ b/tests/ui/mem_replace_option_with_none.stderr @@ -0,0 +1,35 @@ +error: replacing an `Option` with `None` + --> tests/ui/mem_replace_option_with_none.rs:7:13 + | +LL | let _ = mem::replace(&mut an_option, None); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::take()` instead: `an_option.take()` + | + = note: `-D clippy::mem-replace-option-with-none` implied by `-D warnings` + = help: to override `-D warnings` add `#[allow(clippy::mem_replace_option_with_none)]` + +error: replacing an `Option` with `None` + --> tests/ui/mem_replace_option_with_none.rs:10:13 + | +LL | let _ = mem::replace(an_option, None); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::take()` instead: `an_option.take()` + +error: replacing an `Option` with `None` + --> tests/ui/mem_replace_option_with_none.rs:36:13 + | +LL | let _ = std::mem::replace(&mut f.0, None); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::take()` instead: `f.0.take()` + +error: replacing an `Option` with `None` + --> tests/ui/mem_replace_option_with_none.rs:38:13 + | +LL | let _ = std::mem::replace(&mut *f, None); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::take()` instead: `(*f).take()` + +error: replacing an `Option` with `None` + --> tests/ui/mem_replace_option_with_none.rs:40:13 + | +LL | let _ = std::mem::replace(&mut b.opt, None); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::take()` instead: `b.opt.take()` + +error: aborting due to 5 previous errors + diff --git a/tests/ui/mem_replace_option_with_none_no_std.fixed b/tests/ui/mem_replace_option_with_none_no_std.fixed new file mode 100644 index 000000000000..a018547e920b --- /dev/null +++ b/tests/ui/mem_replace_option_with_none_no_std.fixed @@ -0,0 +1,13 @@ +#![warn(clippy::mem_replace_option_with_none)] +#![no_std] + +use core::mem; + +fn it_works() { + let mut an_option = Some(1); + let _ = an_option.take(); + //~^ mem_replace_option_with_none + let an_option = &mut Some(1); + let _ = an_option.take(); + //~^ mem_replace_option_with_none +} diff --git a/tests/ui/mem_replace_option_with_none_no_std.rs b/tests/ui/mem_replace_option_with_none_no_std.rs new file mode 100644 index 000000000000..a2cc68e49d7d --- /dev/null +++ b/tests/ui/mem_replace_option_with_none_no_std.rs @@ -0,0 +1,13 @@ +#![warn(clippy::mem_replace_option_with_none)] +#![no_std] + +use core::mem; + +fn it_works() { + let mut an_option = Some(1); + let _ = mem::replace(&mut an_option, None); + //~^ mem_replace_option_with_none + let an_option = &mut Some(1); + let _ = mem::replace(an_option, None); + //~^ mem_replace_option_with_none +} diff --git a/tests/ui/mem_replace_option_with_none_no_std.stderr b/tests/ui/mem_replace_option_with_none_no_std.stderr new file mode 100644 index 000000000000..085ef4462d5f --- /dev/null +++ b/tests/ui/mem_replace_option_with_none_no_std.stderr @@ -0,0 +1,17 @@ +error: replacing an `Option` with `None` + --> tests/ui/mem_replace_option_with_none_no_std.rs:8:13 + | +LL | let _ = mem::replace(&mut an_option, None); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::take()` instead: `an_option.take()` + | + = note: `-D clippy::mem-replace-option-with-none` implied by `-D warnings` + = help: to override `-D warnings` add `#[allow(clippy::mem_replace_option_with_none)]` + +error: replacing an `Option` with `None` + --> tests/ui/mem_replace_option_with_none_no_std.rs:11:13 + | +LL | let _ = mem::replace(an_option, None); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::take()` instead: `an_option.take()` + +error: aborting due to 2 previous errors + From d9825ad2752211070fd549e6da0ff66522f0d3cf Mon Sep 17 00:00:00 2001 From: Ada Alakbarova Date: Mon, 8 Jun 2026 23:31:38 +0200 Subject: [PATCH 5/6] extract `mem_replace_option_with_some` tests into their own file --- tests/ui/mem_replace.fixed | 24 +---------------- tests/ui/mem_replace.rs | 24 +---------------- tests/ui/mem_replace.stderr | 25 ++---------------- tests/ui/mem_replace_no_std.fixed | 2 +- tests/ui/mem_replace_no_std.rs | 2 +- tests/ui/mem_replace_option_with_some.fixed | 25 ++++++++++++++++++ tests/ui/mem_replace_option_with_some.rs | 25 ++++++++++++++++++ tests/ui/mem_replace_option_with_some.stderr | 23 ++++++++++++++++ .../mem_replace_option_with_some_no_std.fixed | 26 +++++++++++++++++++ .../ui/mem_replace_option_with_some_no_std.rs | 26 +++++++++++++++++++ ...mem_replace_option_with_some_no_std.stderr | 23 ++++++++++++++++ 11 files changed, 154 insertions(+), 71 deletions(-) create mode 100644 tests/ui/mem_replace_option_with_some.fixed create mode 100644 tests/ui/mem_replace_option_with_some.rs create mode 100644 tests/ui/mem_replace_option_with_some.stderr create mode 100644 tests/ui/mem_replace_option_with_some_no_std.fixed create mode 100644 tests/ui/mem_replace_option_with_some_no_std.rs create mode 100644 tests/ui/mem_replace_option_with_some_no_std.stderr diff --git a/tests/ui/mem_replace.fixed b/tests/ui/mem_replace.fixed index 1c5cd64f5ff3..361a45341fb5 100644 --- a/tests/ui/mem_replace.fixed +++ b/tests/ui/mem_replace.fixed @@ -1,4 +1,4 @@ -#![warn(clippy::mem_replace_option_with_some, clippy::mem_replace_with_default)] +#![warn(clippy::mem_replace_with_default)] use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList, VecDeque}; use std::mem; @@ -115,28 +115,6 @@ fn issue9824() { //~^ mem_replace_with_default } -#[clippy::msrv = "1.31"] -fn mem_replace_option_with_some() { - let mut an_option = Some(0); - let replaced = an_option.replace(1); - //~^ mem_replace_option_with_some - - let mut an_option = &mut Some(0); - let replaced = an_option.replace(1); - //~^ mem_replace_option_with_some - - let (mut opt1, mut opt2) = (Some(0), Some(0)); - let b = true; - let replaced = (if b { &mut opt1 } else { &mut opt2 }).replace(1); - //~^ mem_replace_option_with_some -} - -#[clippy::msrv = "1.30"] -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)); diff --git a/tests/ui/mem_replace.rs b/tests/ui/mem_replace.rs index 4d966fb58038..f84f4978b0e5 100644 --- a/tests/ui/mem_replace.rs +++ b/tests/ui/mem_replace.rs @@ -1,4 +1,4 @@ -#![warn(clippy::mem_replace_option_with_some, clippy::mem_replace_with_default)] +#![warn(clippy::mem_replace_with_default)] use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList, VecDeque}; use std::mem; @@ -115,28 +115,6 @@ fn issue9824() { //~^ mem_replace_with_default } -#[clippy::msrv = "1.31"] -fn mem_replace_option_with_some() { - let mut an_option = Some(0); - let replaced = mem::replace(&mut an_option, Some(1)); - //~^ mem_replace_option_with_some - - let mut an_option = &mut Some(0); - let replaced = mem::replace(an_option, Some(1)); - //~^ mem_replace_option_with_some - - let (mut opt1, mut opt2) = (Some(0), Some(0)); - let b = true; - let replaced = mem::replace(if b { &mut opt1 } else { &mut opt2 }, Some(1)); - //~^ mem_replace_option_with_some -} - -#[clippy::msrv = "1.30"] -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()); diff --git a/tests/ui/mem_replace.stderr b/tests/ui/mem_replace.stderr index 497144c67696..f12e337b5222 100644 --- a/tests/ui/mem_replace.stderr +++ b/tests/ui/mem_replace.stderr @@ -127,32 +127,11 @@ error: replacing a value of type `T` with `T::default()` LL | let _ = std::mem::replace(&mut b.val, String::default()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut b.val)` -error: replacing an `Option` with `Some(..)` - --> tests/ui/mem_replace.rs:121:20 - | -LL | let replaced = mem::replace(&mut an_option, Some(1)); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::replace()` instead: `an_option.replace(1)` - | - = note: `-D clippy::mem-replace-option-with-some` implied by `-D warnings` - = help: to override `-D warnings` add `#[allow(clippy::mem_replace_option_with_some)]` - -error: replacing an `Option` with `Some(..)` - --> tests/ui/mem_replace.rs:125:20 - | -LL | let replaced = mem::replace(an_option, Some(1)); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::replace()` instead: `an_option.replace(1)` - -error: replacing an `Option` with `Some(..)` - --> tests/ui/mem_replace.rs:130:20 - | -LL | let replaced = mem::replace(if b { &mut opt1 } else { &mut opt2 }, Some(1)); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::replace()` instead: `(if b { &mut opt1 } else { &mut opt2 }).replace(1)` - error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:142:20 + --> tests/ui/mem_replace.rs:120:20 | LL | let replaced = std::mem::replace(dbg!(&mut text), String::default()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(dbg!(&mut text))` -error: aborting due to 25 previous errors +error: aborting due to 22 previous errors diff --git a/tests/ui/mem_replace_no_std.fixed b/tests/ui/mem_replace_no_std.fixed index 419a9ba864b6..63851bb0a122 100644 --- a/tests/ui/mem_replace_no_std.fixed +++ b/tests/ui/mem_replace_no_std.fixed @@ -1,4 +1,4 @@ -#![warn(clippy::mem_replace_option_with_some, clippy::mem_replace_with_default)] +#![warn(clippy::mem_replace_with_default)] #![no_std] use core::mem; diff --git a/tests/ui/mem_replace_no_std.rs b/tests/ui/mem_replace_no_std.rs index 775f3af9433b..8734918ba7de 100644 --- a/tests/ui/mem_replace_no_std.rs +++ b/tests/ui/mem_replace_no_std.rs @@ -1,4 +1,4 @@ -#![warn(clippy::mem_replace_option_with_some, clippy::mem_replace_with_default)] +#![warn(clippy::mem_replace_with_default)] #![no_std] use core::mem; diff --git a/tests/ui/mem_replace_option_with_some.fixed b/tests/ui/mem_replace_option_with_some.fixed new file mode 100644 index 000000000000..dc0652ae3645 --- /dev/null +++ b/tests/ui/mem_replace_option_with_some.fixed @@ -0,0 +1,25 @@ +#![warn(clippy::mem_replace_option_with_some)] + +use std::mem; + +#[clippy::msrv = "1.31"] +fn main() { + let mut an_option = Some(0); + let replaced = an_option.replace(1); + //~^ mem_replace_option_with_some + + let mut an_option = &mut Some(0); + let replaced = an_option.replace(1); + //~^ mem_replace_option_with_some + + let (mut opt1, mut opt2) = (Some(0), Some(0)); + let b = true; + let replaced = (if b { &mut opt1 } else { &mut opt2 }).replace(1); + //~^ mem_replace_option_with_some +} + +#[clippy::msrv = "1.30"] +fn bad_msrv() { + let mut an_option = Some(0); + let replaced = mem::replace(&mut an_option, Some(1)); +} diff --git a/tests/ui/mem_replace_option_with_some.rs b/tests/ui/mem_replace_option_with_some.rs new file mode 100644 index 000000000000..dca284983795 --- /dev/null +++ b/tests/ui/mem_replace_option_with_some.rs @@ -0,0 +1,25 @@ +#![warn(clippy::mem_replace_option_with_some)] + +use std::mem; + +#[clippy::msrv = "1.31"] +fn main() { + let mut an_option = Some(0); + let replaced = mem::replace(&mut an_option, Some(1)); + //~^ mem_replace_option_with_some + + let mut an_option = &mut Some(0); + let replaced = mem::replace(an_option, Some(1)); + //~^ mem_replace_option_with_some + + let (mut opt1, mut opt2) = (Some(0), Some(0)); + let b = true; + let replaced = mem::replace(if b { &mut opt1 } else { &mut opt2 }, Some(1)); + //~^ mem_replace_option_with_some +} + +#[clippy::msrv = "1.30"] +fn bad_msrv() { + let mut an_option = Some(0); + let replaced = mem::replace(&mut an_option, Some(1)); +} diff --git a/tests/ui/mem_replace_option_with_some.stderr b/tests/ui/mem_replace_option_with_some.stderr new file mode 100644 index 000000000000..a064084c5fac --- /dev/null +++ b/tests/ui/mem_replace_option_with_some.stderr @@ -0,0 +1,23 @@ +error: replacing an `Option` with `Some(..)` + --> tests/ui/mem_replace_option_with_some.rs:8:20 + | +LL | let replaced = mem::replace(&mut an_option, Some(1)); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::replace()` instead: `an_option.replace(1)` + | + = note: `-D clippy::mem-replace-option-with-some` implied by `-D warnings` + = help: to override `-D warnings` add `#[allow(clippy::mem_replace_option_with_some)]` + +error: replacing an `Option` with `Some(..)` + --> tests/ui/mem_replace_option_with_some.rs:12:20 + | +LL | let replaced = mem::replace(an_option, Some(1)); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::replace()` instead: `an_option.replace(1)` + +error: replacing an `Option` with `Some(..)` + --> tests/ui/mem_replace_option_with_some.rs:17:20 + | +LL | let replaced = mem::replace(if b { &mut opt1 } else { &mut opt2 }, Some(1)); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::replace()` instead: `(if b { &mut opt1 } else { &mut opt2 }).replace(1)` + +error: aborting due to 3 previous errors + diff --git a/tests/ui/mem_replace_option_with_some_no_std.fixed b/tests/ui/mem_replace_option_with_some_no_std.fixed new file mode 100644 index 000000000000..7f882519aa62 --- /dev/null +++ b/tests/ui/mem_replace_option_with_some_no_std.fixed @@ -0,0 +1,26 @@ +#![warn(clippy::mem_replace_option_with_some)] +#![no_std] + +use core::mem; + +#[clippy::msrv = "1.31"] +fn it_works() { + let mut an_option = Some(0); + let replaced = an_option.replace(1); + //~^ mem_replace_option_with_some + + let mut an_option = &mut Some(0); + let replaced = an_option.replace(1); + //~^ mem_replace_option_with_some + + let (mut opt1, mut opt2) = (Some(0), Some(0)); + let b = true; + let replaced = (if b { &mut opt1 } else { &mut opt2 }).replace(1); + //~^ mem_replace_option_with_some +} + +#[clippy::msrv = "1.30"] +fn bad_msrv() { + let mut an_option = Some(0); + let replaced = mem::replace(&mut an_option, Some(1)); +} diff --git a/tests/ui/mem_replace_option_with_some_no_std.rs b/tests/ui/mem_replace_option_with_some_no_std.rs new file mode 100644 index 000000000000..539c911a7efd --- /dev/null +++ b/tests/ui/mem_replace_option_with_some_no_std.rs @@ -0,0 +1,26 @@ +#![warn(clippy::mem_replace_option_with_some)] +#![no_std] + +use core::mem; + +#[clippy::msrv = "1.31"] +fn it_works() { + let mut an_option = Some(0); + let replaced = mem::replace(&mut an_option, Some(1)); + //~^ mem_replace_option_with_some + + let mut an_option = &mut Some(0); + let replaced = mem::replace(an_option, Some(1)); + //~^ mem_replace_option_with_some + + let (mut opt1, mut opt2) = (Some(0), Some(0)); + let b = true; + let replaced = mem::replace(if b { &mut opt1 } else { &mut opt2 }, Some(1)); + //~^ mem_replace_option_with_some +} + +#[clippy::msrv = "1.30"] +fn bad_msrv() { + let mut an_option = Some(0); + let replaced = mem::replace(&mut an_option, Some(1)); +} diff --git a/tests/ui/mem_replace_option_with_some_no_std.stderr b/tests/ui/mem_replace_option_with_some_no_std.stderr new file mode 100644 index 000000000000..67965f3335b0 --- /dev/null +++ b/tests/ui/mem_replace_option_with_some_no_std.stderr @@ -0,0 +1,23 @@ +error: replacing an `Option` with `Some(..)` + --> tests/ui/mem_replace_option_with_some_no_std.rs:9:20 + | +LL | let replaced = mem::replace(&mut an_option, Some(1)); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::replace()` instead: `an_option.replace(1)` + | + = note: `-D clippy::mem-replace-option-with-some` implied by `-D warnings` + = help: to override `-D warnings` add `#[allow(clippy::mem_replace_option_with_some)]` + +error: replacing an `Option` with `Some(..)` + --> tests/ui/mem_replace_option_with_some_no_std.rs:13:20 + | +LL | let replaced = mem::replace(an_option, Some(1)); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::replace()` instead: `an_option.replace(1)` + +error: replacing an `Option` with `Some(..)` + --> tests/ui/mem_replace_option_with_some_no_std.rs:18:20 + | +LL | let replaced = mem::replace(if b { &mut opt1 } else { &mut opt2 }, Some(1)); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `Option::replace()` instead: `(if b { &mut opt1 } else { &mut opt2 }).replace(1)` + +error: aborting due to 3 previous errors + From 2ae28777f3ecde1311f3b39e72d5332bad430e1c Mon Sep 17 00:00:00 2001 From: Ada Alakbarova Date: Mon, 8 Jun 2026 23:31:38 +0200 Subject: [PATCH 6/6] rename the remains to `mem_replace_with_default.rs` --- tests/ui/mem_replace_no_std.fixed | 34 -------------- tests/ui/mem_replace_no_std.rs | 34 -------------- ...e.fixed => mem_replace_with_default.fixed} | 4 +- ...replace.rs => mem_replace_with_default.rs} | 4 +- ...stderr => mem_replace_with_default.stderr} | 44 +++++++++---------- ...o.rs => mem_replace_with_default_macro.rs} | 0 ... => mem_replace_with_default_macro.stderr} | 2 +- .../ui/mem_replace_with_default_no_std.fixed | 14 ++++++ tests/ui/mem_replace_with_default_no_std.rs | 14 ++++++ ...=> mem_replace_with_default_no_std.stderr} | 4 +- 10 files changed, 55 insertions(+), 99 deletions(-) delete mode 100644 tests/ui/mem_replace_no_std.fixed delete mode 100644 tests/ui/mem_replace_no_std.rs rename tests/ui/{mem_replace.fixed => mem_replace_with_default.fixed} (98%) rename tests/ui/{mem_replace.rs => mem_replace_with_default.rs} (98%) rename tests/ui/{mem_replace.stderr => mem_replace_with_default.stderr} (84%) rename tests/ui/{mem_replace_macro.rs => mem_replace_with_default_macro.rs} (100%) rename tests/ui/{mem_replace_macro.stderr => mem_replace_with_default_macro.stderr} (90%) create mode 100644 tests/ui/mem_replace_with_default_no_std.fixed create mode 100644 tests/ui/mem_replace_with_default_no_std.rs rename tests/ui/{mem_replace_no_std.stderr => mem_replace_with_default_no_std.stderr} (85%) diff --git a/tests/ui/mem_replace_no_std.fixed b/tests/ui/mem_replace_no_std.fixed deleted file mode 100644 index 63851bb0a122..000000000000 --- a/tests/ui/mem_replace_no_std.fixed +++ /dev/null @@ -1,34 +0,0 @@ -#![warn(clippy::mem_replace_with_default)] -#![no_std] - -use core::mem; - -fn replace_with_default() { - let mut refstr = "hello"; - let _ = core::mem::take(&mut refstr); - //~^ mem_replace_with_default - - let mut slice: &[i32] = &[1, 2, 3]; - let _ = core::mem::take(&mut slice); - //~^ mem_replace_with_default -} - -// lint is disabled for primitives because in this case `take` -// has no clear benefit over `replace` and sometimes is harder to read -fn dont_lint_primitive() { - let mut pbool = true; - let _ = mem::replace(&mut pbool, false); - - let mut pint = 5; - let _ = mem::replace(&mut pint, 0); -} - -fn issue9824() { - struct Bar { - val: u8, - } - - let mut b = Bar { val: 12 }; - // replace with default - let _ = mem::replace(&mut b.val, u8::default()); -} diff --git a/tests/ui/mem_replace_no_std.rs b/tests/ui/mem_replace_no_std.rs deleted file mode 100644 index 8734918ba7de..000000000000 --- a/tests/ui/mem_replace_no_std.rs +++ /dev/null @@ -1,34 +0,0 @@ -#![warn(clippy::mem_replace_with_default)] -#![no_std] - -use core::mem; - -fn replace_with_default() { - let mut refstr = "hello"; - let _ = mem::replace(&mut refstr, ""); - //~^ mem_replace_with_default - - let mut slice: &[i32] = &[1, 2, 3]; - let _ = mem::replace(&mut slice, &[]); - //~^ mem_replace_with_default -} - -// lint is disabled for primitives because in this case `take` -// has no clear benefit over `replace` and sometimes is harder to read -fn dont_lint_primitive() { - let mut pbool = true; - let _ = mem::replace(&mut pbool, false); - - let mut pint = 5; - let _ = mem::replace(&mut pint, 0); -} - -fn issue9824() { - struct Bar { - val: u8, - } - - let mut b = Bar { val: 12 }; - // replace with default - let _ = mem::replace(&mut b.val, u8::default()); -} diff --git a/tests/ui/mem_replace.fixed b/tests/ui/mem_replace_with_default.fixed similarity index 98% rename from tests/ui/mem_replace.fixed rename to tests/ui/mem_replace_with_default.fixed index 361a45341fb5..3270964816e2 100644 --- a/tests/ui/mem_replace.fixed +++ b/tests/ui/mem_replace_with_default.fixed @@ -3,7 +3,7 @@ use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList, VecDeque}; use std::mem; -fn replace_with_default() { +fn main() { let mut s = String::from("foo"); let _ = std::mem::take(&mut s); //~^ mem_replace_with_default @@ -87,8 +87,6 @@ fn dont_lint_not_used() { std::mem::replace(&mut s, String::default()); } -fn main() {} - #[clippy::msrv = "1.39"] fn msrv_1_39() { let mut s = String::from("foo"); diff --git a/tests/ui/mem_replace.rs b/tests/ui/mem_replace_with_default.rs similarity index 98% rename from tests/ui/mem_replace.rs rename to tests/ui/mem_replace_with_default.rs index f84f4978b0e5..ca8f0a668983 100644 --- a/tests/ui/mem_replace.rs +++ b/tests/ui/mem_replace_with_default.rs @@ -3,7 +3,7 @@ use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList, VecDeque}; use std::mem; -fn replace_with_default() { +fn main() { let mut s = String::from("foo"); let _ = std::mem::replace(&mut s, String::default()); //~^ mem_replace_with_default @@ -87,8 +87,6 @@ fn dont_lint_not_used() { std::mem::replace(&mut s, String::default()); } -fn main() {} - #[clippy::msrv = "1.39"] fn msrv_1_39() { let mut s = String::from("foo"); diff --git a/tests/ui/mem_replace.stderr b/tests/ui/mem_replace_with_default.stderr similarity index 84% rename from tests/ui/mem_replace.stderr rename to tests/ui/mem_replace_with_default.stderr index f12e337b5222..d0265a22e05f 100644 --- a/tests/ui/mem_replace.stderr +++ b/tests/ui/mem_replace_with_default.stderr @@ -1,5 +1,5 @@ error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:8:13 + --> tests/ui/mem_replace_with_default.rs:8:13 | LL | let _ = std::mem::replace(&mut s, String::default()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut s)` @@ -8,127 +8,127 @@ LL | let _ = std::mem::replace(&mut s, String::default()); = help: to override `-D warnings` add `#[allow(clippy::mem_replace_with_default)]` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:10:13 + --> tests/ui/mem_replace_with_default.rs:10:13 | LL | let _ = std::mem::replace(&mut s, String::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut s)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:14:13 + --> tests/ui/mem_replace_with_default.rs:14:13 | LL | let _ = std::mem::replace(s, String::default()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(s)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:16:13 + --> tests/ui/mem_replace_with_default.rs:16:13 | LL | let _ = std::mem::replace(s, String::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(s)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:18:13 + --> tests/ui/mem_replace_with_default.rs:18:13 | LL | let _ = std::mem::replace(s, Default::default()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(s)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:22:13 + --> tests/ui/mem_replace_with_default.rs:22:13 | LL | let _ = std::mem::replace(&mut v, Vec::default()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut v)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:24:13 + --> tests/ui/mem_replace_with_default.rs:24:13 | LL | let _ = std::mem::replace(&mut v, Default::default()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut v)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:26:13 + --> tests/ui/mem_replace_with_default.rs:26:13 | LL | let _ = std::mem::replace(&mut v, Vec::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut v)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:28:13 + --> tests/ui/mem_replace_with_default.rs:28:13 | LL | let _ = std::mem::replace(&mut v, vec![]); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut v)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:32:13 + --> tests/ui/mem_replace_with_default.rs:32:13 | LL | let _ = std::mem::replace(&mut hash_map, HashMap::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut hash_map)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:36:13 + --> tests/ui/mem_replace_with_default.rs:36:13 | LL | let _ = std::mem::replace(&mut btree_map, BTreeMap::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut btree_map)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:40:13 + --> tests/ui/mem_replace_with_default.rs:40:13 | LL | let _ = std::mem::replace(&mut vd, VecDeque::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut vd)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:44:13 + --> tests/ui/mem_replace_with_default.rs:44:13 | LL | let _ = std::mem::replace(&mut hash_set, HashSet::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut hash_set)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:48:13 + --> tests/ui/mem_replace_with_default.rs:48:13 | LL | let _ = std::mem::replace(&mut btree_set, BTreeSet::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut btree_set)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:52:13 + --> tests/ui/mem_replace_with_default.rs:52:13 | LL | let _ = std::mem::replace(&mut list, LinkedList::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut list)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:56:13 + --> tests/ui/mem_replace_with_default.rs:56:13 | LL | let _ = std::mem::replace(&mut binary_heap, BinaryHeap::new()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut binary_heap)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:60:13 + --> tests/ui/mem_replace_with_default.rs:60:13 | LL | let _ = std::mem::replace(&mut tuple, (vec![], BinaryHeap::new())); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut tuple)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:64:13 + --> tests/ui/mem_replace_with_default.rs:64:13 | LL | let _ = std::mem::replace(&mut refstr, ""); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut refstr)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:68:13 + --> tests/ui/mem_replace_with_default.rs:68:13 | LL | let _ = std::mem::replace(&mut slice, &[]); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut slice)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:101:13 + --> tests/ui/mem_replace_with_default.rs:99:13 | LL | let _ = std::mem::replace(&mut s, String::default()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut s)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:114:13 + --> tests/ui/mem_replace_with_default.rs:112:13 | LL | let _ = std::mem::replace(&mut b.val, String::default()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(&mut b.val)` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace.rs:120:20 + --> tests/ui/mem_replace_with_default.rs:118:20 | LL | let replaced = std::mem::replace(dbg!(&mut text), String::default()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `std::mem::take` instead: `std::mem::take(dbg!(&mut text))` diff --git a/tests/ui/mem_replace_macro.rs b/tests/ui/mem_replace_with_default_macro.rs similarity index 100% rename from tests/ui/mem_replace_macro.rs rename to tests/ui/mem_replace_with_default_macro.rs diff --git a/tests/ui/mem_replace_macro.stderr b/tests/ui/mem_replace_with_default_macro.stderr similarity index 90% rename from tests/ui/mem_replace_macro.stderr rename to tests/ui/mem_replace_with_default_macro.stderr index b03853a49ec4..a443fc1c2fd2 100644 --- a/tests/ui/mem_replace_macro.stderr +++ b/tests/ui/mem_replace_with_default_macro.stderr @@ -1,5 +1,5 @@ error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace_macro.rs:10:21 + --> tests/ui/mem_replace_with_default_macro.rs:10:21 | LL | let _ = inline!(std::mem::replace($s, Default::default())); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/tests/ui/mem_replace_with_default_no_std.fixed b/tests/ui/mem_replace_with_default_no_std.fixed new file mode 100644 index 000000000000..0b8171df83a7 --- /dev/null +++ b/tests/ui/mem_replace_with_default_no_std.fixed @@ -0,0 +1,14 @@ +#![warn(clippy::mem_replace_with_default)] +#![no_std] + +use core::mem; + +fn it_works() { + let mut refstr = "hello"; + let _ = core::mem::take(&mut refstr); + //~^ mem_replace_with_default + + let mut slice: &[i32] = &[1, 2, 3]; + let _ = core::mem::take(&mut slice); + //~^ mem_replace_with_default +} diff --git a/tests/ui/mem_replace_with_default_no_std.rs b/tests/ui/mem_replace_with_default_no_std.rs new file mode 100644 index 000000000000..621152f01634 --- /dev/null +++ b/tests/ui/mem_replace_with_default_no_std.rs @@ -0,0 +1,14 @@ +#![warn(clippy::mem_replace_with_default)] +#![no_std] + +use core::mem; + +fn it_works() { + let mut refstr = "hello"; + let _ = mem::replace(&mut refstr, ""); + //~^ mem_replace_with_default + + let mut slice: &[i32] = &[1, 2, 3]; + let _ = mem::replace(&mut slice, &[]); + //~^ mem_replace_with_default +} diff --git a/tests/ui/mem_replace_no_std.stderr b/tests/ui/mem_replace_with_default_no_std.stderr similarity index 85% rename from tests/ui/mem_replace_no_std.stderr rename to tests/ui/mem_replace_with_default_no_std.stderr index 90e8ce3f5e48..2d1159a7e9d9 100644 --- a/tests/ui/mem_replace_no_std.stderr +++ b/tests/ui/mem_replace_with_default_no_std.stderr @@ -1,5 +1,5 @@ error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace_no_std.rs:8:13 + --> tests/ui/mem_replace_with_default_no_std.rs:8:13 | LL | let _ = mem::replace(&mut refstr, ""); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `core::mem::take` instead: `core::mem::take(&mut refstr)` @@ -8,7 +8,7 @@ LL | let _ = mem::replace(&mut refstr, ""); = help: to override `-D warnings` add `#[allow(clippy::mem_replace_with_default)]` error: replacing a value of type `T` with `T::default()` - --> tests/ui/mem_replace_no_std.rs:12:13 + --> tests/ui/mem_replace_with_default_no_std.rs:12:13 | LL | let _ = mem::replace(&mut slice, &[]); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `core::mem::take` instead: `core::mem::take(&mut slice)`