diff options
| author | Cameron Steffen <cam.steffen94@gmail.com> | 2021-07-11 20:21:21 -0500 |
|---|---|---|
| committer | Cameron Steffen <cam.steffen94@gmail.com> | 2021-07-13 08:57:16 -0500 |
| commit | 73ffae6ea1b8639050ca19db9732a7a25daae482 (patch) | |
| tree | ee432608ed871ecf2a0c752cb6edb0632188daec | |
| parent | e6ab222b814f20375041a5dd39e7605eed079c70 (diff) | |
| download | rust-73ffae6ea1b8639050ca19db9732a7a25daae482.tar.gz rust-73ffae6ea1b8639050ca19db9732a7a25daae482.zip | |
Reduce redundant code
| -rw-r--r-- | clippy_lints/src/explicit_write.rs | 71 |
1 files changed, 24 insertions, 47 deletions
diff --git a/clippy_lints/src/explicit_write.rs b/clippy_lints/src/explicit_write.rs index eaea70a4294..9a07d422b63 100644 --- a/clippy_lints/src/explicit_write.rs +++ b/clippy_lints/src/explicit_write.rs @@ -67,62 +67,39 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitWrite { // We need to remove the last trailing newline from the string because the // underlying `fmt::write` function doesn't know whether `println!` or `print!` was // used. + let (used, sugg_mac) = if let Some(macro_name) = calling_macro { + ( + format!("{}!({}(), ...)", macro_name, dest_name), + macro_name.replace("write", "print"), + ) + } else { + ( + format!("{}().write_fmt(...)", dest_name), + "print".into(), + ) + }; + let msg = format!("use of `{}.unwrap()`", used); if let [write_output] = *format_args.format_string_symbols { let mut write_output = write_output.to_string(); if write_output.ends_with('\n') { write_output.pop(); } - if let Some(macro_name) = calling_macro { - span_lint_and_sugg( - cx, - EXPLICIT_WRITE, - expr.span, - &format!( - "use of `{}!({}(), ...).unwrap()`", - macro_name, - dest_name - ), - "try this", - format!("{}{}!(\"{}\")", prefix, macro_name.replace("write", "print"), write_output.escape_default()), - Applicability::MachineApplicable - ); - } else { - span_lint_and_sugg( - cx, - EXPLICIT_WRITE, - expr.span, - &format!("use of `{}().write_fmt(...).unwrap()`", dest_name), - "try this", - format!("{}print!(\"{}\")", prefix, write_output.escape_default()), - Applicability::MachineApplicable - ); - } + let sugg = format!("{}{}!(\"{}\")", prefix, sugg_mac, write_output.escape_default()); + span_lint_and_sugg( + cx, + EXPLICIT_WRITE, + expr.span, + &msg, + "try this", + sugg, + Applicability::MachineApplicable + ); } else { // We don't have a proper suggestion - if let Some(macro_name) = calling_macro { - span_lint( - cx, - EXPLICIT_WRITE, - expr.span, - &format!( - "use of `{}!({}(), ...).unwrap()`. Consider using `{}{}!` instead", - macro_name, - dest_name, - prefix, - macro_name.replace("write", "print") - ) - ); - } else { - span_lint( - cx, - EXPLICIT_WRITE, - expr.span, - &format!("use of `{}().write_fmt(...).unwrap()`. Consider using `{}print!` instead", dest_name, prefix), - ); - } + let msg = format!("{}. Consider using `{}{}!` instead", msg, prefix, sugg_mac); + span_lint(cx, EXPLICIT_WRITE, expr.span, &msg); } - } } } |
