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!`.
This commit is contained in:
Josias 2020-11-22 19:02:57 +01:00 committed by Eduardo Broto
parent aaed9d9926
commit b81141cfb9
5 changed files with 48 additions and 0 deletions

View File

@ -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

View File

@ -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),
]);

View File

@ -75,6 +75,24 @@
"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.
@ -201,6 +219,7 @@ pub struct Write {
PRINT_WITH_NEWLINE,
PRINTLN_EMPTY_STRING,
PRINT_STDOUT,
PRINT_STDERR,
USE_DEBUG,
PRINT_LITERAL,
WRITE_WITH_NEWLINE,
@ -260,6 +279,10 @@ fn is_build_script(cx: &EarlyContext<'_>) -> bool {
);
}
}
} 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!`");

6
tests/ui/print_stderr.rs Normal file
View File

@ -0,0 +1,6 @@
#![warn(clippy::print_stderr)]
fn main() {
eprintln!("Hello");
eprint!("World");
}

View File

@ -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