Skip to content

"Unnecessary" braces have an influence #72783

Open
@vandenheuvel

Description

@vandenheuvel

On 1.45.0-nightly (2020-05-29 4bd32c98047a809ba5fd)

fn f() -> usize {
    {1usize} - 1usize
}

Intuitively, I would expect these braces to be redundant and also have no influence, but they do. The compiler expects a () type and then considers the minus sign a unary operator.

warning: unnecessary braces around block return value
 --> src/lib.rs:6:5
  |
6 |     {1usize} - 1usize
  |     ^^^^^^^^ help: remove these braces
  |
  = note: `#[warn(unused_braces)]` on by default

error[E0308]: mismatched types
 --> src/lib.rs:6:6
  |
6 |     {1usize} - 1usize
  |      ^^^^^^ expected `()`, found `usize`

error[E0600]: cannot apply unary operator `-` to type `usize`
 --> src/lib.rs:6:14
  |
6 |     {1usize} - 1usize
  |              ^^^^^^^^ cannot apply unary operator `-`
  |
  = note: unsigned values cannot be negated

It seems to me that either the error message about the braces being redundant is incorrect or misleading, or something more nefarious seems to be going on with scopes, expressions, etc.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-confusingDiagnostics: Confusing error or lint that should be reworked.T-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