Skip to content

Commit 1da8d2c

Browse files
committed
Safer addition in computing length of bounds
If the `usize` is `128`, then this would have failed for (0..=u128::MAX). We now just ignore this case.
1 parent 0d4912b commit 1da8d2c

4 files changed

+6
-2
lines changed

clippy_lints/src/methods/map_with_unused_argument_over_ranges.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ fn extract_count_with_applicability(
3333
let count = if upper_bound >= lower_bound {
3434
match range.limits {
3535
RangeLimits::HalfOpen => upper_bound - lower_bound,
36-
RangeLimits::Closed => upper_bound - lower_bound + 1,
36+
RangeLimits::Closed => (upper_bound - lower_bound).checked_add(1)?,
3737
}
3838
} else {
3939
0

tests/ui/map_with_unused_argument_over_ranges.fixed

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ fn main() {
5353
(lower_fn()..=upper_fn()).map(|_| do_something()); // Ranges not starting at zero not yet handled
5454
(0..10).map(|x| do_something_interesting(x, 4)); // Actual map over range
5555
"Foobar".chars().map(|_| do_something()); // Not a map over range
56+
// i128::MAX == 340282366920938463463374607431768211455
57+
(0..=340282366920938463463374607431768211455).map(|_: u128| do_something()); // Can't be replaced due to overflow
5658
}
5759

5860
#[clippy::msrv = "1.27"]

tests/ui/map_with_unused_argument_over_ranges.rs

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ fn main() {
5353
(lower_fn()..=upper_fn()).map(|_| do_something()); // Ranges not starting at zero not yet handled
5454
(0..10).map(|x| do_something_interesting(x, 4)); // Actual map over range
5555
"Foobar".chars().map(|_| do_something()); // Not a map over range
56+
// i128::MAX == 340282366920938463463374607431768211455
57+
(0..=340282366920938463463374607431768211455).map(|_: u128| do_something()); // Can't be replaced due to overflow
5658
}
5759

5860
#[clippy::msrv = "1.27"]

tests/ui/map_with_unused_argument_over_ranges.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ LL + std::iter::repeat_with(|| do_something()).take((1 << 4) - 0);
197197
|
198198

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

0 commit comments

Comments
 (0)