Skip to content

Commit f2820df

Browse files
committed
Fix println_empty_string suggestion caused error when there is a , after arg
1 parent 40ad1a6 commit f2820df

File tree

5 files changed

+188
-2
lines changed

5 files changed

+188
-2
lines changed

clippy_lints/src/write/empty_string.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::macros::MacroCall;
3-
use clippy_utils::source::expand_past_previous_comma;
3+
use clippy_utils::source::{expand_past_next_comma, expand_past_previous_comma};
44
use clippy_utils::sym;
55
use rustc_ast::{FormatArgs, FormatArgsPiece};
66
use rustc_errors::Applicability;
@@ -17,6 +17,10 @@ pub(super) fn check(cx: &LateContext<'_>, format_args: &FormatArgs, macro_call:
1717

1818
WRITELN_EMPTY_STRING
1919
} else {
20+
span = Some(expand_past_next_comma(cx, span))
21+
.filter(|s| macro_call.span.contains(*s))
22+
.unwrap_or(span);
23+
2024
PRINTLN_EMPTY_STRING
2125
};
2226

clippy_utils/src/source.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,16 @@ pub fn expand_past_previous_comma(sess: &impl HasSession, span: Span) -> Span {
759759
extended.with_lo(extended.lo() - BytePos(1))
760760
}
761761

762+
/// Expand a span to include a following comma
763+
/// ```rust,ignore
764+
/// println!("" ,) -> println!("" ,)
765+
/// ^^ ^^^^
766+
/// ```
767+
pub fn expand_past_next_comma(sess: &impl HasSession, span: Span) -> Span {
768+
let extended = sess.sess().source_map().span_extend_to_next_char(span, ',', true);
769+
extended.with_hi(extended.hi() + BytePos(1))
770+
}
771+
762772
/// Converts `expr` to a `char` literal if it's a `str` literal containing a single
763773
/// character (or a single byte with `ascii_only`)
764774
pub fn str_literal_to_char_literal(

tests/ui/println_empty_string.fixed

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,42 @@ 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+
);
43+
match "a" {
44+
//~v println_empty_string
45+
_ => println!(
46+
47+
),
48+
}
49+
50+
//~v println_empty_string
51+
eprintln!(
52+
53+
);
54+
match "a" {
55+
//~v println_empty_string
56+
_ => eprintln!(
57+
58+
),
59+
}
60+
}

tests/ui/println_empty_string.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,53 @@ 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+
match "a" {
47+
//~v println_empty_string
48+
_ => println!(
49+
"\
50+
\
51+
"
52+
// comment
53+
,
54+
),
55+
}
56+
57+
//~v println_empty_string
58+
eprintln!(
59+
"\
60+
\
61+
",
62+
);
63+
match "a" {
64+
//~v println_empty_string
65+
_ => eprintln!(
66+
"\
67+
\
68+
",
69+
),
70+
}
71+
}

tests/ui/println_empty_string.stderr

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,88 @@ 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+
| ||_________________________________- help: remove the empty string
79+
LL | | );
80+
| |______^
81+
82+
error: empty string literal in `println!`
83+
--> tests/ui/println_empty_string.rs:48:14
84+
|
85+
LL | _ => println!(
86+
| _______________^
87+
LL | |/ "\
88+
LL | || \
89+
LL | || "
90+
LL | || // comment
91+
LL | || ,
92+
| ||_____________________- help: remove the empty string
93+
LL | | ),
94+
| |__________^
95+
96+
error: empty string literal in `eprintln!`
97+
--> tests/ui/println_empty_string.rs:58:5
98+
|
99+
LL | / eprintln!(
100+
LL | |/ "\
101+
LL | || \
102+
LL | || ",
103+
| ||__________________- help: remove the empty string
104+
LL | | );
105+
| |______^
106+
107+
error: empty string literal in `eprintln!`
108+
--> tests/ui/println_empty_string.rs:65:14
109+
|
110+
LL | _ => eprintln!(
111+
| _______________^
112+
LL | |/ "\
113+
LL | || \
114+
LL | || ",
115+
| ||______________________- help: remove the empty string
116+
LL | | ),
117+
| |__________^
118+
119+
error: aborting due to 12 previous errors
37120

0 commit comments

Comments
 (0)