Skip to content

Avoid unused braces lint for macro generated arguments#158791

Open
chenyukang wants to merge 1 commit into
rust-lang:mainfrom
chenyukang:yukang-fix-158747-unused-braces-macro
Open

Avoid unused braces lint for macro generated arguments#158791
chenyukang wants to merge 1 commit into
rust-lang:mainfrom
chenyukang:yukang-fix-158747-unused-braces-macro

Conversation

@chenyukang

@chenyukang chenyukang commented Jul 5, 2026

Copy link
Copy Markdown
Member

Fixes #158747

This changes the unused_braces lint to avoid suggesting brace removal for macro arguments when those braces may affect macro_rules!.

But the fragment kind matters:

  • $e:expr matches an expression. Both 1 and { 1 } are expressions, so removing braces can still leave the macro matching the same kind of input.
  • $b:block only matches a block expression written with braces. { 1 } matches, but 1 does not.

So call_expr!({ 1 }) can usually be simplified to call_expr!(1), while call_block!({ 1 }) cannot.

It seems a more concrect report need a full macro_rules! matcher analysis, that seems too heavy for unused lint.

@rustbot rustbot added 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. labels Jul 5, 2026
@rustbot

rustbot commented Jul 5, 2026

Copy link
Copy Markdown
Collaborator

r? @folkertdev

rustbot has assigned @folkertdev.
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 75 candidates
  • Random selection from 21 candidates

for arg in args_to_check {
if callee_from_expansion && Self::block_wraps_expanded_expr(arg) {
continue;
}

@folkertdev folkertdev Jul 5, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this deserves a comment

View changes since the review

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe

Whether an expression is wrapped in a block can change what arm of a macro_rules! is taken. Don't report the braces as unused that might be the case.

);
}

fn block_wraps_expanded_expr(value: &ast::Expr) -> bool {

@folkertdev folkertdev Jul 5, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

probably could use a comment too

View changes since the review

@@ -0,0 +1,38 @@
//@ check-pass
//@ run-rustfix

@folkertdev folkertdev Jul 5, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

these days we often like to put a one-sentence description of what this is testing and why at the top of these files

View changes since the review

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 5, 2026
@chenyukang chenyukang force-pushed the yukang-fix-158747-unused-braces-macro branch from 27f37a2 to b50b5b2 Compare July 5, 2026 23:51
@folkertdev

Copy link
Copy Markdown
Contributor

Lovely, thanks!

@bors r+ rollup

@rust-bors

rust-bors Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

📌 Commit b50b5b2 has been approved by folkertdev

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 7, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jul 7, 2026
…d-braces-macro, r=folkertdev

Avoid unused braces lint for macro generated arguments

Fixes rust-lang#158747

This changes the `unused_braces` lint to avoid suggesting brace removal for macro arguments when those braces may affect `macro_rules!`.

But the fragment kind matters:

- `$e:expr` matches an expression. Both `1` and `{ 1 }` are expressions, so removing braces can still leave the macro matching the same kind of input.
- `$b:block` only matches a block expression written with braces. `{ 1 }` matches, but `1` does not.

So `call_expr!({ 1 })` can usually be simplified to `call_expr!(1)`, while `call_block!({ 1 })` cannot.

It seems a more concrect report need a full `macro_rules!` matcher analysis, that seems too heavy for `unused` lint.
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jul 7, 2026
…d-braces-macro, r=folkertdev

Avoid unused braces lint for macro generated arguments

Fixes rust-lang#158747

This changes the `unused_braces` lint to avoid suggesting brace removal for macro arguments when those braces may affect `macro_rules!`.

But the fragment kind matters:

- `$e:expr` matches an expression. Both `1` and `{ 1 }` are expressions, so removing braces can still leave the macro matching the same kind of input.
- `$b:block` only matches a block expression written with braces. `{ 1 }` matches, but `1` does not.

So `call_expr!({ 1 })` can usually be simplified to `call_expr!(1)`, while `call_block!({ 1 })` cannot.

It seems a more concrect report need a full `macro_rules!` matcher analysis, that seems too heavy for `unused` lint.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

#[warn(unused_braces)] false positive for macro that accepts types and expressions and forwards them to a function

3 participants