Skip to content

Commit 2c485e3

Browse files
committed
Don't move yield or inline assembly into closure
1 parent a3278a1 commit 2c485e3

File tree

4 files changed

+51
-20
lines changed

4 files changed

+51
-20
lines changed

clippy_lints/src/manual_map.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,12 @@ fn can_move_expr_to_closure(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> boo
203203

204204
fn visit_expr(&mut self, e: &'tcx Expr<'_>) {
205205
match e.kind {
206-
ExprKind::Break(..) | ExprKind::Continue(_) | ExprKind::Ret(_) => {
206+
ExprKind::Break(..)
207+
| ExprKind::Continue(_)
208+
| ExprKind::Ret(_)
209+
| ExprKind::Yield(..)
210+
| ExprKind::InlineAsm(_)
211+
| ExprKind::LlvmInlineAsm(_) => {
207212
self.make_closure = false;
208213
},
209214
// Accessing a field of a local value can only be done if the type isn't

tests/ui/manual_map_option.fixed

+13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// edition:2018
12
// run-rustfix
23

34
#![warn(clippy::manual_map)]
@@ -115,4 +116,16 @@ fn main() {
115116
Some(0).map(|x| vec![x]);
116117

117118
option_env!("").map(String::from);
119+
120+
// #6819
121+
async fn f2(x: u32) -> u32 {
122+
x
123+
}
124+
125+
async fn f3() {
126+
match Some(0) {
127+
Some(x) => Some(f2(x).await),
128+
None => None,
129+
};
130+
}
118131
}

tests/ui/manual_map_option.rs

+13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// edition:2018
12
// run-rustfix
23

34
#![warn(clippy::manual_map)]
@@ -173,4 +174,16 @@ fn main() {
173174
Some(x) => Some(String::from(x)),
174175
None => None,
175176
};
177+
178+
// #6819
179+
async fn f2(x: u32) -> u32 {
180+
x
181+
}
182+
183+
async fn f3() {
184+
match Some(0) {
185+
Some(x) => Some(f2(x).await),
186+
None => None,
187+
};
188+
}
176189
}

tests/ui/manual_map_option.stderr

+19-19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: manual implementation of `Option::map`
2-
--> $DIR/manual_map_option.rs:13:5
2+
--> $DIR/manual_map_option.rs:14:5
33
|
44
LL | / match Some(0) {
55
LL | | Some(_) => Some(2),
@@ -10,7 +10,7 @@ LL | | };
1010
= note: `-D clippy::manual-map` implied by `-D warnings`
1111

1212
error: manual implementation of `Option::map`
13-
--> $DIR/manual_map_option.rs:18:5
13+
--> $DIR/manual_map_option.rs:19:5
1414
|
1515
LL | / match Some(0) {
1616
LL | | Some(x) => Some(x + 1),
@@ -19,7 +19,7 @@ LL | | };
1919
| |_____^ help: try this: `Some(0).map(|x| x + 1)`
2020

2121
error: manual implementation of `Option::map`
22-
--> $DIR/manual_map_option.rs:23:5
22+
--> $DIR/manual_map_option.rs:24:5
2323
|
2424
LL | / match Some("") {
2525
LL | | Some(x) => Some(x.is_empty()),
@@ -28,7 +28,7 @@ LL | | };
2828
| |_____^ help: try this: `Some("").map(|x| x.is_empty())`
2929

3030
error: manual implementation of `Option::map`
31-
--> $DIR/manual_map_option.rs:28:5
31+
--> $DIR/manual_map_option.rs:29:5
3232
|
3333
LL | / if let Some(x) = Some(0) {
3434
LL | | Some(!x)
@@ -38,7 +38,7 @@ LL | | };
3838
| |_____^ help: try this: `Some(0).map(|x| !x)`
3939

4040
error: manual implementation of `Option::map`
41-
--> $DIR/manual_map_option.rs:35:5
41+
--> $DIR/manual_map_option.rs:36:5
4242
|
4343
LL | / match Some(0) {
4444
LL | | Some(x) => { Some(std::convert::identity(x)) }
@@ -47,7 +47,7 @@ LL | | };
4747
| |_____^ help: try this: `Some(0).map(std::convert::identity)`
4848

4949
error: manual implementation of `Option::map`
50-
--> $DIR/manual_map_option.rs:40:5
50+
--> $DIR/manual_map_option.rs:41:5
5151
|
5252
LL | / match Some(&String::new()) {
5353
LL | | Some(x) => Some(str::len(x)),
@@ -56,7 +56,7 @@ LL | | };
5656
| |_____^ help: try this: `Some(&String::new()).map(|x| str::len(x))`
5757

5858
error: manual implementation of `Option::map`
59-
--> $DIR/manual_map_option.rs:50:5
59+
--> $DIR/manual_map_option.rs:51:5
6060
|
6161
LL | / match &Some([0, 1]) {
6262
LL | | Some(x) => Some(x[0]),
@@ -65,7 +65,7 @@ LL | | };
6565
| |_____^ help: try this: `Some([0, 1]).as_ref().map(|x| x[0])`
6666

6767
error: manual implementation of `Option::map`
68-
--> $DIR/manual_map_option.rs:55:5
68+
--> $DIR/manual_map_option.rs:56:5
6969
|
7070
LL | / match &Some(0) {
7171
LL | | &Some(x) => Some(x * 2),
@@ -74,7 +74,7 @@ LL | | };
7474
| |_____^ help: try this: `Some(0).map(|x| x * 2)`
7575

7676
error: manual implementation of `Option::map`
77-
--> $DIR/manual_map_option.rs:60:5
77+
--> $DIR/manual_map_option.rs:61:5
7878
|
7979
LL | / match Some(String::new()) {
8080
LL | | Some(ref x) => Some(x.is_empty()),
@@ -83,7 +83,7 @@ LL | | };
8383
| |_____^ help: try this: `Some(String::new()).as_ref().map(|x| x.is_empty())`
8484

8585
error: manual implementation of `Option::map`
86-
--> $DIR/manual_map_option.rs:65:5
86+
--> $DIR/manual_map_option.rs:66:5
8787
|
8888
LL | / match &&Some(String::new()) {
8989
LL | | Some(x) => Some(x.len()),
@@ -92,7 +92,7 @@ LL | | };
9292
| |_____^ help: try this: `Some(String::new()).as_ref().map(|x| x.len())`
9393

9494
error: manual implementation of `Option::map`
95-
--> $DIR/manual_map_option.rs:70:5
95+
--> $DIR/manual_map_option.rs:71:5
9696
|
9797
LL | / match &&Some(0) {
9898
LL | | &&Some(x) => Some(x + x),
@@ -101,7 +101,7 @@ LL | | };
101101
| |_____^ help: try this: `Some(0).map(|x| x + x)`
102102

103103
error: manual implementation of `Option::map`
104-
--> $DIR/manual_map_option.rs:83:9
104+
--> $DIR/manual_map_option.rs:84:9
105105
|
106106
LL | / match &mut Some(String::new()) {
107107
LL | | Some(x) => Some(x.push_str("")),
@@ -110,7 +110,7 @@ LL | | };
110110
| |_________^ help: try this: `Some(String::new()).as_mut().map(|x| x.push_str(""))`
111111

112112
error: manual implementation of `Option::map`
113-
--> $DIR/manual_map_option.rs:89:5
113+
--> $DIR/manual_map_option.rs:90:5
114114
|
115115
LL | / match &mut Some(String::new()) {
116116
LL | | Some(ref x) => Some(x.len()),
@@ -119,7 +119,7 @@ LL | | };
119119
| |_____^ help: try this: `Some(String::new()).as_ref().map(|x| x.len())`
120120

121121
error: manual implementation of `Option::map`
122-
--> $DIR/manual_map_option.rs:94:5
122+
--> $DIR/manual_map_option.rs:95:5
123123
|
124124
LL | / match &mut &Some(String::new()) {
125125
LL | | Some(x) => Some(x.is_empty()),
@@ -128,7 +128,7 @@ LL | | };
128128
| |_____^ help: try this: `Some(String::new()).as_ref().map(|x| x.is_empty())`
129129

130130
error: manual implementation of `Option::map`
131-
--> $DIR/manual_map_option.rs:99:5
131+
--> $DIR/manual_map_option.rs:100:5
132132
|
133133
LL | / match Some((0, 1, 2)) {
134134
LL | | Some((x, y, z)) => Some(x + y + z),
@@ -137,7 +137,7 @@ LL | | };
137137
| |_____^ help: try this: `Some((0, 1, 2)).map(|(x, y, z)| x + y + z)`
138138

139139
error: manual implementation of `Option::map`
140-
--> $DIR/manual_map_option.rs:104:5
140+
--> $DIR/manual_map_option.rs:105:5
141141
|
142142
LL | / match Some([1, 2, 3]) {
143143
LL | | Some([first, ..]) => Some(first),
@@ -146,7 +146,7 @@ LL | | };
146146
| |_____^ help: try this: `Some([1, 2, 3]).map(|[first, ..]| first)`
147147

148148
error: manual implementation of `Option::map`
149-
--> $DIR/manual_map_option.rs:109:5
149+
--> $DIR/manual_map_option.rs:110:5
150150
|
151151
LL | / match &Some((String::new(), "test")) {
152152
LL | | Some((x, y)) => Some((y, x)),
@@ -155,7 +155,7 @@ LL | | };
155155
| |_____^ help: try this: `Some((String::new(), "test")).as_ref().map(|(x, y)| (y, x))`
156156

157157
error: manual implementation of `Option::map`
158-
--> $DIR/manual_map_option.rs:167:5
158+
--> $DIR/manual_map_option.rs:168:5
159159
|
160160
LL | / match Some(0) {
161161
LL | | Some(x) => Some(vec![x]),
@@ -164,7 +164,7 @@ LL | | };
164164
| |_____^ help: try this: `Some(0).map(|x| vec![x])`
165165

166166
error: manual implementation of `Option::map`
167-
--> $DIR/manual_map_option.rs:172:5
167+
--> $DIR/manual_map_option.rs:173:5
168168
|
169169
LL | / match option_env!("") {
170170
LL | | Some(x) => Some(String::from(x)),

0 commit comments

Comments
 (0)