-
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
range_plus_one lint wrongly suggests using RangeInclusive when Range is required and the other way around #3307
Comments
Another example using rayon: The following program compiles, but clippy produces a warning on it: extern crate rayon;
use rayon::prelude::*;
fn main() {
(0..1 + 1).into_par_iter();
} The warning is:
However, applying the suggestion results in the following error:
See also the rayon documentation for ranges. |
+1 for this issue. I also met it when calling a function requiring fn foo(_: std::ops::Range<i32>) {}
fn main() {
let x = 1;
foo(x..(x + 1));
} I don't think implementing a generic function for all ranges would be always better. |
Citing #4898 to keep traack of this in a single issue:
So the same problem exists the other way around. |
We hit this in gfx-rs since the code expects |
This also happens when returning a value from a function. pub struct Span {
offset: usize,
length: usize,
}
impl From<Span> for RangeInclusive<usize> {
fn from(span: Span) -> Self {
// clippy wants me to put span.offset..(span.offset + span.length) here
span.offset..=(span.offset + span.length - 1)
}
} |
@flip1995 Hey I would like to tackle this one but I am not sure how we would know if the type of an expression can be changed or not. Any suggestions? |
This moves the range_minus_one lint to the pedantic category, so there will not be any warnings emitted by default. This should work around problems where the suggestion is impossible to resolve due to the range consumer only accepting a specific range implementation, rather than the `RangeBounds` trait (see rust-lang#3307). While it is possible to work around this by extracting the boundary into a variable, I don't think clippy should encourage people to disable or work around lints, but instead the lints should be fixable. So hopefully this will help until a proper implementation checks what the range is used for.
This moves the range_minus_one lint to the pedantic category, so there will not be any warnings emitted by default. This should work around problems where the suggestion is impossible to resolve due to the range consumer only accepting a specific range implementation, rather than the `RangeBounds` trait (see rust-lang#3307). While it is possible to work around this by extracting the boundary into a variable, I don't think clippy should encourage people to disable or work around lints, but instead the lints should be fixable. So hopefully this will help until a proper implementation checks what the range is used for.
Move range_minus_one to pedantic This moves the range_minus_one lint to the pedantic category, so there will not be any warnings emitted by default. This should work around problems where the suggestion is impossible to resolve due to the range consumer only accepting a specific range implementation, rather than the `RangeBounds` trait (see #3307). While it is possible to work around this by extracting the boundary into a variable, I don't think clippy should encourage people to disable or work around lints, but instead the lints should be fixable. So hopefully this will help until a proper implementation checks what the range is used for. *Please keep the line below* changelog: move [`range_minus_one`] to pedantic
Move range_minus_one to pedantic This moves the range_minus_one lint to the pedantic category, so there will not be any warnings emitted by default. This should work around problems where the suggestion is impossible to resolve due to the range consumer only accepting a specific range implementation, rather than the `RangeBounds` trait (see rust-lang#3307). While it is possible to work around this by extracting the boundary into a variable, I don't think clippy should encourage people to disable or work around lints, but instead the lints should be fixable. So hopefully this will help until a proper implementation checks what the range is used for. *Please keep the line below* changelog: move [`range_minus_one`] to pedantic
Move range_minus_one to pedantic This moves the range_minus_one lint to the pedantic category, so there will not be any warnings emitted by default. This should work around problems where the suggestion is impossible to resolve due to the range consumer only accepting a specific range implementation, rather than the `RangeBounds` trait (see rust-lang#3307). While it is possible to work around this by extracting the boundary into a variable, I don't think clippy should encourage people to disable or work around lints, but instead the lints should be fixable. So hopefully this will help until a proper implementation checks what the range is used for. *Please keep the line below* changelog: move [`range_minus_one`] to pedantic
@rustbot label +L-suggestion-causes-error |
@rustbot claim |
I've just tripped this lint in some code that uses the Of course, some of the blame for that could probably be directed at |
@sendittothenewts Can you post a small code snippet where you ran into this issue? Doesn't have to be the original code, just a snippet that illustrates the problem. |
Ah yes, since However, the following slight variation does exhibit changed run-time behaviour without any more warnings: fn main() {
let names = ["fred", "barney", "wilma"];
let bound = ..=names.len() - 1; // clippy suggests `..names.len()` instead
println!("{}", bound.end); // Would print "3" instead of "2"
println!("{}", names[bound.end - 1]); // Would print "wilma" instead of "barney"
println!("{}", names[bound.end]); // Would panic instead of printing "wilma"
} |
I see, so the main problem is with |
Yes, that's right. Without the |
The
range_plus_one
lint suggests replacingRange
syntax withRangeInclusive
syntax. That's fine if you're immediately going to loop. But it's invalid if theRange
in question is going to be assigned to a variable of typeRange
. For example, clippy will suggest replacing this code:with this:
But that fails to compile
The text was updated successfully, but these errors were encountered: