Skip to content

Commit 18fa70d

Browse files
committed
Handle empty ranges more gracefully
1 parent f33ec49 commit 18fa70d

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

clippy_lints/src/methods/map_with_unused_argument_over_ranges.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,16 @@ fn extract_count_with_applicability(
2929
&& let LitKind::Int(Pu128(upper_bound), _) = lit.node
3030
{
3131
// Here we can explicitly calculate the number of iterations
32-
let mut count = upper_bound - lower_bound;
33-
if range.limits == RangeLimits::Closed {
34-
count += 1;
35-
}
32+
let count = if upper_bound > lower_bound {
33+
match range.limits {
34+
RangeLimits::HalfOpen => upper_bound - lower_bound,
35+
RangeLimits::Closed => upper_bound - lower_bound + 1,
36+
}
37+
} else if upper_bound == lower_bound && range.limits == RangeLimits::Closed {
38+
1
39+
} else {
40+
0
41+
};
3642
return Some(format!("{count}"));
3743
}
3844
let end_snippet = snippet_with_applicability(cx, end.span, "...", applicability).into_owned();

0 commit comments

Comments
 (0)