Skip to content

Commit 75e2dcf

Browse files
author
Michael Wright
committed
literal representation: simplification
Simplify calculation in grouping. Add test case to ensure `count()` can't be zero in that branch.
1 parent 2e9d173 commit 75e2dcf

File tree

4 files changed

+14
-2
lines changed

4 files changed

+14
-2
lines changed

clippy_lints/src/literal_representation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ impl<'a> NumericLiteral<'a> {
268268
let first_group_size;
269269

270270
if partial_group_first {
271-
first_group_size = (digits.clone().count() + group_size - 1) % group_size + 1;
271+
first_group_size = (digits.clone().count() - 1) % group_size + 1;
272272
if pad {
273273
for _ in 0..group_size - first_group_size {
274274
output.push('0');

tests/ui/inconsistent_digit_grouping.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@ fn main() {
1818
let _ = 0x0100_0000;
1919
let _ = 0x1000_0000;
2020
let _ = 0x0001_0000_0000_u64;
21+
22+
// Test suggestion when fraction has no digits
23+
let _: f32 = 123_456.;
2124
}

tests/ui/inconsistent_digit_grouping.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@ fn main() {
1818
let _ = 0x1000000;
1919
let _ = 0x10000000;
2020
let _ = 0x100000000_u64;
21+
22+
// Test suggestion when fraction has no digits
23+
let _: f32 = 1_23_456.;
2124
}

tests/ui/inconsistent_digit_grouping.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,11 @@ error: long literal lacking separators
5656
LL | let _ = 0x100000000_u64;
5757
| ^^^^^^^^^^^^^^^ help: consider: `0x0001_0000_0000_u64`
5858

59-
error: aborting due to 9 previous errors
59+
error: digits grouped inconsistently by underscores
60+
--> $DIR/inconsistent_digit_grouping.rs:23:18
61+
|
62+
LL | let _: f32 = 1_23_456.;
63+
| ^^^^^^^^^ help: consider: `123_456.`
64+
65+
error: aborting due to 10 previous errors
6066

0 commit comments

Comments
 (0)