Skip to content

Bad suggestion on missing block inside of a closure #107959

Closed
@GoldsteinE

Description

@GoldsteinE

Code

fn main() {
    let _x = std::boxed::Box::new(|x|x+1;);
}

Current output

error: closure bodies that contain statements must be surrounded by braces
 --> src/main.rs:2:37
  |
2 |     let _x = std::boxed::Box::new(|x|x+1;);
  |                                     ^
3 | }
  | ^
  |
note: statement found outside of a block
 --> src/main.rs:2:41
  |
2 |     let _x = std::boxed::Box::new(|x|x+1;);
  |                                      ---^ this `;` turns the preceding closure into a statement
  |                                      |
  |                                      this expression is a statement because of the trailing semicolon
note: the closure body may be incorrectly delimited
 --> src/main.rs:2:35
  |
2 |     let _x = std::boxed::Box::new(|x|x+1;);
  |                                   ^^^^^^ this is the parsed closure...
3 | }
  | - ...but likely you meant the closure to end here
help: try adding braces
  |
2 ~     let _x = std::boxed::Box::new(|x| {x+1;);
3 ~ }}
  |

error: expected `;`, found `}`
 --> src/main.rs:2:44
  |
2 |     let _x = std::boxed::Box::new(|x|x+1;);
  |                                            ^ help: add `;` here
3 | }
  | - unexpected token

error: could not compile `playground` due to 3 previous errors

Desired output

error: closure bodies that contain statements must be surrounded by braces
 --> src/main.rs:2:37
  |
2 |     let _x = std::boxed::Box::new(|x|x+1;);
  |                                     ^
3 | }
  | ^
  |
note: statement found outside of a block
 --> src/main.rs:2:41
  |
2 |     let _x = std::boxed::Box::new(|x|x+1;);
  |                                      ---^ this `;` turns the preceding closure into a statement
  |                                      |
  |                                      this expression is a statement because of the trailing semicolon
help: try adding braces
  |
2 ~     let _x = std::boxed::Box::new(|x| {x+1;});
  |

error: could not compile `playground` due to 3 previous errors

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions