about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPhilipp Hansch <dev@phansch.net>2018-11-29 08:15:44 +0100
committerPhilipp Hansch <dev@phansch.net>2018-12-12 07:31:04 +0100
commit9a6216ed05e353825a712c265923993b8b19d887 (patch)
treefb5be388838e2d3a65ff30686832bbb3a8944f23
parent5f007a88b4d650068525d4300aa9d773a572de11 (diff)
downloadrust-9a6216ed05e353825a712c265923993b8b19d887.tar.gz
rust-9a6216ed05e353825a712c265923993b8b19d887.zip
Address review feedback
* Fix typo
* Handle None value instead of using `unwrap()`
* `pop()` instead of `x.truncate(x.len() - 1)`
-rw-r--r--clippy_lints/src/explicit_write.rs78
1 files changed, 52 insertions, 26 deletions
diff --git a/clippy_lints/src/explicit_write.rs b/clippy_lints/src/explicit_write.rs
index 996dc126ec1..79c4c5f0530 100644
--- a/clippy_lints/src/explicit_write.rs
+++ b/clippy_lints/src/explicit_write.rs
@@ -83,35 +83,61 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
                 };
 
                 // We need to remove the last trailing newline from the string because the
-                // underlying `fmt::write` function doesn't know wether `println!` or `print!` was
+                // underlying `fmt::write` function doesn't know whether `println!` or `print!` was
                 // used.
-                let mut write_output: String = write_output_string(write_args).unwrap();
-                if write_output.ends_with('\n') {
-                    write_output.truncate(write_output.len() - 1)
-                }
-                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())
-                    );
+                if let Some(mut write_output) = write_output_string(write_args) {
+                    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())
+                        );
+                    } 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())
+                        );
+                    }
                 } 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())
-                    );
+                    // 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),
+                        );
+                    }
                 }
+
             }
         }
     }