Skip to content

Commit b81141c

Browse files
justjosiasebroto
authored andcommitted
Add lint print_stderr
Resolves rust-lang#6348 Almost identical to print_stdout, this lint applies to the `eprintln!` and `eprint!` macros rather than `println!` and `print!`.
1 parent aaed9d9 commit b81141c

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2006,6 +2006,7 @@ Released 2018-09-13
20062006
[`possible_missing_comma`]: https://rust-lang.github.io/rust-clippy/master/index.html#possible_missing_comma
20072007
[`precedence`]: https://rust-lang.github.io/rust-clippy/master/index.html#precedence
20082008
[`print_literal`]: https://rust-lang.github.io/rust-clippy/master/index.html#print_literal
2009+
[`print_stderr`]: https://rust-lang.github.io/rust-clippy/master/index.html#print_stderr
20092010
[`print_stdout`]: https://rust-lang.github.io/rust-clippy/master/index.html#print_stdout
20102011
[`print_with_newline`]: https://rust-lang.github.io/rust-clippy/master/index.html#print_with_newline
20112012
[`println_empty_string`]: https://rust-lang.github.io/rust-clippy/master/index.html#println_empty_string

clippy_lints/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
934934
&wildcard_imports::WILDCARD_IMPORTS,
935935
&write::PRINTLN_EMPTY_STRING,
936936
&write::PRINT_LITERAL,
937+
&write::PRINT_STDERR,
937938
&write::PRINT_STDOUT,
938939
&write::PRINT_WITH_NEWLINE,
939940
&write::USE_DEBUG,
@@ -1247,6 +1248,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
12471248
LintId::of(&types::RC_BUFFER),
12481249
LintId::of(&unwrap_in_result::UNWRAP_IN_RESULT),
12491250
LintId::of(&verbose_file_reads::VERBOSE_FILE_READS),
1251+
LintId::of(&write::PRINT_STDERR),
12501252
LintId::of(&write::PRINT_STDOUT),
12511253
LintId::of(&write::USE_DEBUG),
12521254
]);

clippy_lints/src/write.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,24 @@ declare_clippy_lint! {
7575
"printing on stdout"
7676
}
7777

78+
declare_clippy_lint! {
79+
/// **What it does:** Checks for printing on *stderr*. The purpose of this lint
80+
/// is to catch debugging remnants.
81+
///
82+
/// **Why is this bad?** People often print on *stderr* while debugging an
83+
/// application and might forget to remove those prints afterward.
84+
///
85+
/// **Known problems:** Only catches `eprint!` and `eprintln!` calls.
86+
///
87+
/// **Example:**
88+
/// ```rust
89+
/// eprintln!("Hello world!");
90+
/// ```
91+
pub PRINT_STDERR,
92+
restriction,
93+
"printing on stderr"
94+
}
95+
7896
declare_clippy_lint! {
7997
/// **What it does:** Checks for use of `Debug` formatting. The purpose of this
8098
/// lint is to catch debugging remnants.
@@ -201,6 +219,7 @@ impl_lint_pass!(Write => [
201219
PRINT_WITH_NEWLINE,
202220
PRINTLN_EMPTY_STRING,
203221
PRINT_STDOUT,
222+
PRINT_STDERR,
204223
USE_DEBUG,
205224
PRINT_LITERAL,
206225
WRITE_WITH_NEWLINE,
@@ -260,6 +279,10 @@ impl EarlyLintPass for Write {
260279
);
261280
}
262281
}
282+
} else if mac.path == sym!(eprintln) {
283+
span_lint(cx, PRINT_STDERR, mac.span(), "use of `eprintln!`");
284+
} else if mac.path == sym!(eprint) {
285+
span_lint(cx, PRINT_STDERR, mac.span(), "use of `eprint!`");
263286
} else if mac.path == sym!(print) {
264287
if !is_build_script(cx) {
265288
span_lint(cx, PRINT_STDOUT, mac.span(), "use of `print!`");

tests/ui/print_stderr.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#![warn(clippy::print_stderr)]
2+
3+
fn main() {
4+
eprintln!("Hello");
5+
eprint!("World");
6+
}

tests/ui/print_stderr.stderr

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: use of `eprintln!`
2+
--> $DIR/print_stderr.rs:4:5
3+
|
4+
LL | eprintln!("Hello");
5+
| ^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::print-stderr` implied by `-D warnings`
8+
9+
error: use of `eprint!`
10+
--> $DIR/print_stderr.rs:5:5
11+
|
12+
LL | eprint!("World");
13+
| ^^^^^^^^^^^^^^^^
14+
15+
error: aborting due to 2 previous errors
16+

0 commit comments

Comments
 (0)