Skip to content

Commit

Permalink
Safer addition in computing length of bounds
Browse files Browse the repository at this point in the history
If the `usize` is `128`, then this would have failed for (0..=u128::MAX).
We now just ignore this case.
  • Loading branch information
rspencer01 committed Aug 26, 2024
1 parent 0d4912b commit 1da8d2c
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn extract_count_with_applicability(
let count = if upper_bound >= lower_bound {
match range.limits {
RangeLimits::HalfOpen => upper_bound - lower_bound,
RangeLimits::Closed => upper_bound - lower_bound + 1,
RangeLimits::Closed => (upper_bound - lower_bound).checked_add(1)?,
}
} else {
0
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/map_with_unused_argument_over_ranges.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ fn main() {
(lower_fn()..=upper_fn()).map(|_| do_something()); // Ranges not starting at zero not yet handled
(0..10).map(|x| do_something_interesting(x, 4)); // Actual map over range
"Foobar".chars().map(|_| do_something()); // Not a map over range
// i128::MAX == 340282366920938463463374607431768211455
(0..=340282366920938463463374607431768211455).map(|_: u128| do_something()); // Can't be replaced due to overflow
}

#[clippy::msrv = "1.27"]
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/map_with_unused_argument_over_ranges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ fn main() {
(lower_fn()..=upper_fn()).map(|_| do_something()); // Ranges not starting at zero not yet handled
(0..10).map(|x| do_something_interesting(x, 4)); // Actual map over range
"Foobar".chars().map(|_| do_something()); // Not a map over range
// i128::MAX == 340282366920938463463374607431768211455
(0..=340282366920938463463374607431768211455).map(|_: u128| do_something()); // Can't be replaced due to overflow
}

#[clippy::msrv = "1.27"]
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/map_with_unused_argument_over_ranges.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ LL + std::iter::repeat_with(|| do_something()).take((1 << 4) - 0);
|

error: map of a closure that does not depend on its parameter over a range
--> tests/ui/map_with_unused_argument_over_ranges.rs:65:5
--> tests/ui/map_with_unused_argument_over_ranges.rs:67:5
|
LL | (0..10).map(|_| do_something());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down

0 comments on commit 1da8d2c

Please sign in to comment.