-
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
Suggest to use turbofish syntax instead of type hints #7081
Comments
I am one of those people. I like to be able to glance at But, my opinion aside, this could possibly be done as a restriction lint. But it would be very hard (impossible?) to implement since we would have to reverse engineer the rustc type checking process. |
At least in IntelliJ Rust and rust-analyzer, inlay type hints solve this problem.
I do see this argument although it is not my workflow at all. I almost never type From that perspective, I find it an annoyance if I have to modify the code that was created because it interrupts my flow, esp. if I am modifying the code and I end up with non-compiling code due to now stale
I am not too familiar with writing clippy lints. Could we start with a hard-coded list of types? Maybe only supporting simple expressions like |
It should be possible for simple cases at least where the generic function output is assigned directly, or maybe with one added method like |
Okay, thanks for the confirmation. I'll leave this open and see if there is further interest in it. I may also attempt to write it myself now that I've discovered https://github.com/trailofbits/dylint. |
What it does
Suggest to use turbofish syntax instead of type hints.
Categories (optional)
clippy::style
What is the advantage of the recommended code over the original code
Type hints can get stale if the expression is modified / refactored and ends up returning a different type than what was originally returned.
Using turbofish syntax instead allows the compiler to continue with its type-inference and hence not require a type hint. Contrary to the type hint on the variable, the turbofish syntax is applied "locally" to where it is needed and therefore is more likely to stay relevant as the code around it changes.
Overall, I think this makes it easier to maintain Rust code.
Drawbacks
Some people may prefer to write type hints instead of turbo-fish syntax.
Examples
Could be written as:
The text was updated successfully, but these errors were encountered: