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 must not expand macro in suggestion #6850

Closed
matthiaskrgr opened this issue Mar 5, 2021 · 2 comments · Fixed by #6871
Closed

redundant_closure must not expand macro in suggestion #6850

matthiaskrgr opened this issue Mar 5, 2021 · 2 comments · Fixed by #6871
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 I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@matthiaskrgr
Copy link
Member

Lint name: redundant_closure

warning: redundant closure found
   --> src/iter/test.rs:121:13
    |
121 |             || vec![],
    |             ^^^^^^^^^ help: remove closure as shown: `$crate::vec::Vec::new`
    |
    = note: `#[warn(clippy::redundant_closure)]` on by default
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure

This is obviously not going to work with $crate::vec...

The suggestion should just say Vec::new()

How to fix: find out where the lint generates the snippet and use snippet_with_macro_callsite instead of just snippet.

( this type of bug popped up a couple of times already #6801 (comment)
maybe we should lint for that?)

@matthiaskrgr matthiaskrgr added 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 I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied labels Mar 5, 2021
@mgacek8
Copy link
Contributor

mgacek8 commented Mar 5, 2021

If we go here: https://github.com/rust-lang/rust-clippy/blob/master/clippy_lints/src/eta_reduction.rs#L111 and change that snipped_opt to snippet_with_macro_callsite, then clippy would suggest the following:

+error: redundant closure found
+  --> $DIR/eta.rs:207:42
+   |
+LL |     let _ : Option<Vec<i32>> = true.then(||vec![]);
+   |                                          ^^^^^^^^ help: remove closure as shown: `vec![]`

which causes the compiler error.

We can strip the $crate::vec prefix, but it will work only for the vec![] macro.
This issue is similar: #6732 but with the quote!() macro
Maybe there is no sense to trigger redundant closure lint for macros ?

@camsteffen
Copy link
Contributor

In the general case, || macro!() should not fire redundant_closure since the suggestion will force the user to inline the macro and expose its implementation. But vec![] -> Vec::new is a special case. So that special case can use a hard-coded suggestion or (better) obtain the minimal path to Vec depending on whether it is imported.

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 I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants