diff --git a/clippy_lints/src/write.rs b/clippy_lints/src/write.rs index 3c912756d2d..2231715c8d2 100644 --- a/clippy_lints/src/write.rs +++ b/clippy_lints/src/write.rs @@ -3,6 +3,7 @@ use rustc::{declare_lint, lint_array}; use syntax::ast::*; use syntax::tokenstream::{ThinTokenStream, TokenStream}; use syntax::parse::{token, parser}; +use std::borrow::Cow; use crate::utils::{span_lint, span_lint_and_sugg, snippet}; /// **What it does:** This lint warns when you use `println!("")` to @@ -212,7 +213,7 @@ impl EarlyLintPass for Pass { let check_tts = check_tts(cx, &mac.node.tts, true); if let Some(fmtstr) = check_tts.0 { if fmtstr == "" { - let suggestion = check_tts.1.map_or("v", |expr| snippet(cx, expr.span, "v").into_owned().as_str()); + let suggestion = check_tts.1.map_or(Cow::Borrowed("v"), |expr| snippet(cx, expr.span, "v")); span_lint_and_sugg( cx, @@ -220,7 +221,7 @@ impl EarlyLintPass for Pass { mac.span, format!("using writeln!({}, \"\")", suggestion).as_str(), "replace it with", - format!("writeln!({})", "v"), + format!("writeln!({})", suggestion), ); } } @@ -239,20 +240,19 @@ fn check_tts<'a>(cx: &EarlyContext<'a>, tts: &ThinTokenStream, is_write: bool) - ); let mut expr: Option = None; if is_write { - // skip the initial write target - expr = match parser.parse_expr().map_err(|mut err| err.cancel()).ok() { - Some(p) => Some(p.and_then(|expr| expr)), - None => return (None, None), + expr = match parser.parse_expr().map_err(|mut err| err.cancel()) { + Ok(p) => Some(p.into_inner()), + Err(_) => return (None, None), }; // might be `writeln!(foo)` - if let None = parser.expect(&token::Comma).map_err(|mut err| err.cancel()).ok() { + if parser.expect(&token::Comma).map_err(|mut err| err.cancel()).is_err() { return (None, expr); } } - let fmtstr = match parser.parse_str().map_err(|mut err| err.cancel()).ok() { - Some(token) => token.0.to_string(), - None => return (None, expr), + let fmtstr = match parser.parse_str().map_err(|mut err| err.cancel()) { + Ok(token) => token.0.to_string(), + Err(_) => return (None, expr), }; use fmt_macros::*; let tmp = fmtstr.clone(); @@ -281,9 +281,9 @@ fn check_tts<'a>(cx: &EarlyContext<'a>, tts: &ThinTokenStream, is_write: bool) - assert!(parser.eat(&token::Eof)); return (Some(fmtstr), expr); } - let token_expr = match parser.parse_expr().map_err(|mut err| err.cancel()).ok() { - Some(expr) => expr, - None => return (Some(fmtstr), None), + let token_expr = match parser.parse_expr().map_err(|mut err| err.cancel()) { + Ok(expr) => expr, + Err(_) => return (Some(fmtstr), None), }; const SIMPLE: FormatSpec<'_> = FormatSpec { fill: None, diff --git a/tests/ui/writeln_empty_string.rs b/tests/ui/writeln_empty_string.rs index c7092eb8c4b..faccfd8291c 100644 --- a/tests/ui/writeln_empty_string.rs +++ b/tests/ui/writeln_empty_string.rs @@ -5,9 +5,12 @@ use std::io::Write; fn main() { let mut v = Vec::new(); - // This should fail + // These should fail writeln!(&mut v, ""); + let mut suggestion = Vec::new(); + writeln!(&mut suggestion, ""); + // These should be fine writeln!(&mut v); writeln!(&mut v, " "); diff --git a/tests/ui/writeln_empty_string.stderr b/tests/ui/writeln_empty_string.stderr index 16a8e0a203d..8bfec673c4a 100644 --- a/tests/ui/writeln_empty_string.stderr +++ b/tests/ui/writeln_empty_string.stderr @@ -1,10 +1,16 @@ -error: using `writeln!(v, "")` +error: using writeln!(&mut v, "") --> $DIR/writeln_empty_string.rs:9:5 | 9 | writeln!(&mut v, ""); - | ^^^^^^^^^^^^^^^^^^^^ help: replace it with: `writeln!(v)` + | ^^^^^^^^^^^^^^^^^^^^ help: replace it with: `writeln!(&mut v)` | = note: `-D writeln-empty-string` implied by `-D warnings` -error: aborting due to previous error +error: using writeln!(&mut suggestion, "") + --> $DIR/writeln_empty_string.rs:12:5 + | +12 | writeln!(&mut suggestion, ""); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `writeln!(&mut suggestion)` + +error: aborting due to 2 previous errors