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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
#![warn(clippy::manual_unwrap_or_default)]
#![allow(clippy::unnecessary_literal_unwrap)]
fn main() {
let x: Option<Vec<String>> = None;
match x {
//~^ manual_unwrap_or_default
Some(v) => v,
None => Vec::default(),
};
let x: Option<Vec<String>> = None;
match x {
//~^ manual_unwrap_or_default
Some(v) => v,
_ => Vec::default(),
};
let x: Option<String> = None;
match x {
//~^ manual_unwrap_or_default
Some(v) => v,
None => String::new(),
};
let x: Option<Vec<String>> = None;
match x {
//~^ manual_unwrap_or_default
None => Vec::default(),
Some(v) => v,
};
let x: Option<Vec<String>> = None;
if let Some(v) = x {
//~^ manual_unwrap_or_default
v
} else {
Vec::default()
};
// Issue #12564
// No error as &Vec<_> doesn't implement std::default::Default
let mut map = std::collections::HashMap::from([(0, vec![0; 3]), (1, vec![1; 3]), (2, vec![2])]);
let x: &[_] = if let Some(x) = map.get(&0) { x } else { &[] };
// Same code as above written using match.
let x: &[_] = match map.get(&0) {
Some(x) => x,
None => &[],
};
let x: Result<String, i64> = Ok(String::new());
match x {
//~^ manual_unwrap_or_default
Ok(v) => v,
Err(_) => String::new(),
};
let x: Result<String, i64> = Ok(String::new());
if let Ok(v) = x {
//~^ manual_unwrap_or_default
v
} else {
String::new()
};
}
// Issue #12531
unsafe fn no_deref_ptr(a: Option<i32>, b: *const Option<i32>) -> i32 {
unsafe {
match a {
// `*b` being correct depends on `a == Some(_)`
Some(_) => match *b {
//~^ manual_unwrap_or_default
Some(v) => v,
_ => 0,
},
_ => 0,
}
}
}
const fn issue_12568(opt: Option<bool>) -> bool {
match opt {
Some(s) => s,
None => false,
}
}
fn issue_12569() {
let match_none_se = match 1u32.checked_div(0) {
Some(v) => v,
None => {
println!("important");
0
},
};
let match_some_se = match 1u32.checked_div(0) {
Some(v) => {
println!("important");
v
},
None => 0,
};
let iflet_else_se = if let Some(v) = 1u32.checked_div(0) {
v
} else {
println!("important");
0
};
let iflet_then_se = if let Some(v) = 1u32.checked_div(0) {
println!("important");
v
} else {
0
};
}
// Should not warn!
fn issue_12928() {
let x = Some((1, 2));
let y = if let Some((a, _)) = x { a } else { 0 };
let y = if let Some((a, ..)) = x { a } else { 0 };
let x = Some([1, 2]);
let y = if let Some([a, _]) = x { a } else { 0 };
let y = if let Some([a, ..]) = x { a } else { 0 };
struct X {
a: u8,
b: u8,
}
let x = Some(X { a: 0, b: 0 });
let y = if let Some(X { a, .. }) = x { a } else { 0 };
struct Y(u8, u8);
let x = Some(Y(0, 0));
let y = if let Some(Y(a, _)) = x { a } else { 0 };
let y = if let Some(Y(a, ..)) = x { a } else { 0 };
}
// For symmetry with `manual_unwrap_or` test
fn allowed_manual_unwrap_or_zero() -> u32 {
if let Some(x) = Some(42) {
//~^ manual_unwrap_or_default
x
} else {
0
}
}
mod issue14716 {
struct Foo {
name: Option<String>,
}
fn bar(project: &Foo) {
let _name = match project.name {
Some(ref x) => x,
None => "",
};
}
}
|