-
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 false positive when return type is type parameter #1502
Comments
we need to check whether the rhs contains any type inference. |
I'm not sure how to solve this. This will need some forensics first to figure out whether rustc can tell us if it used inference. |
I think it would work to recognize just this specific pattern: return type consists entirely of a type parameter that is not used by any of the argument types. In that case permit let_unit_value. |
Downgrade let_unit_value to pedantic Given that the false positive in rust-lang#1502 is marked E-hard and I don't have much hope of it getting fixed, I think it would be wise to disable this lint by default. I have had to suppress this lint in every substantial codebase (\>100k line) I have worked in. Any time this lint is being triggered, it's always the false positive case. The motivation for this lint is documented as: > A unit value cannot usefully be used anywhere. So binding one is kind of pointless. with this example: > ```rust > let x = { > 1; > }; > ``` Sure, but the author would find this out via an unused_variable warning or from `x` not being the type that they need further down. If there ends up being a type error on `x`, clippy's advice isn't going to help get the code compiling because it can only run if the code already compiles. changelog: Remove let_unit_value from default set of enabled lints
Downgrade let_unit_value to pedantic Given that the false positive in rust-lang#1502 is marked E-hard and I don't have much hope of it getting fixed, I think it would be wise to disable this lint by default. I have had to suppress this lint in every substantial codebase (\>100k line) I have worked in. Any time this lint is being triggered, it's always the false positive case. The motivation for this lint is documented as: > A unit value cannot usefully be used anywhere. So binding one is kind of pointless. with this example: > ```rust > let x = { > 1; > }; > ``` Sure, but the author would find this out via an unused_variable warning or from `x` not being the type that they need further down. If there ends up being a type error on `x`, clippy's advice isn't going to help get the code compiling because it can only run if the code already compiles. changelog: Remove let_unit_value from default set of enabled lints
This is an interesting thing I stumpled upon today. I would say that your code @dtolnay should still be linted, because you assign the unit value to a named variable (although it has an underscore, but that doesn't matter here ;) ) I would let clippy suggest doing this: let () = f(); This would still let the compiler infer the type but telling the user, that he can't do anything with the unit value. |
|
I somehow saw |
This fix for this doesn't work for me. Approximate code: let (_tx, rx) = oneshot::channel();
let handle = spawn(async {
let () = rx.await.unwrap();
}); This triggers the lint in 1.62.0 (now that it defaults to warn). Using |
It sounds like #8998 might cover my case, though I don't know how that is different from the issue described here. |
Maybe this specific case would be clearer with
f::<()>()
but I consider this a false positive. Came up in serde_json unit tests where we test that serde_json::from_str can parse various representations of ().The text was updated successfully, but these errors were encountered: