-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 positive with unit_arg when returning Ok(function) #6521
Comments
If
|
If it's any consolation, this triggers the needless question mark lint, #6507 |
@bengsparks To be fair, that lint is valid in this case. I was struggling come up with simple examples, rather than pasting blocks of my real code here! |
I'm not so sure this is a false positive. |
Maybe a more compelling example: thing.with_foo(|foo| Ok(foo.find(key)?.set_value(11))) This pattern also triggers the lint. If we were to follow the suggestion, it would have to be: thing.with_foo(|foo| {
foo.find(key)?.set_value(11);
Ok(())
}) Not the end of the world, but I would argue that the first fits just fine in idiomatic Rust. |
I do fundamentally disagree with this lint, considering it negatively impacts the ability to write concise code in iterators and match arms. But I'd also like to point out that the description itself is somewhat confusing for this scenario. You have an enum variant here and a function and the clippy warning clearly states that it has an issue with a function. But I'm not sure most people would immediately connect the "function" reference to the |
This is also problematic in match branches: fn return_result(input: Stuff) -> Result<(), Error> {
match input {
Stuff::Weird => Err(build_error()),
Stuff::Good { cake } => Ok(eat(cake)),
}
}
fn eat(cake: Cake) { ... } Having to write the following feels wrong (4 lines instead of 1, hindering readability by scattering code): Stuff::Good { cake } => {
eat(cake);
Ok(())
} |
I've just ran into it again, with this code: match orientation {
Orientation::NoTransforms => Ok(()),
Orientation::Rotate90 => Ok(*image = image.rotate90()),
Orientation::Rotate180 => Ok(image.rotate180_in_place()),
Orientation::Rotate270 => Ok(*image = image.rotate270()),
Orientation::FlipHorizontal => Ok(image.fliph_in_place()),
Orientation::FlipVertical => Ok(image.flipv_in_place()),
} which Clippy wants me to turn into match orientation {
Orientation::NoTransforms => Ok(()),
Orientation::Rotate90 => {
*image = image.rotate90();
Ok(())
},
Orientation::Rotate180 => {
image.rotate180_in_place();
Ok(())
},
Orientation::Rotate270 => {
*image = image.rotate270();
Ok(())
},
Orientation::FlipHorizontal => {
image.fliph_in_place();
Ok(())
},
Orientation::FlipVertical => {
image.flipv_in_place();
Ok(())
},
} Which is quite obviously unhelpful. The function signature already specifies it returns a unit type; all this accomplishes is create a lot of visual noise. |
I don't think the match orientation {
Orientation::NoTransforms => (),
Orientation::Rotate90 => *image = image.rotate90(),
Orientation::Rotate180 => image.rotate180_in_place(),
Orientation::Rotate270 => *image = image.rotate270(),
Orientation::FlipHorizontal => image.fliph_in_place(),
Orientation::FlipVertical => image.flipv_in_place(),
}
Ok(()) |
I personally allow this lint at crate level in all my crates. I didn't see yet a single case where it's useful. Ok(match expr {
Pat => good()?,
Pat => return Err(bad()),
}) So assuming the match value to be the ok side and using control flow for the err side. |
I looked up the original motivation of the lint. The documentation on the lint states:
I am not passing a unit value as an argument to a function. I am returning it from a function.
The function is explicitly annotated as returning a unit type. There is no way it can be a result of an accidental semicolon. |
You're passing it to
I do agree, an accidental missed semicolon would normally result in a compilation error. I don't see how Rust would let you accidentally pass a unit value to a function that should have gotten something else. I guess maybe there could be some generic code where you might end up doing the wrong thing by accident. But I can't really think of a convincing example myself. |
Lint name: unit_arg
I tried this code:
I expected to see this happen: No error
Instead, this happened: It triggered the lint
In this case, I think the lint is overly strict and the code above is arguably better than the accepted fix:
Meta
cargo clippy -V
: clippy 0.0.212 (04488af 2020-08-24)rustc -Vv
:The text was updated successfully, but these errors were encountered: