Skip to content

unused_braces false positive with assignment to wildcard #116536

@kadiwa4

Description

@kadiwa4

This code:

fn main() {
    let e = std::iter::empty::<()>();
    _ = { e };
    // for _ in e {} // uncomment to verify that e was dropped
}

Gives this warning:

warning: unnecessary braces around assigned value
 --> src/main.rs:3:9
  |
3 |     _ = { e };
  |         ^^ ^^
  |
  = note: `#[warn(unused_braces)]` on by default
help: remove these braces
  |
3 -     _ = { e };
3 +     _ = e;
  |

However, the braces are not unnecessary. _ = { e }; does the same thing as drop(e);, but _ = e; doesn't do anything (not even move e).
You also can't just write { e };, that would trigger a unused_must_use warning.
I think the lint shouldn't trigger here.

There are also more complicated examples like this:

use std::fmt::Error;

fn main() {
    let e = (std::iter::empty::<()>(), Error);
    (_, Error) = { e };
    // for _ in e.0 {} // uncomment to verify that e was dropped
}

Where it isn't even possible to tell if the braces are unnecessary just based on the AST. But surely no-one writes code like that :)

Version

rustc 1.75.0-nightly (2023-10-07)

@rustbot label: T-compiler A-lint

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.L-unused_bracesLint: unused_bracesT-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