diff options
| author | bors <bors@rust-lang.org> | 2024-01-16 06:26:29 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-01-16 06:26:29 +0000 |
| commit | ecb0311fccae4d26ea448a61298b3ae6ffe233be (patch) | |
| tree | 780e139005961788f948b3b987d0d66583fc8705 /tests/ui/mem_replace_no_std.fixed | |
| parent | 771a2a965c300f91400f341d831797c3c41705a9 (diff) | |
| parent | 874f851ac5d82a5e2c68e46ddf850b8047f40415 (diff) | |
Auto merge of #12149 - GuillaumeGomez:core-std-suggestions, r=llogiq
Correctly suggest std or core path depending if this is a `no_std` crate A few lints emit suggestions using `std` paths whether or not this is a `no_std` crate, which is an issue when running `rustfix` afterwards. So in case this is an item that is defined in both `std` and `core`, we need to check if the crate is `no_std` to emit the right path. r? `@llogiq` changelog: Correctly suggest std or core path depending if this is a `no_std` crate
Diffstat (limited to 'tests/ui/mem_replace_no_std.fixed')
| -rw-r--r-- | tests/ui/mem_replace_no_std.fixed | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/tests/ui/mem_replace_no_std.fixed b/tests/ui/mem_replace_no_std.fixed new file mode 100644 index 00000000000..c970f2ba281 --- /dev/null +++ b/tests/ui/mem_replace_no_std.fixed @@ -0,0 +1,82 @@ +#![allow(unused)] +#![warn( + clippy::all, + clippy::style, + clippy::mem_replace_option_with_none, + 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); + let _ = an_option.take(); + let an_option = &mut Some(1); + let _ = an_option.take(); +} + +fn replace_with_default() { + let mut refstr = "hello"; + let _ = core::mem::take(&mut refstr); + + let mut slice: &[i32] = &[1, 2, 3]; + let _ = core::mem::take(&mut slice); +} + +// 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 main() { + replace_option_with_none(); + replace_with_default(); + 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<u8>, + 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(); + let _ = (*f).take(); + let _ = b.opt.take(); + // replace with default + let _ = mem::replace(&mut b.val, u8::default()); +} |
