Skip to content

fix unused_braces suggestion for wildcard assignment - #158011

Open
pbkx wants to merge 1 commit into
rust-lang:mainfrom
pbkx:issue-116536-unused-braces-wildcard-assignment
Open

pbkx wants to merge 1 commit into
rust-lang:mainfrom
pbkx:issue-116536-unused-braces-wildcard-assignment

Conversation

@pbkx

@pbkx pbkx commented Jun 17, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #116536.

The unused_braces lint could suggest removing braces from wildcard assignments like:

_ = { e };

producing:

_ = e;

These are not equivalent: _ = { e }; moves/drops e, while _ = e; does not. This PR suppresses unused_braces for assignment RHS blocks when the assignment LHS contains _.

ui regression test covers both the simple wildcard assignment case and a tuple destructuring assignment case

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 17, 2026
@pbkx
pbkx marked this pull request as ready for review June 17, 2026 10:09
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 17, 2026
@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jun 17, 2026
@rustbot

rustbot commented Jun 17, 2026

Copy link
Copy Markdown
Collaborator

r? @TaKO8Ki

rustbot has assigned @TaKO8Ki.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 73 candidates
  • Random selection from 21 candidates

@TaKO8Ki TaKO8Ki left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. I left one comment.

View changes since this review

Comment thread compiler/rustc_lint/src/unused.rs Outdated

Assign(_, ref value, _) | AssignOp(.., ref value) => {
Assign(ref lhs, ref value, _) => {
let ctx = if expr_contains_underscore(lhs) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this lint should not be triggered in some other cases like (..) = { e } [..] = { e }. Can we address them here as well?

#![deny(unused_braces)]

struct D(&'static str);

impl Drop for D {
    fn drop(&mut self) {
        println!("drop {}", self.0);
    }
}

fn main() {
    let e = (D("a"), D("b"));
    (..) = { e };
}

@rust-bors

This comment has been minimized.

@pbkx
pbkx force-pushed the issue-116536-unused-braces-wildcard-assignment branch from a8f5835 to b14a543 Compare September 7, 2026 04:22
@rustbot

rustbot commented Sep 7, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@pbkx

pbkx commented Sep 7, 2026

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@pbkx

pbkx commented Oct 11, 2026

Copy link
Copy Markdown
Contributor Author

@TaKO8Ki

&& (ctx != UnusedDelimsCtx::AnonConst
|| (matches!(expr.kind, ast::ExprKind::Lit(_))
&& !expr.span.from_expansion()))
&& ctx != UnusedDelimsCtx::AssignedValueWithWildcard

@fmease fmease Nov 2, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's too coarse. Consider:

fn main() {
    let e = String::new();
    _ = { (e,) };
}

The braces are indeed unnecessary because the tuple construction (_,) already moves/drops e regardless of whether there are any { }. But now under your PR we no longer lint here.

The tuple construction is just an example.

Should at least be a FIXME. Not sure if it's worth blocking this PR on fixing this new class of false negatives...

View changes since the review

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

unused_braces false positive with assignment to wildcard

4 participants