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

unit_arg suggestions don't make sense when run against Option::map_or(unit_expr, ...) #5823

Closed
Arnavion opened this issue Jul 19, 2020 · 2 comments · Fixed by #5931
Closed
Labels
C-bug Category: Clippy is not doing the correct thing E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-suggestion Lint: Improving, adding or fixing lint suggestions

Comments

@Arnavion
Copy link
Contributor

#[deny(clippy::unit_arg)]
pub fn foo(o: Option<i32>, f: impl FnOnce(i32), g: impl FnOnce()) {
    o.map_or(g(), |i| f(i))
}

(For context, this is the code after taking the code in #5821 and applying clippy::option_if_let_else's suggestion.)

The lint's suggestion is:

help: move the expression in front of the call...
  |
3 |     g();
  |
help: ...and use a unit literal instead
  |
3 |     o.map_or((), |i| f(i))
  |              ^^

I'm not sure what the first suggestion is trying to say.

The second suggestion is removing the call to g entirely, which is definitely wrong.

Note that the expectation of this code was that it would actually trigger clippy::or_fun_call to suggest o.map_or_else(|| g(), |i| f(i)) (as mentioned in #5821), so the problem is not only that it ended up triggering clippy::unit_arg instead but also that the suggestions don't make sense.

Meta

  • cargo clippy -V: clippy 0.0.212 (39d5a61 2020-07-17)
  • rustc -Vv:
    rustc 1.47.0-nightly (39d5a61f2 2020-07-17)
    binary: rustc
    commit-hash: 39d5a61f2e4e237123837f5162cc275c2fd7e625
    commit-date: 2020-07-17
    host: x86_64-unknown-linux-gnu
    release: 1.47.0-nightly
    LLVM version: 10.0
    
@flip1995
Copy link
Member

The suggestion suggests to move the g() call in front of the map_or call and replacing it with (). This suggestion could still be improved, since this doesn't seem to be clear.

Anyway, the or_fun_call lint should definitely trigger here.

@flip1995 flip1995 added L-suggestion Lint: Improving, adding or fixing lint suggestions E-medium Call for participation: Medium difficulty level problem and requires some initial experience. labels Jul 28, 2020
@Arnavion
Copy link
Contributor Author

Arnavion commented Jul 28, 2020

Ah, I get it now. Perhaps it could emit the whole change as a single line so that the context is there, like:

help: move the expression in front of the call and replace the original value with a unit literal
  |
3 |     g(); o.map_or((), |i| f(i))
  |

bors added a commit that referenced this issue Sep 10, 2020
improve the suggestion of the lint `unit-arg`

Fixes #5823
Fixes #6015

Changes
```
help: move the expression in front of the call...
  |
3 |     g();
  |
help: ...and use a unit literal instead
  |
3 |     o.map_or((), |i| f(i))
  |
```
into
```
help: move the expression in front of the call and replace it with the unit literal `()`
  |
3 |     g();
  |     o.map_or((), |i| f(i))
  |
```
changelog: improve the suggestion of the lint `unit-arg`
@bors bors closed this as completed in 55efa96 Sep 10, 2020
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 E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-suggestion Lint: Improving, adding or fixing lint suggestions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants