blob: 669450e59d8b0f5dd9105152b55081d7520d4344 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
#![allow(unused, clippy::needless_lifetimes)]
#![warn(
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();
//~^ 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);
//~^ 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 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();
//~^ 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 _ = mem::replace(&mut b.val, u8::default());
}
|