about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJosias <me@josias.dev>2020-11-22 19:02:57 +0100
committerEduardo Broto <ebroto@tutanota.com>2020-12-07 23:38:56 +0100
commitb81141cfb91cbf39e87e32a27530537f18e85405 (patch)
tree0853088a2fda1ccfd63d926838bc52e574a81afe
parentaaed9d99267cccacd99929a0c28c4b0a552cb443 (diff)
downloadrust-b81141cfb91cbf39e87e32a27530537f18e85405.tar.gz
rust-b81141cfb91cbf39e87e32a27530537f18e85405.zip
Add lint print_stderr
Resolves #6348
Almost identical to print_stdout, this lint applies to the
`eprintln!` and `eprint!` macros rather than `println!` and
`print!`.
-rw-r--r--CHANGELOG.md1
-rw-r--r--clippy_lints/src/lib.rs2
-rw-r--r--clippy_lints/src/write.rs23
-rw-r--r--tests/ui/print_stderr.rs6
-rw-r--r--tests/ui/print_stderr.stderr16
5 files changed, 48 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c7e02aaf4e1..1f2c4f310de 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2006,6 +2006,7 @@ Released 2018-09-13
 [`possible_missing_comma`]: https://rust-lang.github.io/rust-clippy/master/index.html#possible_missing_comma
 [`precedence`]: https://rust-lang.github.io/rust-clippy/master/index.html#precedence
 [`print_literal`]: https://rust-lang.github.io/rust-clippy/master/index.html#print_literal
+[`print_stderr`]: https://rust-lang.github.io/rust-clippy/master/index.html#print_stderr
 [`print_stdout`]: https://rust-lang.github.io/rust-clippy/master/index.html#print_stdout
 [`print_with_newline`]: https://rust-lang.github.io/rust-clippy/master/index.html#print_with_newline
 [`println_empty_string`]: https://rust-lang.github.io/rust-clippy/master/index.html#println_empty_string
diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs
index 2b99ed570b1..0e630a352fe 100644
--- a/clippy_lints/src/lib.rs
+++ b/clippy_lints/src/lib.rs
@@ -934,6 +934,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
         &wildcard_imports::WILDCARD_IMPORTS,
         &write::PRINTLN_EMPTY_STRING,
         &write::PRINT_LITERAL,
+        &write::PRINT_STDERR,
         &write::PRINT_STDOUT,
         &write::PRINT_WITH_NEWLINE,
         &write::USE_DEBUG,
@@ -1247,6 +1248,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
         LintId::of(&types::RC_BUFFER),
         LintId::of(&unwrap_in_result::UNWRAP_IN_RESULT),
         LintId::of(&verbose_file_reads::VERBOSE_FILE_READS),
+        LintId::of(&write::PRINT_STDERR),
         LintId::of(&write::PRINT_STDOUT),
         LintId::of(&write::USE_DEBUG),
     ]);
diff --git a/clippy_lints/src/write.rs b/clippy_lints/src/write.rs
index ff414f748ef..2248bc1e32e 100644
--- a/clippy_lints/src/write.rs
+++ b/clippy_lints/src/write.rs
@@ -76,6 +76,24 @@ declare_clippy_lint! {
 }
 
 declare_clippy_lint! {
+    /// **What it does:** Checks for printing on *stderr*. The purpose of this lint
+    /// is to catch debugging remnants.
+    ///
+    /// **Why is this bad?** People often print on *stderr* while debugging an
+    /// application and might forget to remove those prints afterward.
+    ///
+    /// **Known problems:** Only catches `eprint!` and `eprintln!` calls.
+    ///
+    /// **Example:**
+    /// ```rust
+    /// eprintln!("Hello world!");
+    /// ```
+    pub PRINT_STDERR,
+    restriction,
+    "printing on stderr"
+}
+
+declare_clippy_lint! {
     /// **What it does:** Checks for use of `Debug` formatting. The purpose of this
     /// lint is to catch debugging remnants.
     ///
@@ -201,6 +219,7 @@ impl_lint_pass!(Write => [
     PRINT_WITH_NEWLINE,
     PRINTLN_EMPTY_STRING,
     PRINT_STDOUT,
+    PRINT_STDERR,
     USE_DEBUG,
     PRINT_LITERAL,
     WRITE_WITH_NEWLINE,
@@ -260,6 +279,10 @@ impl EarlyLintPass for Write {
                     );
                 }
             }
+        } else if mac.path == sym!(eprintln) {
+            span_lint(cx, PRINT_STDERR, mac.span(), "use of `eprintln!`");
+        } else if mac.path == sym!(eprint) {
+            span_lint(cx, PRINT_STDERR, mac.span(), "use of `eprint!`");
         } else if mac.path == sym!(print) {
             if !is_build_script(cx) {
                 span_lint(cx, PRINT_STDOUT, mac.span(), "use of `print!`");
diff --git a/tests/ui/print_stderr.rs b/tests/ui/print_stderr.rs
new file mode 100644
index 00000000000..e53f46b1c2f
--- /dev/null
+++ b/tests/ui/print_stderr.rs
@@ -0,0 +1,6 @@
+#![warn(clippy::print_stderr)]
+
+fn main() {
+    eprintln!("Hello");
+    eprint!("World");
+}
diff --git a/tests/ui/print_stderr.stderr b/tests/ui/print_stderr.stderr
new file mode 100644
index 00000000000..7252fce72c6
--- /dev/null
+++ b/tests/ui/print_stderr.stderr
@@ -0,0 +1,16 @@
+error: use of `eprintln!`
+  --> $DIR/print_stderr.rs:4:5
+   |
+LL |     eprintln!("Hello");
+   |     ^^^^^^^^^^^^^^^^^^
+   |
+   = note: `-D clippy::print-stderr` implied by `-D warnings`
+
+error: use of `eprint!`
+  --> $DIR/print_stderr.rs:5:5
+   |
+LL |     eprint!("World");
+   |     ^^^^^^^^^^^^^^^^
+
+error: aborting due to 2 previous errors
+