Skip to content

redundant_closure_call triggered when closure is a macro parameter #11274

Closed
@tspiteri

Description

@tspiteri

Summary

Though after macro expansion the closure seems redundant, the closure is required for the macro invocation.

Lint Name

redundant_closure_call

Reproducer

I tried this code:

macro_rules! m {
    ($closure:expr) => {
        $closure(1)
    };
}

fn main() {
    m!(|x| println!("{x}"));
}

I saw this happen:

warning: try not to call a closure in the expression where it is declared
 --> src/main.rs:3:9
  |
3 |         $closure(1)
  |         ^^^^^^^^^^^
...
8 |     m!(|x| println!("{x}"));
  |     ----------------------- in this macro invocation
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure_call
  = note: `#[warn(clippy::redundant_closure_call)]` on by default
  = note: this warning originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)

I expected to see this happen:

No warning.

Version

rustc 1.73.0-nightly (32303b219 2023-07-29)
binary: rustc
commit-hash: 32303b219d4dffa447aa606bc11c7a648f44a862
commit-date: 2023-07-29
host: x86_64-unknown-linux-gnu
release: 1.73.0-nightly
LLVM version: 16.0.5

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveT-macrosType: Issues with macros and macro expansiongood first issueThese issues are a good way to get started with Clippy

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions