-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When expecting closure argument but finding block provide suggestion
Detect if there is a potential typo where the `{` meant to open the closure body was written before the body. ``` error[E0277]: expected a `FnOnce<({integer},)>` closure, found `Option<usize>` --> $DIR/ruby_style_closure_successful_parse.rs:3:31 | LL | let p = Some(45).and_then({|x| | ______________________--------_^ | | | | | required by a bound introduced by this call LL | | 1 + 1; LL | | Some(x * 2) | | ----------- this tail expression is of type `Option<usize>` LL | | }); | |_____^ expected an `FnOnce<({integer},)>` closure, found `Option<usize>` | = help: the trait `FnOnce<({integer},)>` is not implemented for `Option<usize>` note: required by a bound in `Option::<T>::and_then` --> $SRC_DIR/core/src/option.rs:LL:COL help: you might have meant to open the closure body instead of placing a closure within a block | LL - let p = Some(45).and_then({|x| LL + let p = Some(45).and_then(|x| { | ``` Detect the potential typo where the closure header is missing. ``` error[E0277]: expected a `FnOnce<(&bool,)>` closure, found `bool` --> $DIR/block_instead_of_closure_in_arg.rs:3:23 | LL | Some(true).filter({ | _________________------_^ | | | | | required by a bound introduced by this call LL | |/ if number % 2 == 0 { LL | || number == 0 LL | || } else { LL | || number != 0 LL | || } | ||_________- this tail expression is of type `bool` LL | | }); | |______^ expected an `FnOnce<(&bool,)>` closure, found `bool` | = help: the trait `for<'a> FnOnce<(&'a bool,)>` is not implemented for `bool` note: required by a bound in `Option::<T>::filter` --> $SRC_DIR/core/src/option.rs:LL:COL help: you might have meant to create the closure instead of a block | LL | Some(true).filter(|_| { | +++ ``` Partially address #27300.
- Loading branch information
Showing
5 changed files
with
117 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
tests/ui/expr/malformed_closure/block_instead_of_closure_in_arg.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
fn main() { | ||
let number = 2; | ||
Some(true).filter({ //~ ERROR expected a `FnOnce<(&bool,)>` closure, found `bool` | ||
if number % 2 == 0 { | ||
number == 0 | ||
} else { | ||
number != 0 | ||
} | ||
}); | ||
} |
27 changes: 27 additions & 0 deletions
27
tests/ui/expr/malformed_closure/block_instead_of_closure_in_arg.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
error[E0277]: expected a `FnOnce<(&bool,)>` closure, found `bool` | ||
--> $DIR/block_instead_of_closure_in_arg.rs:3:23 | ||
| | ||
LL | Some(true).filter({ | ||
| _________________------_^ | ||
| | | | ||
| | required by a bound introduced by this call | ||
LL | |/ if number % 2 == 0 { | ||
LL | || number == 0 | ||
LL | || } else { | ||
LL | || number != 0 | ||
LL | || } | ||
| ||_________- this tail expression is of type `bool` | ||
LL | | }); | ||
| |______^ expected an `FnOnce<(&bool,)>` closure, found `bool` | ||
| | ||
= help: the trait `for<'a> FnOnce<(&'a bool,)>` is not implemented for `bool` | ||
note: required by a bound in `Option::<T>::filter` | ||
--> $SRC_DIR/core/src/option.rs:LL:COL | ||
help: you might have meant to create the closure instead of a block | ||
| | ||
LL | Some(true).filter(|_| { | ||
| +++ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0277`. |
7 changes: 7 additions & 0 deletions
7
tests/ui/expr/malformed_closure/ruby_style_closure_successful_parse.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const x: usize =42; | ||
fn main() { | ||
let p = Some(45).and_then({|x| //~ ERROR expected a `FnOnce<({integer},)>` closure, found `Option<usize>` | ||
1 + 1; | ||
Some(x * 2) | ||
}); | ||
} |
25 changes: 25 additions & 0 deletions
25
tests/ui/expr/malformed_closure/ruby_style_closure_successful_parse.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
error[E0277]: expected a `FnOnce<({integer},)>` closure, found `Option<usize>` | ||
--> $DIR/ruby_style_closure_successful_parse.rs:3:31 | ||
| | ||
LL | let p = Some(45).and_then({|x| | ||
| ______________________--------_^ | ||
| | | | ||
| | required by a bound introduced by this call | ||
LL | | 1 + 1; | ||
LL | | Some(x * 2) | ||
| | ----------- this tail expression is of type `Option<usize>` | ||
LL | | }); | ||
| |_____^ expected an `FnOnce<({integer},)>` closure, found `Option<usize>` | ||
| | ||
= help: the trait `FnOnce<({integer},)>` is not implemented for `Option<usize>` | ||
note: required by a bound in `Option::<T>::and_then` | ||
--> $SRC_DIR/core/src/option.rs:LL:COL | ||
help: you might have meant to open the closure body instead of placing a closure within a block | ||
| | ||
LL - let p = Some(45).and_then({|x| | ||
LL + let p = Some(45).and_then(|x| { | ||
| | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0277`. |