From 82d91c5fcb01c35c0337d152cfac9c85957e2eb3 Mon Sep 17 00:00:00 2001 From: Pascal Hertleif Date: Thu, 4 Jan 2018 12:37:47 +0100 Subject: [PATCH] Add auto-fixable `println!()` suggestion Fixes #2319 --- clippy_lints/src/print.rs | 12 +++++++++--- tests/ui/println_empty_string.stderr | 4 ++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/clippy_lints/src/print.rs b/clippy_lints/src/print.rs index 61ed8d5ac250..451b27033ea3 100644 --- a/clippy_lints/src/print.rs +++ b/clippy_lints/src/print.rs @@ -5,7 +5,7 @@ use rustc::lint::*; use syntax::ast::LitKind; use syntax::symbol::InternedString; use syntax_pos::Span; -use utils::{is_expn_of, match_def_path, match_path, resolve_node, span_lint}; +use utils::{is_expn_of, match_def_path, match_path, resolve_node, span_lint, span_lint_and_sugg}; use utils::{opt_def_id, paths}; /// **What it does:** This lint warns when you using `println!("")` to @@ -182,8 +182,14 @@ fn check_println<'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 { - span_lint(cx, PRINT_WITH_NEWLINE, span, - "using `println!(\"\")`, consider using `println!()` instead"); + span_lint_and_sugg( + cx, + PRINT_WITH_NEWLINE, + span, + "using `println!(\"\")`", + "replace it with", + "println!()".to_string(), + ); } } } diff --git a/tests/ui/println_empty_string.stderr b/tests/ui/println_empty_string.stderr index 8beca8b88cb3..2036d7d976bf 100644 --- a/tests/ui/println_empty_string.stderr +++ b/tests/ui/println_empty_string.stderr @@ -1,8 +1,8 @@ -error: using `println!("")`, consider using `println!()` instead +error: using `println!("")` --> $DIR/println_empty_string.rs:3:5 | 3 | println!(""); - | ^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^ help: replace it with: `println!()` | = note: `-D print-with-newline` implied by `-D warnings`