Skip to content

False-positive unused_parens missing macro cases #138234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
MultisampledNight opened this issue Mar 8, 2025 · 0 comments · May be fixed by #138271
Open

False-positive unused_parens missing macro cases #138234

MultisampledNight opened this issue Mar 8, 2025 · 0 comments · May be fixed by #138271
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) L-false-positive Lint: False positive (should not have fired). L-unused_parens Lint: unused_parens T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@MultisampledNight
Copy link
Contributor

MultisampledNight commented Mar 8, 2025

Code

macro_rules! wrap {
    ($name:ident $arg:expr) => {
        $name($arg);
    };
}

fn main() {
    wrap!(unary(routine()));
}

fn unary(_: ()) {}
fn routine() {}

Current output

warning: unnecessary parentheses around function argument
 --> src/main.rs:8:16
  |
8 |     wrap!(unary(routine()));
  |                ^         ^
  |
  = note: `#[warn(unused_parens)]` on by default
help: remove these parentheses
  |
8 -     wrap!(unary(routine()));
8 +     wrap!(unaryroutine());
  |

Desired output

(this section left empty, lint should not trigger)

Rationale and extra context

This:

wrap!(unary(routine()))

expands to:

unary((routine()))

Where the parentheses are indeed overkill.
Hence the lint would somehow need to look at the original span to see if it's necessary.

I see 3 paths forward from this:

  1. Do not trigger the lint.
  2. Introduce a new lint that suggests inserting a space before the parentheses for clarity.
  3. Introduce a new lint that does 2. but also suggests removing the parens in one go.

Point 1. is probably the easiest forward, the other ones probably require the information necessary for 1. anyway.

Other cases

// The argument can be actually anything, it is not necessary for it to be a function call
// It'll just be appended to the ident

macro_rules! wrap {
    ($name:ident $arg:expr) => {
        $name($arg);
    };
}

fn main() {
    wrap!(unary(0));
}

fn unary<T>(_: T) {}

Rust Version

$ rustc --version --verbose
rustc 1.87.0-nightly (f5a1ef712 2025-03-07)
binary: rustc
commit-hash: f5a1ef7121ad661b5a21a1d02941c8064d54ee0b
commit-date: 2025-03-07
host: x86_64-unknown-linux-gnu
release: 1.87.0-nightly
LLVM version: 20.1.0

Anything else?

Thank you all for your work. <3

@MultisampledNight MultisampledNight added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 8, 2025
@MultisampledNight MultisampledNight changed the title unused_parens lint misses macro cases False-positive unused_parens missing macro cases Mar 8, 2025
@MultisampledNight MultisampledNight changed the title False-positive unused_parens missing macro cases False-negative unused_parens missing macro cases Mar 8, 2025
@MultisampledNight MultisampledNight changed the title False-negative unused_parens missing macro cases False-positive unused_parens missing macro cases Mar 8, 2025
@lukas-code lukas-code added A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) L-unused_parens Lint: unused_parens L-false-positive Lint: False positive (should not have fired). labels Mar 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) L-false-positive Lint: False positive (should not have fired). L-unused_parens Lint: unused_parens T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants