From d175c797e5299ba530460a619a4c34d9a818b187 Mon Sep 17 00:00:00 2001 From: "MSI\\Stew's Laptop" Date: Mon, 9 Apr 2018 00:07:47 -0400 Subject: [PATCH] fixing error message for empty println macro --- .gitignore | 3 ++- clippy_lints/src/doc.rs | 2 +- clippy_lints/src/write.rs | 17 ++++++++++------- tests/ui/println_empty_string.rs | 4 ++++ tests/ui/println_empty_string.stderr | 10 ++++++++-- tests/ui/writeln_empty_string.stderr | 2 +- 6 files changed, 26 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 6f472c418d2..3c8f96c0f6f 100644 --- a/.gitignore +++ b/.gitignore @@ -28,5 +28,6 @@ util/gh-pages/lints.json *.rs.bk helper.txt - +*.iml .vscode +.idea diff --git a/clippy_lints/src/doc.rs b/clippy_lints/src/doc.rs index 1439c23f0fa..5f6c15e222d 100644 --- a/clippy_lints/src/doc.rs +++ b/clippy_lints/src/doc.rs @@ -12,7 +12,7 @@ use url::Url; /// /// **Why is this bad?** *Rustdoc* supports markdown formatting, `_`, `::` and /// camel-case probably indicates some code which should be included between -/// ticks. `_` can also be used for empasis in markdown, this lint tries to +/// ticks. `_` can also be used for emphasis in markdown, this lint tries to /// consider that. /// /// **Known problems:** Lots of bad docs won’t be fixed, what the lint checks diff --git a/clippy_lints/src/write.rs b/clippy_lints/src/write.rs index 0531c14cba8..4773aab27da 100644 --- a/clippy_lints/src/write.rs +++ b/clippy_lints/src/write.rs @@ -230,11 +230,11 @@ fn check_write_variants<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, "using `write!()` with a format string that ends in a \ newline, consider using `writeln!()` instead"); }, - "writeln" => if has_empty_arg(cx, span, fmtstr, fmtlen) { + "writeln" => if let Some(final_span) = has_empty_arg(cx, span, fmtstr, fmtlen) { span_lint_and_sugg( cx, WRITE_WITH_NEWLINE, - span, + final_span, "using `writeln!(v, \"\")`", "replace it with", "writeln!(v)".to_string(), @@ -295,11 +295,11 @@ fn check_print_variants<'a, 'tcx>( newline, consider using `println!()` instead"); }, "println" => - if has_empty_arg(cx, span, fmtstr, fmtlen) { + if let Some(final_span) = has_empty_arg(cx, span, fmtstr, fmtlen) { span_lint_and_sugg( cx, PRINT_WITH_NEWLINE, - span, + final_span, "using `println!(\"\")`", "replace it with", "println!()".to_string(), @@ -390,7 +390,7 @@ fn has_newline_end(args: &HirVec, fmtstr: InternedString, fmtlen: usize) - } /// Check for writeln!(v, "") / println!("") -fn has_empty_arg<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, span: Span, fmtstr: InternedString, fmtlen: usize) -> bool { +fn has_empty_arg<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, span: Span, fmtstr: InternedString, fmtlen: usize) -> Option { if_chain! { // check that the string is empty if fmtlen == 1; @@ -400,10 +400,13 @@ fn has_empty_arg<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, span: Span, fmtstr: Inter if let Ok(snippet) = cx.sess().codemap().span_to_snippet(span); if snippet.contains("\"\""); then { - return true + if snippet.ends_with(';') { + return Some(cx.sess().codemap().span_until_char(span, ';')); + } + return Some(span) } } - false + None } /// Returns the slice of format string parts in an `Arguments::new_v1` call. diff --git a/tests/ui/println_empty_string.rs b/tests/ui/println_empty_string.rs index 82495f1b39d..9df348050ad 100644 --- a/tests/ui/println_empty_string.rs +++ b/tests/ui/println_empty_string.rs @@ -1,4 +1,8 @@ fn main() { println!(); println!(""); + + match "a" { + _ => println!(""), + } } diff --git a/tests/ui/println_empty_string.stderr b/tests/ui/println_empty_string.stderr index f70b056e562..1148a4496a5 100644 --- a/tests/ui/println_empty_string.stderr +++ b/tests/ui/println_empty_string.stderr @@ -2,9 +2,15 @@ error: using `println!("")` --> $DIR/println_empty_string.rs:3:5 | 3 | println!(""); - | ^^^^^^^^^^^^^ help: replace it with: `println!()` + | ^^^^^^^^^^^^ help: replace it with: `println!()` | = note: `-D print-with-newline` implied by `-D warnings` -error: aborting due to previous error +error: using `println!("")` + --> $DIR/println_empty_string.rs:6:14 + | +6 | _ => println!(""), + | ^^^^^^^^^^^^ help: replace it with: `println!()` + +error: aborting due to 2 previous errors diff --git a/tests/ui/writeln_empty_string.stderr b/tests/ui/writeln_empty_string.stderr index e20aad779d9..b4649384865 100644 --- a/tests/ui/writeln_empty_string.stderr +++ b/tests/ui/writeln_empty_string.stderr @@ -2,7 +2,7 @@ error: using `writeln!(v, "")` --> $DIR/writeln_empty_string.rs:9:5 | 9 | writeln!(&mut v, ""); - | ^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `writeln!(v)` + | ^^^^^^^^^^^^^^^^^^^^ help: replace it with: `writeln!(v)` | = note: `-D write-with-newline` implied by `-D warnings`