Skip to content

Commit f3dc7f1

Browse files
committed
Fix println_empty_string suggestion caused error when there is a , after arg
1 parent 6110c80 commit f3dc7f1

File tree

5 files changed

+183
-3
lines changed

5 files changed

+183
-3
lines changed

clippy_lints/src/write/empty_string.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use clippy_utils::source::expand_past_previous_comma;
44
use clippy_utils::sym;
55
use rustc_ast::{FormatArgs, FormatArgsPiece};
66
use rustc_errors::Applicability;
7-
use rustc_lint::LateContext;
7+
use rustc_lint::{LateContext, LintContext};
88

99
use super::{PRINTLN_EMPTY_STRING, WRITELN_EMPTY_STRING};
1010

@@ -17,6 +17,12 @@ pub(super) fn check(cx: &LateContext<'_>, format_args: &FormatArgs, macro_call:
1717

1818
WRITELN_EMPTY_STRING
1919
} else {
20+
let closing_paren =
21+
cx.sess()
22+
.source_map()
23+
.span_extend_to_prev_char_before(macro_call.span.shrink_to_hi(), ')', false);
24+
span = format_args.span.with_hi(closing_paren.lo());
25+
2026
PRINTLN_EMPTY_STRING
2127
};
2228

tests/ui/crashes/ice-10148.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: empty string literal in `println!`
22
--> tests/ui/crashes/ice-10148.rs:8:5
33
|
44
LL | println!(with_span!(""something ""));
5-
| ^^^^^^^^^^^^^^^^^^^^-----------^^^^^
5+
| ^^^^^^^^^^^^^^^^^^^^---------------^
66
| |
77
| help: remove the empty string
88
|

tests/ui/println_empty_string.fixed

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,38 @@ fn main() {
1919
//~^ println_empty_string
2020
}
2121
}
22+
23+
#[rustfmt::skip]
24+
fn issue_16167() {
25+
println!();
26+
//~^ println_empty_string
27+
match "a" {
28+
_ => println!(),
29+
//~^ println_empty_string
30+
}
31+
32+
eprintln!();
33+
//~^^ println_empty_string
34+
match "a" {
35+
_ => eprintln!(), // tab
36+
//~^ println_empty_string
37+
}
38+
39+
//~v println_empty_string
40+
println!(
41+
);
42+
match "a" {
43+
//~v println_empty_string
44+
_ => println!(
45+
),
46+
}
47+
48+
//~v println_empty_string
49+
eprintln!(
50+
);
51+
match "a" {
52+
//~v println_empty_string
53+
_ => eprintln!(
54+
),
55+
}
56+
}

tests/ui/println_empty_string.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,54 @@ fn main() {
1919
//~^ println_empty_string
2020
}
2121
}
22+
23+
#[rustfmt::skip]
24+
fn issue_16167() {
25+
println!(""/* ,,, */);
26+
//~^ println_empty_string
27+
match "a" {
28+
_ => println!("" ,),
29+
//~^ println_empty_string
30+
}
31+
32+
eprintln!(""
33+
,);
34+
//~^^ println_empty_string
35+
match "a" {
36+
_ => eprintln!("" ,), // tab
37+
//~^ println_empty_string
38+
}
39+
40+
//~v println_empty_string
41+
println!(
42+
"\
43+
\
44+
" /* comment ,,,
45+
, */ ,
46+
);
47+
match "a" {
48+
//~v println_empty_string
49+
_ => println!(
50+
"\
51+
\
52+
"
53+
//, other comment
54+
,
55+
),
56+
}
57+
58+
//~v println_empty_string
59+
eprintln!(
60+
"\
61+
\
62+
",
63+
);
64+
match "a" {
65+
//~v println_empty_string
66+
_ => eprintln!(
67+
"\
68+
\
69+
", // , comment
70+
),
71+
}
72+
}

tests/ui/println_empty_string.stderr

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,93 @@ LL | _ => eprintln!(""),
3333
| |
3434
| help: remove the empty string
3535

36-
error: aborting due to 4 previous errors
36+
error: empty string literal in `println!`
37+
--> tests/ui/println_empty_string.rs:25:5
38+
|
39+
LL | println!(""/* ,,, */);
40+
| ^^^^^^^^^-----------^
41+
| |
42+
| help: remove the empty string
43+
44+
error: empty string literal in `println!`
45+
--> tests/ui/println_empty_string.rs:28:14
46+
|
47+
LL | _ => println!("" ,),
48+
| ^^^^^^^^^--------^
49+
| |
50+
| help: remove the empty string
51+
52+
error: empty string literal in `eprintln!`
53+
--> tests/ui/println_empty_string.rs:32:5
54+
|
55+
LL | eprintln!(""
56+
| _____^ -
57+
| |_______________|
58+
LL | || ,);
59+
| ||_________-^
60+
| |__________|
61+
| help: remove the empty string
62+
63+
error: empty string literal in `eprintln!`
64+
--> tests/ui/println_empty_string.rs:36:14
65+
|
66+
LL | _ => eprintln!("" ,), // tab
67+
| ^^^^^^^^^^-------^
68+
| |
69+
| help: remove the empty string
70+
71+
error: empty string literal in `println!`
72+
--> tests/ui/println_empty_string.rs:41:5
73+
|
74+
LL | / println!(
75+
LL | |/ "\
76+
LL | || \
77+
LL | || " /* comment ,,,
78+
LL | || , */ ,
79+
LL | || );
80+
| ||____-^
81+
| |____|
82+
| help: remove the empty string
83+
84+
error: empty string literal in `println!`
85+
--> tests/ui/println_empty_string.rs:49:14
86+
|
87+
LL | _ => println!(
88+
| _______________^
89+
LL | |/ "\
90+
LL | || \
91+
LL | || "
92+
LL | || //, other comment
93+
LL | || ,
94+
LL | || ),
95+
| ||________-^
96+
| |________|
97+
| help: remove the empty string
98+
99+
error: empty string literal in `eprintln!`
100+
--> tests/ui/println_empty_string.rs:59:5
101+
|
102+
LL | / eprintln!(
103+
LL | |/ "\
104+
LL | || \
105+
LL | || ",
106+
LL | || );
107+
| ||____-^
108+
| |____|
109+
| help: remove the empty string
110+
111+
error: empty string literal in `eprintln!`
112+
--> tests/ui/println_empty_string.rs:66:14
113+
|
114+
LL | _ => eprintln!(
115+
| _______________^
116+
LL | |/ "\
117+
LL | || \
118+
LL | || ", // , comment
119+
LL | || ),
120+
| ||________-^
121+
| |________|
122+
| help: remove the empty string
123+
124+
error: aborting due to 12 previous errors
37125

0 commit comments

Comments
 (0)