Skip to content
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

redundant_closure_call triggered when closure is a macro parameter #11274

Closed
tspiteri opened this issue Aug 1, 2023 · 2 comments · Fixed by #12082
Closed

redundant_closure_call triggered when closure is a macro parameter #11274

tspiteri opened this issue Aug 1, 2023 · 2 comments · Fixed by #12082
Labels
C-bug Category: Clippy is not doing the correct thing good-first-issue These issues are a good way to get started with Clippy I-false-positive Issue: The lint was triggered on code it shouldn't have T-macros Type: Issues with macros and macro expansion

Comments

@tspiteri
Copy link
Contributor

tspiteri commented Aug 1, 2023

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

@tspiteri tspiteri added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Aug 1, 2023
@gthb
Copy link

gthb commented Aug 29, 2023

Duplicate of #1553?

@m-rph
Copy link
Contributor

m-rph commented Dec 29, 2023

@rustbot label +T-Macros +good-first-issue

Looks like a duplicate.

@rustbot rustbot added T-macros Type: Issues with macros and macro expansion good-first-issue These issues are a good way to get started with Clippy labels Dec 29, 2023
bors added a commit that referenced this issue Jan 27, 2024
Fixed FP in `redundant_closure_call` when closures are passed to macros

There are cases where the closure call is needed in some macros, this in particular occurs when the closure has parameters. To handle this case, we allow the lint when there are no parameters in the closure, or the closure is outside a macro invocation.

fixes: #11274 #1553
changelog: FP: [`redundant_closure_call`] when closures with parameters are passed in macros.
@bors bors closed this as completed in e0228ee Jan 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing good-first-issue These issues are a good way to get started with Clippy I-false-positive Issue: The lint was triggered on code it shouldn't have T-macros Type: Issues with macros and macro expansion
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants