-
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
let_unit_value should be allowed when I explicitly write "let () = ..." #9048
Comments
Related to #8998 Here are my two cents:
If you can miss a return value, e.g. it's crucial that you use that return value, than that function should be attributed with On the other hand, I like your style more than The only place, where I think a assignment to the unit value is good, is when the return value is a type parameter and must be specified. Is this understandable? |
It looks like #9056 will fix this. |
#9056 will allow it when used for inference, not in general. |
Oh. Then I think it ought to be changed to also allow // Generic things
pub struct ThingError;
pub trait DoThing {
type Output;
fn do_thing(&self) -> Result<Self::Output, ThingError>;
}
// Specific concrete type
struct NoOutput;
impl DoThing for NoOutput {
type Output = ();
fn do_thing(&self) -> Result<Self::Output, ThingError> {
Ok(())
}
}
pub fn example() -> Result<&'static str, ThingError> {
// This should stop compiling if the output type changes to something nontrivial
let () = NoOutput.do_thing()?;
Ok("some other return value")
} Note that In the particular case this came up, replacing (A better overall solution would be some way to express nested |
The new lint `clippy::let_unit_value` doesn't like us using `let () =` to demonstrate that we're not ignoring a more interesting value. So, introduce a type that isn't `()` to use instead. My comment at the Clippy issue: rust-lang/rust-clippy#9048 (comment)
I agree that |
Just ran into this today. Agreed that
because there are no actual variable bindings in the form |
I have another use case were fn do_something(pool: SqlPool) {
let mut transaction = pool.start_transaction();
#[allow(unused_variables)]
let pool = (); // NOTE: shadowing the name to ensure we use `transaction`.
} |
@Thomasdezeeuw , I would recommend |
@Thomasdezeeuw , oops, this would have different semantics |
And it's also not possible if the transaction depends on the pool and thus is connection via e.g. a lifetime. |
+1 on |
At the very least this should be downgraded from |
Workaround! Use this: let mut v = vec![2, 1];
[()] = [v.sort()]; |
rust-lang/rust#112380 is about to merge. It will add similar lint to rustc. Unfortunately, as well as I understand, the new lint will be allow-by-default, as opposed to clippy's (buggy) lint, which is warning-by-default, and which will be kept for now. So, clippy in its default configuration will still be buggy :( |
Summary
Sometimes I write code like this:
let () = some_function_call()
. I do this to make sure that this function returns unit value, i. e. to make sure that I didn't miss any return value. But let_unit_value warns on this code. So, please, make it not to warn on this codeLint Name
let_unit_value
Reproducer
I tried this code:
I saw this happen:
I expected to see this happen:
(no warn)
Version
Additional Labels
No response
The text was updated successfully, but these errors were encountered: