-
Notifications
You must be signed in to change notification settings - Fork 1.7k
single_range_in_vec_init
stronger than expected, when type is known
#11086
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
Comments
Ignoring it for arguments when it's a local would likely be pretty tricky, though type annotations should definitely be checked |
Something not currently covered by the PR: use std::ops::Range;
struct A {
ranges: Vec<Range<usize>>,
}
struct B(Vec<Range<usize>>);
fn main() {
A { ranges: vec![0..5] };
B(vec![0..5]);
} The correct type of a struct's field is also known "from the outside". |
Should be simple enough to fix |
It ignores |
I'm also running into this in Quinn, in the tests for the |
This is coming up with a lot of false positives for our project as well. I'm concerned that it is valid to have/want a Vec with a single Range, and that the suggestions to correct it would alter functionality. If anyone goes and applies them blindly, they might cause subtle bugs. |
The help message should probably be altered to make sure this doesn't happen, since right now it acts as though this is almost certainly what they want |
It also causes quite annoying issues in my project. I completely don't understand the intention behind this lint and why is it enabled by default. |
This lint doesn't tell me what to do if I actually WANT an array/vec of ranges, which makes its strength even more bothersome. |
Agreed with everyone here, in our case it's only triggering on locals that are immediately passed to function calls that wouldn't typecheck otherwise. Should this lint be downgraded to a category that isn't warn-by-default? |
Summary
The
single_range_in_vec_init
warning seems a bit overly cautious, if the type resulting from an invocation of avec!
macro is already known "from the outside", e.g. via a type annotation or when it is used as an argument / return value.For example
In this case clippy might infer, that the user indeed wanted a
Vec<Range<...>>
, because otherwise the program could not even have type checked. The same is true for this example:One explicit way to remedy this is using explicit lengths:
although I am slightly worried that tools like cargo-fmt might at some point opt to remove that.
Lint Name
single_range_in_vec_init
Reproducer
I tried this code:
I saw this happen:
I expected to see this happen:
No warnings produced.
Version
No response
Additional Labels
No response
The text was updated successfully, but these errors were encountered: