Skip to content

Incorrect "closure bodies that contain statements must be surrounded by braces" error in unrelatedly invalid code #127223

Closed

Description

@izik1

Code

fn test() {
    let value = || {};

    (value;);
}

Current output

error: closure bodies that contain statements must be surrounded by braces
 --> src/lib.rs:2:17
  |
2 |     let value = || {};
  |                 ^^
3 |
4 |     (value;);
  |            ^
  |
note: statement found outside of a block
 --> src/lib.rs:4:11
  |
2 |     let value = || {};
  |                    -- this expression is a statement because of the trailing semicolon
3 |
4 |     (value;);
  |           ^ this `;` turns the preceding closure into a statement
note: the closure body may be incorrectly delimited
 --> src/lib.rs:2:17
  |
2 |     let value = || {};
  |                 ^^^^^ this is the parsed closure...
3 |
4 |     (value;);
  |            - ...but likely you meant the closure to end here
help: try adding braces
  |
2 ~     let value = || { {};
3 |
4 ~     (value;});
  |

Desired output

error: expected one of `!`, `)`, `,`, `.`, `::`, `?`, `{`, or an operator, found `;`
 --> src/lib.rs:4:11
  |
4 |     (value;);
  |           ^ expected one of 8 possible tokens

Rationale and extra context

This error is unrelated to the closure, and indeed, applying the suggestion leaves us with:

fn test() {
    let value = || { {};

    (value; });
}

Which now has an improperly closed brace. The correct code would (most likely) be (value)

The original code I had made the suggestion even weirder since the closure was an argument to a Higher Order Function (instead of bound to a variable) and the last line was a function call with a ; after its argument (instead of a paren expression), which happened due to a typo 😅. Just in case something following that structure would make for a useful test case as well:

fn test() {
    let value = std::iter::once_with(|| ());

    std::men::drop(value;);
}

Other cases

No response

Rust Version

❯ rustc +nightly --version --verbose
rustc 1.81.0-nightly (6868c831a 2024-06-30)
binary: rustc
commit-hash: 6868c831a1eb45c5150ff623cef5e42a8b8946d0
commit-date: 2024-06-30
host: aarch64-apple-darwin
release: 1.81.0-nightly
LLVM version: 18.1.7

Anything else?

No response

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Jul 1, 2024
  2. changed the title [-]Incorrect "closure bodies that contain statements must be surrounded by braces" error unrelatedly invalid code[/-] [+]Incorrect "closure bodies that contain statements must be surrounded by braces" error in unrelatedly invalid code[/+] on Jul 1, 2024
  3. eggyal commented on Jul 2, 2024

    @eggyal
    Contributor

    Some(closure_spans) if self.token.kind == TokenKind::Semi => {
    // Finding a semicolon instead of a comma
    // after a closure body indicates that the
    // closure body may be a block but the user
    // forgot to put braces around its
    // statements.
    self.recover_missing_braces_around_closure_body(
    closure_spans,
    expect_err,
    )?;
    continue;
    }

    I suppose this test should only be applied when at the same depth as the closure definition?

  4. added a commit that references this issue on Sep 15, 2026
    58d799d
  5. added a commit that references this issue on Sep 15, 2026
    4550238
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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