Skip to content

Commit

Permalink
Don't move yield or inline assembly into closure
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarcho committed Mar 1, 2021
1 parent a3278a1 commit 2c485e3
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 20 deletions.
7 changes: 6 additions & 1 deletion clippy_lints/src/manual_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,12 @@ fn can_move_expr_to_closure(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> boo

fn visit_expr(&mut self, e: &'tcx Expr<'_>) {
match e.kind {
ExprKind::Break(..) | ExprKind::Continue(_) | ExprKind::Ret(_) => {
ExprKind::Break(..)
| ExprKind::Continue(_)
| ExprKind::Ret(_)
| ExprKind::Yield(..)
| ExprKind::InlineAsm(_)
| ExprKind::LlvmInlineAsm(_) => {
self.make_closure = false;
},
// Accessing a field of a local value can only be done if the type isn't
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/manual_map_option.fixed
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// edition:2018
// run-rustfix

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

option_env!("").map(String::from);

// #6819
async fn f2(x: u32) -> u32 {
x
}

async fn f3() {
match Some(0) {
Some(x) => Some(f2(x).await),
None => None,
};
}
}
13 changes: 13 additions & 0 deletions tests/ui/manual_map_option.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// edition:2018
// run-rustfix

#![warn(clippy::manual_map)]
Expand Down Expand Up @@ -173,4 +174,16 @@ fn main() {
Some(x) => Some(String::from(x)),
None => None,
};

// #6819
async fn f2(x: u32) -> u32 {
x
}

async fn f3() {
match Some(0) {
Some(x) => Some(f2(x).await),
None => None,
};
}
}
38 changes: 19 additions & 19 deletions tests/ui/manual_map_option.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: manual implementation of `Option::map`
--> $DIR/manual_map_option.rs:13:5
--> $DIR/manual_map_option.rs:14:5
|
LL | / match Some(0) {
LL | | Some(_) => Some(2),
Expand All @@ -10,7 +10,7 @@ LL | | };
= note: `-D clippy::manual-map` implied by `-D warnings`

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

0 comments on commit 2c485e3

Please sign in to comment.