Skip to content

Commit 3e5dcba

Browse files
committed
Small cleanup of option_if_let_else and additional tests.
1 parent 8b3ca9a commit 3e5dcba

File tree

4 files changed

+57
-17
lines changed

4 files changed

+57
-17
lines changed

clippy_lints/src/option_if_let_else.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ fn detect_option_if_let_else<'tcx>(
174174
ExprKind::Field(e, _) | ExprKind::AddrOf(_, _, e) => Some(e),
175175
_ => None,
176176
});
177-
if let ExprKind::Path(QPath::Resolved(None, Path { res: Res::Local(l), .. })) = e.kind {
178-
match some_captures.get(l)
179-
.or_else(|| (method_sugg == "map_or_else").then(|| ()).and_then(|_| none_captures.get(l)))
177+
if let ExprKind::Path(QPath::Resolved(None, Path { res: Res::Local(local_id), .. })) = e.kind {
178+
match some_captures.get(local_id)
179+
.or_else(|| (method_sugg == "map_or_else").then(|| ()).and_then(|_| none_captures.get(local_id)))
180180
{
181181
Some(CaptureKind::Value | CaptureKind::Ref(Mutability::Mut)) => return None,
182182
Some(CaptureKind::Ref(Mutability::Not)) if as_mut => return None,

tests/ui/option_if_let_else.fixed

+20
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// edition:2018
12
// run-rustfix
23
#![warn(clippy::option_if_let_else)]
34
#![allow(clippy::redundant_closure)]
@@ -128,4 +129,23 @@ fn main() {
128129
let _s = s;
129130
10
130131
};
132+
133+
let mut s = Some(String::new());
134+
// Don't lint, `Some` branch mutably borrows `s`, but else branch also borrows `s`
135+
let _ = if let Some(x) = &mut s {
136+
x.push_str("test");
137+
x.len()
138+
} else {
139+
let _s = &s;
140+
10
141+
};
142+
143+
async fn _f1(x: u32) -> u32 {
144+
x
145+
}
146+
147+
async fn _f2() {
148+
// Don't lint. `await` can't be moved into a closure.
149+
let _ = if let Some(x) = Some(0) { _f1(x).await } else { 0 };
150+
}
131151
}

tests/ui/option_if_let_else.rs

+20
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// edition:2018
12
// run-rustfix
23
#![warn(clippy::option_if_let_else)]
34
#![allow(clippy::redundant_closure)]
@@ -153,4 +154,23 @@ fn main() {
153154
let _s = s;
154155
10
155156
};
157+
158+
let mut s = Some(String::new());
159+
// Don't lint, `Some` branch mutably borrows `s`, but else branch also borrows `s`
160+
let _ = if let Some(x) = &mut s {
161+
x.push_str("test");
162+
x.len()
163+
} else {
164+
let _s = &s;
165+
10
166+
};
167+
168+
async fn _f1(x: u32) -> u32 {
169+
x
170+
}
171+
172+
async fn _f2() {
173+
// Don't lint. `await` can't be moved into a closure.
174+
let _ = if let Some(x) = Some(0) { _f1(x).await } else { 0 };
175+
}
156176
}

tests/ui/option_if_let_else.stderr

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: use Option::map_or instead of an if let/else
2-
--> $DIR/option_if_let_else.rs:7:5
2+
--> $DIR/option_if_let_else.rs:8:5
33
|
44
LL | / if let Some(x) = string {
55
LL | | (true, x)
@@ -11,19 +11,19 @@ LL | | }
1111
= note: `-D clippy::option-if-let-else` implied by `-D warnings`
1212

1313
error: use Option::map_or instead of an if let/else
14-
--> $DIR/option_if_let_else.rs:25:13
14+
--> $DIR/option_if_let_else.rs:26:13
1515
|
1616
LL | let _ = if let Some(s) = *string { s.len() } else { 0 };
1717
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `string.map_or(0, |s| s.len())`
1818

1919
error: use Option::map_or instead of an if let/else
20-
--> $DIR/option_if_let_else.rs:26:13
20+
--> $DIR/option_if_let_else.rs:27:13
2121
|
2222
LL | let _ = if let Some(s) = &num { s } else { &0 };
2323
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `num.as_ref().map_or(&0, |s| s)`
2424

2525
error: use Option::map_or instead of an if let/else
26-
--> $DIR/option_if_let_else.rs:27:13
26+
--> $DIR/option_if_let_else.rs:28:13
2727
|
2828
LL | let _ = if let Some(s) = &mut num {
2929
| _____________^
@@ -43,13 +43,13 @@ LL ~ });
4343
|
4444

