Skip to content

Commit 144d940

Browse files
committed
Auto merge of #4510 - lzutao:unsep-literals-regression-macro-attr, r=flip1995
Fix regression in case of proc-macro attribute expansion cc #4507 changelog: none r? @flip1995
2 parents 58e01ea + ca1c0aa commit 144d940

File tree

6 files changed

+33
-17
lines changed

6 files changed

+33
-17
lines changed

clippy_lints/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// error-pattern:cargo-clippy
22

3+
#![feature(bind_by_move_pattern_guards)] // proposed to be stabilized in Rust v1.39
34
#![feature(box_syntax)]
45
#![feature(box_patterns)]
56
#![feature(never_type)]

clippy_lints/src/misc_early.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -430,15 +430,13 @@ impl EarlyLintPass for MiscEarlyLints {
430430

431431
impl MiscEarlyLints {
432432
fn check_lit(self, cx: &EarlyContext<'_>, lit: &Lit) {
433-
// The `line!()` macro is compiler built-in and a special case for these lints.
433+
// We test if first character in snippet is a number, because the snippet could be an expansion
434+
// from a built-in macro like `line!()` or a proc-macro like `#[wasm_bindgen]`.
435+
// Note that this check also covers special case that `line!()` is eagerly expanded by compiler.
436+
// See <https://github.com/rust-lang/rust-clippy/issues/4507> for a regression.
437+
// FIXME: Find a better way to detect those cases.
434438
let lit_snip = match snippet_opt(cx, lit.span) {
435-
Some(snip) => {
436-
// The snip could be empty in case of expand from procedure macro
437-
if snip.is_empty() || snip.contains('!') {
438-
return;
439-
}
440-
snip
441-
},
439+
Some(snip) if snip.chars().next().map_or(false, |c| c.is_digit(10)) => snip,
442440
_ => return,
443441
};
444442

mini-macro/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,8 @@ pub fn mini_macro(_: TokenStream) -> TokenStream {
1717
println!("{}", items[i]);
1818
}
1919
}
20+
fn line_wrapper() {
21+
println!("{}", line!());
22+
}
2023
)
2124
}

tests/ui/unseparated_prefix_literals.fixed

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
#![warn(clippy::unseparated_literal_suffix)]
44
#![allow(dead_code)]
55

6+
#[macro_use]
7+
extern crate clippy_mini_macro_test;
8+
9+
// Test for proc-macro attribute
10+
#[derive(ClippyMiniMacroTest)]
11+
struct Foo;
12+
613
macro_rules! lit_from_macro {
714
() => {
815
42_usize

tests/ui/unseparated_prefix_literals.rs

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
#![warn(clippy::unseparated_literal_suffix)]
44
#![allow(dead_code)]
55

6+
#[macro_use]
7+
extern crate clippy_mini_macro_test;
8+
9+
// Test for proc-macro attribute
10+
#[derive(ClippyMiniMacroTest)]
11+
struct Foo;
12+
613
macro_rules! lit_from_macro {
714
() => {
815
42usize

tests/ui/unseparated_prefix_literals.stderr

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,49 @@
11
error: integer type suffix should be separated by an underscore
2-
--> $DIR/unseparated_prefix_literals.rs:16:18
2+
--> $DIR/unseparated_prefix_literals.rs:23:18
33
|
44
LL | let _fail1 = 1234i32;
55
| ^^^^^^^ help: add an underscore: `1234_i32`
66
|
77
= note: `-D clippy::unseparated-literal-suffix` implied by `-D warnings`
88

99
error: integer type suffix should be separated by an underscore
10-
--> $DIR/unseparated_prefix_literals.rs:17:18
10+
--> $DIR/unseparated_prefix_literals.rs:24:18
1111
|
1212
LL | let _fail2 = 1234u32;
1313
| ^^^^^^^ help: add an underscore: `1234_u32`
1414

1515
error: integer type suffix should be separated by an underscore
16-
--> $DIR/unseparated_prefix_literals.rs:18:18
16+
--> $DIR/unseparated_prefix_literals.rs:25:18
1717
|
1818
LL | let _fail3 = 1234isize;
1919
| ^^^^^^^^^ help: add an underscore: `1234_isize`
2020

2121
error: integer type suffix should be separated by an underscore
22-
--> $DIR/unseparated_prefix_literals.rs:19:18
22+
--> $DIR/unseparated_prefix_literals.rs:26:18
2323
|
2424
LL | let _fail4 = 1234usize;
2525
| ^^^^^^^^^ help: add an underscore: `1234_usize`
2626

2727
error: integer type suffix should be separated by an underscore
28-
--> $DIR/unseparated_prefix_literals.rs:20:18
28+
--> $DIR/unseparated_prefix_literals.rs:27:18
2929
|
3030
LL | let _fail5 = 0x123isize;
3131
| ^^^^^^^^^^ help: add an underscore: `0x123_isize`
3232

3333
error: float type suffix should be separated by an underscore
34-
--> $DIR/unseparated_prefix_literals.rs:24:19
34+
--> $DIR/unseparated_prefix_literals.rs:31:19
3535
|
3636
LL | let _failf1 = 1.5f32;
3737
| ^^^^^^ help: add an underscore: `1.5_f32`
3838

3939
error: float type suffix should be separated by an underscore
40-
--> $DIR/unseparated_prefix_literals.rs:25:19
40+
--> $DIR/unseparated_prefix_literals.rs:32:19
4141
|
4242
LL | let _failf2 = 1f32;
4343
| ^^^^ help: add an underscore: `1_f32`
4444

4545
error: integer type suffix should be separated by an underscore
46-
--> $DIR/unseparated_prefix_literals.rs:8:9
46+
--> $DIR/unseparated_prefix_literals.rs:15:9
4747
|
4848
LL | 42usize
4949
| ^^^^^^^ help: add an underscore: `42_usize`
@@ -52,7 +52,7 @@ LL | let _ = lit_from_macro!();
5252
| ----------------- in this macro invocation
5353

5454
error: integer type suffix should be separated by an underscore
55-
--> $DIR/unseparated_prefix_literals.rs:33:16
55+
--> $DIR/unseparated_prefix_literals.rs:40:16
5656
|
5757
LL | assert_eq!(4897u32, 32223);
5858
| ^^^^^^^ help: add an underscore: `4897_u32`

0 commit comments

Comments
 (0)