Skip to content

Commit

Permalink
Add lint print_stderr
Browse files Browse the repository at this point in the history
Resolves rust-lang#6348
Almost identical to print_stdout, this lint applies to the
`eprintln!` and `eprint!` macros rather than `println!` and
`print!`.
  • Loading branch information
justjosias committed Dec 4, 2020
1 parent 13c1a01 commit 9fb326a
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -1246,6 +1247,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),
]);
Expand Down
23 changes: 23 additions & 0 deletions clippy_lints/src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,24 @@ declare_clippy_lint! {
"printing on stdout"
}

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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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!`");
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/print_stderr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![warn(clippy::print_stderr)]

fn main() {
eprintln!("Hello");
eprint!("World");
}
16 changes: 16 additions & 0 deletions tests/ui/print_stderr.stderr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9fb326a

Please sign in to comment.