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

Suggest to use turbofish syntax instead of type hints #7081

Open
thomaseizinger opened this issue Apr 15, 2021 · 4 comments
Open

Suggest to use turbofish syntax instead of type hints #7081

thomaseizinger opened this issue Apr 15, 2021 · 4 comments
Labels
A-lint Area: New lints E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-restriction Lint: Belongs in the restriction lint group

Comments

@thomaseizinger
Copy link

What it does

Suggest to use turbofish syntax instead of type hints.

Categories (optional)

  • Kind: 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

let numbers: Vec<_> = (0..10).collect();
let lucky_number: u64 = "42".parse().unwrap();

Could be written as:

let numbers = (0..10).collect::<Vec<_>>();
let lucky_number = "42".parse::<u64>().unwrap();
@thomaseizinger thomaseizinger added the A-lint Area: New lints label Apr 15, 2021
@camsteffen
Copy link
Contributor

Some people

I am one of those people. I like to be able to glance at let lucky_number and see the type of the variable without compiling the code after it in my head. I also like to write code saying "This is the type I ultimately want, Rust will figure out the details for me." I also like how writing an expression that is returned by a function works in a consistent way - the type is just annotated by the function signature rather than the let.

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.

@camsteffen camsteffen added E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-restriction Lint: Belongs in the restriction lint group labels May 3, 2021
@thomaseizinger
Copy link
Author

I like to be able to glance at let lucky_number and see the type of the variable without compiling the code after it in my head.

At least in IntelliJ Rust and rust-analyzer, inlay type hints solve this problem.

I also like to write code saying "This is the type I ultimately want, Rust will figure out the details for me." I also like how writing an expression that is returned by a function works in a consistent way - the type is just annotated by the function signature rather than the let.

I do see this argument although it is not my workflow at all. I almost never type let bindings myself but always start with the expression and use IDE assists like "Extract variable" or postfix completions like .let to end with Rust code that compiles.

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 let bindings.

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.

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 .parse() could be a feature because they are easy enough to understand quickly.

@camsteffen
Copy link
Contributor

It should be possible for simple cases at least where the generic function output is assigned directly, or maybe with one added method like unwrap. It would just be hard to implement in a way that covers all cases.

@thomaseizinger
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-restriction Lint: Belongs in the restriction lint group
Projects
None yet
Development

No branches or pull requests

2 participants