Skip to content

Commit 7bef38b

Browse files
committed
Fix #2494 add suggestion for unreadable_literal
Cover large digit group case in tests Cover inconsistent group case in tests
1 parent adb8f7e commit 7bef38b

File tree

3 files changed

+36
-20
lines changed

3 files changed

+36
-20
lines changed

clippy_lints/src/literal_representation.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use rustc::lint::*;
55
use syntax::ast::*;
66
use syntax_pos;
7-
use utils::{in_external_macro, snippet_opt, span_help_and_lint};
7+
use utils::{in_external_macro, snippet_opt, span_lint_and_sugg};
88

99
/// **What it does:** Warns if a long integral or floating-point constant does
1010
/// not contain underscores.
@@ -222,33 +222,37 @@ enum WarningType {
222222
impl WarningType {
223223
pub fn display(&self, grouping_hint: &str, cx: &EarlyContext, span: &syntax_pos::Span) {
224224
match *self {
225-
WarningType::UnreadableLiteral => span_help_and_lint(
225+
WarningType::UnreadableLiteral => span_lint_and_sugg(
226226
cx,
227227
UNREADABLE_LITERAL,
228228
*span,
229229
"long literal lacking separators",
230-
&format!("consider: {}", grouping_hint),
230+
"consider",
231+
grouping_hint.to_owned(),
231232
),
232-
WarningType::LargeDigitGroups => span_help_and_lint(
233+
WarningType::LargeDigitGroups => span_lint_and_sugg(
233234
cx,
234235
LARGE_DIGIT_GROUPS,
235236
*span,
236237
"digit groups should be smaller",
237-
&format!("consider: {}", grouping_hint),
238+
"consider",
239+
grouping_hint.to_owned(),
238240
),
239-
WarningType::InconsistentDigitGrouping => span_help_and_lint(
241+
WarningType::InconsistentDigitGrouping => span_lint_and_sugg(
240242
cx,
241243
INCONSISTENT_DIGIT_GROUPING,
242244
*span,
243245
"digits grouped inconsistently by underscores",
244-
&format!("consider: {}", grouping_hint),
246+
"consider",
247+
grouping_hint.to_owned(),
245248
),
246-
WarningType::DecimalRepresentation => span_help_and_lint(
249+
WarningType::DecimalRepresentation => span_lint_and_sugg(
247250
cx,
248251
DECIMAL_LITERAL_REPRESENTATION,
249252
*span,
250253
"integer literal has a better hexadecimal representation",
251-
&format!("consider: {}", grouping_hint),
254+
"consider",
255+
grouping_hint.to_owned(),
252256
),
253257
};
254258
}

tests/ui/unreadable_literal.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@
55
fn main() {
66
let good = (0b1011_i64, 0o1_234_u32, 0x1_234_567, 1_2345_6789, 1234_f32, 1_234.12_f32, 1_234.123_f32, 1.123_4_f32);
77
let bad = (0b10110_i64, 0x12345678901_usize, 12345_f32, 1.23456_f32);
8+
9+
let large_digit_groups: u64 = 6186491_8973511;
10+
let inconsistent_groups: u64 = 618_64_9189_73_511;
811
}

tests/ui/unreadable_literal.stderr

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,43 @@ error: long literal lacking separators
22
--> $DIR/unreadable_literal.rs:7:16
33
|
44
7 | let bad = (0b10110_i64, 0x12345678901_usize, 12345_f32, 1.23456_f32);
5-
| ^^^^^^^^^^^
5+
| ^^^^^^^^^^^ help: consider: `0b1_0110_i64`
66
|
77
= note: `-D unreadable-literal` implied by `-D warnings`
8-
= help: consider: 0b1_0110_i64
98

109
error: long literal lacking separators
1110
--> $DIR/unreadable_literal.rs:7:29
1211
|
1312
7 | let bad = (0b10110_i64, 0x12345678901_usize, 12345_f32, 1.23456_f32);
14-
| ^^^^^^^^^^^^^^^^^^^
15-
|
16-
= help: consider: 0x123_4567_8901_usize
13+
| ^^^^^^^^^^^^^^^^^^^ help: consider: `0x123_4567_8901_usize`
1714

1815
error: long literal lacking separators
1916
--> $DIR/unreadable_literal.rs:7:50
2017
|
2118
7 | let bad = (0b10110_i64, 0x12345678901_usize, 12345_f32, 1.23456_f32);
22-
| ^^^^^^^^^
23-
|
24-
= help: consider: 12_345_f32
19+
| ^^^^^^^^^ help: consider: `12_345_f32`
2520

2621
error: long literal lacking separators
2722
--> $DIR/unreadable_literal.rs:7:61
2823
|
2924
7 | let bad = (0b10110_i64, 0x12345678901_usize, 12345_f32, 1.23456_f32);
30-
| ^^^^^^^^^^^
25+
| ^^^^^^^^^^^ help: consider: `1.234_56_f32`
26+
27+
error: digit groups should be smaller
28+
--> $DIR/unreadable_literal.rs:9:35
3129
|
32-
= help: consider: 1.234_56_f32
30+
9 | let large_digit_groups: u64 = 6186491_8973511;
31+
| ^^^^^^^^^^^^^^^ help: consider: `61_864_918_973_511`
32+
|
33+
= note: `-D large-digit-groups` implied by `-D warnings`
34+
35+
error: digits grouped inconsistently by underscores
36+
--> $DIR/unreadable_literal.rs:10:36
37+
|
38+
10 | let inconsistent_groups: u64 = 618_64_9189_73_511;
39+
| ^^^^^^^^^^^^^^^^^^ help: consider: `61_864_918_973_511`
40+
|
41+
= note: `-D inconsistent-digit-grouping` implied by `-D warnings`
3342

34-
error: aborting due to 4 previous errors
43+
error: aborting due to 6 previous errors
3544

0 commit comments

Comments
 (0)