4545
error: use Option::map_or instead of an if let/else
46-
--> $DIR/option_if_let_else.rs:33:13
46+
--> $DIR/option_if_let_else.rs:34:13
4747
|
4848
LL | let _ = if let Some(ref s) = num { s } else { &0 };
4949
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `num.as_ref().map_or(&0, |s| s)`
5050

5151
error: use Option::map_or instead of an if let/else
52-
--> $DIR/option_if_let_else.rs:34:13
52+
--> $DIR/option_if_let_else.rs:35:13
5353
|
5454
LL | let _ = if let Some(mut s) = num {
5555
| _____________^
@@ -69,7 +69,7 @@ LL ~ });
6969
|
7070

7171
error: use Option::map_or instead of an if let/else
72-
--> $DIR/option_if_let_else.rs:40:13
72+
--> $DIR/option_if_let_else.rs:41:13
7373
|
7474
LL | let _ = if let Some(ref mut s) = num {
7575
| _____________^
@@ -89,7 +89,7 @@ LL ~ });
8989
|
9090

9191
error: use Option::map_or instead of an if let/else
92-
--> $DIR/option_if_let_else.rs:49:5
92+
--> $DIR/option_if_let_else.rs:50:5
9393
|
9494
LL | / if let Some(x) = arg {
9595
LL | | let y = x * x;
@@ -108,7 +108,7 @@ LL + })
108108
|
109109

110110
error: use Option::map_or_else instead of an if let/else
111-
--> $DIR/option_if_let_else.rs:62:13
111+
--> $DIR/option_if_let_else.rs:63:13
112112
|
113113
LL | let _ = if let Some(x) = arg {
114114
| _____________^
@@ -120,7 +120,7 @@ LL | | };
120120
| |_____^ help: try: `arg.map_or_else(|| side_effect(), |x| x)`
121121

122122
error: use Option::map_or_else instead of an if let/else
123-
--> $DIR/option_if_let_else.rs:71:13
123+
--> $DIR/option_if_let_else.rs:72:13
124124
|
125125
LL | let _ = if let Some(x) = arg {
126126
| _____________^
@@ -143,13 +143,13 @@ LL ~ }, |x| x * x * x * x);
143143
|
144144

145145
error: use Option::map_or instead of an if let/else
146-
--> $DIR/option_if_let_else.rs:100:13
146+
--> $DIR/option_if_let_else.rs:101:13
147147
|
148148
LL | let _ = if let Some(x) = optional { x + 2 } else { 5 };
149149
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `optional.map_or(5, |x| x + 2)`
150150

151151
error: use Option::map_or instead of an if let/else
152-
--> $DIR/option_if_let_else.rs:109:13
152+
--> $DIR/option_if_let_else.rs:110:13
153153
|
154154
LL | let _ = if let Some(x) = Some(0) {
155155
| _____________^
@@ -171,13 +171,13 @@ LL ~ });
171171
|
172172

173173
error: use Option::map_or_else instead of an if let/else
174-
--> $DIR/option_if_let_else.rs:137:13
174+
--> $DIR/option_if_let_else.rs:138:13
175175
|
176176
LL | let _ = if let Some(x) = Some(0) { s.len() + x } else { s.len() };
177177
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Some(0).map_or_else(|| s.len(), |x| s.len() + x)`
178178

179179
error: use Option::map_or instead of an if let/else
180-
--> $DIR/option_if_let_else.rs:141:13
180+
--> $DIR/option_if_let_else.rs:142:13
181181
|
182182
LL | let _ = if let Some(x) = Some(0) {
183183
| _____________^

0 commit comments

Comments
 (0)