-
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
Warn on redundant / needless type annotations #9155
Comments
Related: #7081 |
hi folks, is this issue ready to be picked up? I was thinking of looking into this issue as my first opportunity to cross the clippy user/clippy contributor barrier but before I do I wanted to see (1) if I'd be stepping on anyone's toes, (2) if there were any outstanding questions about this issue, and (3) if you all had any tips for where to look to get started in addition to the adding lints docs page. thanks! |
Just comment If you are unsure how to proceed, feel free to comment your questions on the issue or in the clippy channel on the rust-lang zulip. |
As for open questions, I'd suggest picking the |
@rustbot claim |
@rustbot release-assignment |
Hi everyone! If I succeed, this will be my first contribution to any opensource software, so hopefully it goes well! |
@rustbot claim |
I am sorry for the amount of time this issue is taking me, but I think I'm on the right track, and I'm still working on it! |
It seems pretty logical to me that this lint should also flag the use of redundant type annotations in the case of a let a_struct: A = A;
let b_struct: B = B::new(); |
Actually, now that I think about it, when is it useful to actually have the type annotation? Because it feels like it's always redundant, except maybe for numbers? But even then it could be considered redundant. So is this what I should check for? Or am I missing something? |
Effectively, I'd expect that the lint would have the same behavior (but likely a vastly different implementation) as removing the type annotation and then seeing if the code still compiles. I think that use std::iter;
fn main() {
let type_required: Vec<()> = iter::once(()).collect();
dbg!(type_required);
let type_not_required: Vec<()> = iter::once(()).collect();
usage(type_not_required);
}
fn usage(v: Vec<()>) {
dbg!(v);
} |
I'm very sorry, but I think this is not for me. |
@rustbot release-assignment |
@rustbot claim |
Hello, I'm trying to work on this, it is my first lint for clippy :) I wanted to start by linting on function calls with non generic return types, for example fn f() -> String {
String::new()
}
...
let a: String = f(); When I dump the HIR, for the
How would you get the returned type from the function calls from here? Is there anything I can use in the Also, which Thanks! |
Ok I managed to get it to work somehow, for now it lints on function calls with non generic types. Do you think that I could add more test cases? (As it's my first lint I'm not 100% of the assumptions I'm doing when checking the types, e.g. it doesn't work with method calls and with primitive types) Thanks! |
What it does
Warns about needless / redundant type annotations.
Lint Name
redundant_type_annotation
Category
style, pedantic
Advantage
Code is more idiomatic, shorter, and easier to modify.
Drawbacks
Types may be added by some novice Rust developers as hints to help remember what type is present.
Example
Could be written as:
The text was updated successfully, but these errors were encountered: