Skip to content

Commit 0ee7025

Browse files
committed
Auto merge of rust-lang#9303 - Jarcho:ice_9297, r=Alexendoo
Fix ICE when reading literals with weird proc-macro spans fixes rust-lang#9297 changelog: Fix ICE when reading literals with weird proc-macro spans
2 parents 10853f7 + 99abd4a commit 0ee7025

File tree

4 files changed

+34
-20
lines changed

4 files changed

+34
-20
lines changed

clippy_utils/src/numeric_literal.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,12 @@ impl<'a> NumericLiteral<'a> {
223223

224224
fn split_suffix<'a>(src: &'a str, lit_kind: &LitKind) -> (&'a str, Option<&'a str>) {
225225
debug_assert!(lit_kind.is_numeric());
226-
lit_suffix_length(lit_kind).map_or((src, None), |suffix_length| {
227-
let (unsuffixed, suffix) = src.split_at(src.len() - suffix_length);
228-
(unsuffixed, Some(suffix))
229-
})
226+
lit_suffix_length(lit_kind)
227+
.and_then(|suffix_length| src.len().checked_sub(suffix_length))
228+
.map_or((src, None), |split_pos| {
229+
let (unsuffixed, suffix) = src.split_at(split_pos);
230+
(unsuffixed, Some(suffix))
231+
})
230232
}
231233

232234
fn lit_suffix_length(lit_kind: &LitKind) -> Option<usize> {

tests/ui/mistyped_literal_suffix.fixed

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// run-rustfix
2+
// aux-build: proc_macro_with_span.rs
23

34
#![allow(
45
dead_code,
@@ -9,6 +10,9 @@
910
clippy::unusual_byte_groupings
1011
)]
1112

13+
extern crate proc_macro_with_span;
14+
use proc_macro_with_span::with_span;
15+
1216
fn main() {
1317
let fail14 = 2_i32;
1418
let fail15 = 4_i64;
@@ -40,4 +44,6 @@ fn main() {
4044
let ok38 = 124_64.0;
4145

4246
let _ = 1.123_45E1_f32;
47+
48+
let _ = with_span!(1 2_u32);
4349
}

tests/ui/mistyped_literal_suffix.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// run-rustfix
2+
// aux-build: proc_macro_with_span.rs
23

34
#![allow(
45
dead_code,
@@ -9,6 +10,9 @@
910
clippy::unusual_byte_groupings
1011
)]
1112

13+
extern crate proc_macro_with_span;
14+
use proc_macro_with_span::with_span;
15+
1216
fn main() {
1317
let fail14 = 2_32;
1418
let fail15 = 4_64;
@@ -40,4 +44,6 @@ fn main() {
4044
let ok38 = 124_64.0;
4145

4246
let _ = 1.12345E1_32;
47+
48+
let _ = with_span!(1 2_u32);
4349
}

tests/ui/mistyped_literal_suffix.stderr

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,97 @@
11
error: mistyped literal suffix
2-
--> $DIR/mistyped_literal_suffix.rs:13:18
2+
--> $DIR/mistyped_literal_suffix.rs:17:18
33
|
44
LL | let fail14 = 2_32;
55
| ^^^^ help: did you mean to write: `2_i32`
66
|
77
= note: `#[deny(clippy::mistyped_literal_suffixes)]` on by default
88

99
error: mistyped literal suffix
10-
--> $DIR/mistyped_literal_suffix.rs:14:18
10+
--> $DIR/mistyped_literal_suffix.rs:18:18
1111
|
1212
LL | let fail15 = 4_64;
1313
| ^^^^ help: did you mean to write: `4_i64`
1414

1515
error: mistyped literal suffix
16-
--> $DIR/mistyped_literal_suffix.rs:15:18
16+
--> $DIR/mistyped_literal_suffix.rs:19:18
1717
|
1818
LL | let fail16 = 7_8; //
1919
| ^^^ help: did you mean to write: `7_i8`
2020

2121
error: mistyped literal suffix
22-
--> $DIR/mistyped_literal_suffix.rs:16:18
22+
--> $DIR/mistyped_literal_suffix.rs:20:18
2323
|
2424
LL | let fail17 = 23_16; //
2525
| ^^^^^ help: did you mean to write: `23_i16`
2626

2727
error: mistyped literal suffix
28-
--> $DIR/mistyped_literal_suffix.rs:19:18
28+
--> $DIR/mistyped_literal_suffix.rs:23:18
2929
|
3030
LL | let fail20 = 2__8; //
3131
| ^^^^ help: did you mean to write: `2_i8`
3232

3333
error: mistyped literal suffix
34-
--> $DIR/mistyped_literal_suffix.rs:20:18
34+
--> $DIR/mistyped_literal_suffix.rs:24:18
3535
|
3636
LL | let fail21 = 4___16; //
3737
| ^^^^^^ help: did you mean to write: `4_i16`
3838

3939
error: mistyped literal suffix
40-
--> $DIR/mistyped_literal_suffix.rs:23:18
40+
--> $DIR/mistyped_literal_suffix.rs:27:18
4141
|
4242
LL | let fail25 = 1E2_32;
4343
| ^^^^^^ help: did you mean to write: `1E2_f32`
4444

4545
error: mistyped literal suffix
46-
--> $DIR/mistyped_literal_suffix.rs:24:18
46+
--> $DIR/mistyped_literal_suffix.rs:28:18
4747
|
4848
LL | let fail26 = 43E7_64;
4949
| ^^^^^^^ help: did you mean to write: `43E7_f64`
5050

5151
error: mistyped literal suffix
52-
--> $DIR/mistyped_literal_suffix.rs:25:18
52+
--> $DIR/mistyped_literal_suffix.rs:29:18
5353
|
5454
LL | let fail27 = 243E17_32;
5555
| ^^^^^^^^^ help: did you mean to write: `243E17_f32`
5656

5757
error: mistyped literal suffix
58-
--> $DIR/mistyped_literal_suffix.rs:26:18
58+
--> $DIR/mistyped_literal_suffix.rs:30:18
5959
|
6060
LL | let fail28 = 241251235E723_64;
6161
| ^^^^^^^^^^^^^^^^ help: did you mean to write: `241_251_235E723_f64`
6262

6363
error: mistyped literal suffix
64-
--> $DIR/mistyped_literal_suffix.rs:30:18
64+
--> $DIR/mistyped_literal_suffix.rs:34:18
6565
|
6666
LL | let fail30 = 127_8; // should be i8
6767
| ^^^^^ help: did you mean to write: `127_i8`
6868

6969
error: mistyped literal suffix
70-
--> $DIR/mistyped_literal_suffix.rs:31:18
70+
--> $DIR/mistyped_literal_suffix.rs:35:18
7171
|
7272
LL | let fail31 = 240_8; // should be u8
7373
| ^^^^^ help: did you mean to write: `240_u8`
7474

7575
error: mistyped literal suffix
76-
--> $DIR/mistyped_literal_suffix.rs:33:18
76+
--> $DIR/mistyped_literal_suffix.rs:37:18
7777
|
7878
LL | let fail33 = 0x1234_16;
7979
| ^^^^^^^^^ help: did you mean to write: `0x1234_i16`
8080

8181
error: mistyped literal suffix
82-
--> $DIR/mistyped_literal_suffix.rs:34:18
82+
--> $DIR/mistyped_literal_suffix.rs:38:18
8383
|
8484
LL | let fail34 = 0xABCD_16;
8585
| ^^^^^^^^^ help: did you mean to write: `0xABCD_u16`
8686

8787
error: mistyped literal suffix
88-
--> $DIR/mistyped_literal_suffix.rs:36:18
88+
--> $DIR/mistyped_literal_suffix.rs:40:18
8989
|
9090
LL | let fail36 = 0xFFFF_FFFF_FFFF_FFFF_64; // u64
9191
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: did you mean to write: `0xFFFF_FFFF_FFFF_FFFF_u64`
9292

9393
error: mistyped literal suffix
94-
--> $DIR/mistyped_literal_suffix.rs:42:13
94+
--> $DIR/mistyped_literal_suffix.rs:46:13
9595
|
9696
LL | let _ = 1.12345E1_32;
9797
| ^^^^^^^^^^^^ help: did you mean to write: `1.123_45E1_f32`

0 commit comments

Comments
 (0)