Skip to content

Commit 486dc5c

Browse files
authored
Merge pull request #2650 from thekidxp/fixEmptyPrintln
fixEmptyPrintln
2 parents b2e4b88 + d175c79 commit 486dc5c

File tree

6 files changed

+26
-12
lines changed

6 files changed

+26
-12
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@ util/gh-pages/lints.json
2828
*.rs.bk
2929

3030
helper.txt
31-
31+
*.iml
3232
.vscode
33+
.idea

clippy_lints/src/doc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use url::Url;
1212
///
1313
/// **Why is this bad?** *Rustdoc* supports markdown formatting, `_`, `::` and
1414
/// camel-case probably indicates some code which should be included between
15-
/// ticks. `_` can also be used for empasis in markdown, this lint tries to
15+
/// ticks. `_` can also be used for emphasis in markdown, this lint tries to
1616
/// consider that.
1717
///
1818
/// **Known problems:** Lots of bad docs won’t be fixed, what the lint checks

clippy_lints/src/write.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,11 @@ fn check_write_variants<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr,
230230
"using `write!()` with a format string that ends in a \
231231
newline, consider using `writeln!()` instead");
232232
},
233-
"writeln" => if has_empty_arg(cx, span, fmtstr, fmtlen) {
233+
"writeln" => if let Some(final_span) = has_empty_arg(cx, span, fmtstr, fmtlen) {
234234
span_lint_and_sugg(
235235
cx,
236236
WRITE_WITH_NEWLINE,
237-
span,
237+
final_span,
238238
"using `writeln!(v, \"\")`",
239239
"replace it with",
240240
"writeln!(v)".to_string(),
@@ -295,11 +295,11 @@ fn check_print_variants<'a, 'tcx>(
295295
newline, consider using `println!()` instead");
296296
},
297297
"println" =>
298-
if has_empty_arg(cx, span, fmtstr, fmtlen) {
298+
if let Some(final_span) = has_empty_arg(cx, span, fmtstr, fmtlen) {
299299
span_lint_and_sugg(
300300
cx,
301301
PRINT_WITH_NEWLINE,
302-
span,
302+
final_span,
303303
"using `println!(\"\")`",
304304
"replace it with",
305305
"println!()".to_string(),
@@ -390,7 +390,7 @@ fn has_newline_end(args: &HirVec<Expr>, fmtstr: InternedString, fmtlen: usize) -
390390
}
391391

392392
/// Check for writeln!(v, "") / println!("")
393-
fn has_empty_arg<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, span: Span, fmtstr: InternedString, fmtlen: usize) -> bool {
393+
fn has_empty_arg<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, span: Span, fmtstr: InternedString, fmtlen: usize) -> Option<Span> {
394394
if_chain! {
395395
// check that the string is empty
396396
if fmtlen == 1;
@@ -400,10 +400,13 @@ fn has_empty_arg<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, span: Span, fmtstr: Inter
400400
if let Ok(snippet) = cx.sess().codemap().span_to_snippet(span);
401401
if snippet.contains("\"\"");
402402
then {
403-
return true
403+
if snippet.ends_with(';') {
404+
return Some(cx.sess().codemap().span_until_char(span, ';'));
405+
}
406+
return Some(span)
404407
}
405408
}
406-
false
409+
None
407410
}
408411

409412
/// Returns the slice of format string parts in an `Arguments::new_v1` call.

tests/ui/println_empty_string.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
fn main() {
22
println!();
33
println!("");
4+
5+
match "a" {
6+
_ => println!(""),
7+
}
48
}

tests/ui/println_empty_string.stderr

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@ error: using `println!("")`
22
--> $DIR/println_empty_string.rs:3:5
33
|
44
3 | println!("");
5-
| ^^^^^^^^^^^^^ help: replace it with: `println!()`
5+
| ^^^^^^^^^^^^ help: replace it with: `println!()`
66
|
77
= note: `-D print-with-newline` implied by `-D warnings`
88

9-
error: aborting due to previous error
9+
error: using `println!("")`
10+
--> $DIR/println_empty_string.rs:6:14
11+
|
12+
6 | _ => println!(""),
13+
| ^^^^^^^^^^^^ help: replace it with: `println!()`
14+
15+
error: aborting due to 2 previous errors
1016

tests/ui/writeln_empty_string.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: using `writeln!(v, "")`
22
--> $DIR/writeln_empty_string.rs:9:5
33
|
44
9 | writeln!(&mut v, "");
5-
| ^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `writeln!(v)`
5+
| ^^^^^^^^^^^^^^^^^^^^ help: replace it with: `writeln!(v)`
66
|
77
= note: `-D write-with-newline` implied by `-D warnings`
88

0 commit comments

Comments
 (0)