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

false positives and crazy suggestsiong for unit_arg lint #6015

Closed
pm100 opened this issue Sep 7, 2020 · 4 comments · Fixed by #5931
Closed

false positives and crazy suggestsiong for unit_arg lint #6015

pm100 opened this issue Sep 7, 2020 · 4 comments · Fixed by #5931
Labels
C-bug Category: Clippy is not doing the correct thing S-needs-discussion Status: Needs further discussion before merging or work can be started

Comments

@pm100
Copy link

pm100 commented Sep 7, 2020

I have this code:

    pub(crate) fn set_word(&mut self, addr: Word, value: Word) -> ExcResult<()> {
        Ok(self.ram.set_word(addr, value))
    }

ExcResult is Result<(), Exception>

clippy says

475 |         Ok(self.ram.set_word(addr, value))
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: `#[warn(clippy::unit_arg)]` on by default
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unit_arg
help: move the expression in front of the call...
    |
475 |         self.ram.set_word(addr, value);
    |
help: ...and use a unit literal instead
    |
475 |         Ok(())
    |            ^^

which is clearly not right.

I have half a dozen similar errors when calling functions that return ()

Meta

clippy 0.0.212 (04488afe3 2020-08-24)
cargo 1.46.0 (149022b1d 2020-07-17)
rustc 1.46.0 (04488afe3 2020-08-24)

@pm100 pm100 added the C-bug Category: Clippy is not doing the correct thing label Sep 7, 2020
@ghost
Copy link

ghost commented Sep 7, 2020

It looks like this lint is working as intended to me.

Are you saying that Ok(self.ram.set_word(addr, value)) isn't equivalent to self.ram.set_word(addr, value); Ok(()) ? Why not?

@pm100
Copy link
Author

pm100 commented Sep 7, 2020

ok i misundertood what it says, I read it as saying it could be reduced to Ok(()). THe text is not clear.

Having said that, I disagree with the lint. As the code stands the intent is very clear -> 'Okize' the result of calling this function. You dont have to look at set_word to see if it returns something that I am choosing to ignore, also the intent is 100% clear. And its very concise.

In clippy's approved version one is left wondering about that function call.

I have a long series of small functions that are proxying non Result returning functions and upscaling them to be Result returners. The fact that some of them return () as opposed to i32 or string is not relevant.

Having said that I must say that clippy is awesome. It found some real bugs. I ported c++ code that uses octal literals a lot (a PDP 11 emulator) and clippy found some literals I had not added 'o' to. Ty v much.

@ebroto
Copy link
Member

ebroto commented Sep 7, 2020

FYI there's a PR that will be merged soon that improves the suggestion (#5931).

About your use case, to be fair the lint help mentions functions, so the fact that enum constructors are included may be an oversight in the implementation and could be special cased.

I'm adding the "needs-discussion" tag to hopefully have more eyes on this, as I think it's debatable.

@ebroto ebroto added the S-needs-discussion Status: Needs further discussion before merging or work can be started label Sep 7, 2020
@pm100
Copy link
Author

pm100 commented Sep 7, 2020

also note that the message says line 475 should say Ok(()) thats why its confusing

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 S-needs-discussion Status: Needs further discussion before merging or work can be started
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants