Auto merge of #6367 - justjosias:6348-print-stderr, r=ebroto
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!`. changelog: Add new lint [`print_stderr`]. [`println_empty_string`] and [`print_with_newline`] now apply to `eprint!()` and `eprintln!()` respectively.
This commit is contained in:
commit
b02b0c737a
10 changed files with 294 additions and 38 deletions
|
|
@ -936,6 +936,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,
|
||||
|
|
@ -1250,6 +1251,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),
|
||||
]);
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
@ -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,
|
||||
|
|
@ -243,47 +262,22 @@ impl EarlyLintPass for Write {
|
|||
.map_or(false, |crate_name| crate_name == "build_script_build")
|
||||
}
|
||||
|
||||
if mac.path == sym!(println) {
|
||||
if !is_build_script(cx) {
|
||||
span_lint(cx, PRINT_STDOUT, mac.span(), "use of `println!`");
|
||||
}
|
||||
if let (Some(fmt_str), _) = self.check_tts(cx, mac.args.inner_tokens(), false) {
|
||||
if fmt_str.symbol == Symbol::intern("") {
|
||||
span_lint_and_sugg(
|
||||
cx,
|
||||
PRINTLN_EMPTY_STRING,
|
||||
mac.span(),
|
||||
"using `println!(\"\")`",
|
||||
"replace it with",
|
||||
"println!()".to_string(),
|
||||
Applicability::MachineApplicable,
|
||||
);
|
||||
}
|
||||
}
|
||||
} else if mac.path == sym!(print) {
|
||||
if mac.path == sym!(print) {
|
||||
if !is_build_script(cx) {
|
||||
span_lint(cx, PRINT_STDOUT, mac.span(), "use of `print!`");
|
||||
}
|
||||
if let (Some(fmt_str), _) = self.check_tts(cx, mac.args.inner_tokens(), false) {
|
||||
if check_newlines(&fmt_str) {
|
||||
span_lint_and_then(
|
||||
cx,
|
||||
PRINT_WITH_NEWLINE,
|
||||
mac.span(),
|
||||
"using `print!()` with a format string that ends in a single newline",
|
||||
|err| {
|
||||
err.multipart_suggestion(
|
||||
"use `println!` instead",
|
||||
vec![
|
||||
(mac.path.span, String::from("println")),
|
||||
(newline_span(&fmt_str), String::new()),
|
||||
],
|
||||
Applicability::MachineApplicable,
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
self.lint_print_with_newline(cx, mac);
|
||||
} else if mac.path == sym!(println) {
|
||||
if !is_build_script(cx) {
|
||||
span_lint(cx, PRINT_STDOUT, mac.span(), "use of `println!`");
|
||||
}
|
||||
self.lint_println_empty_string(cx, mac);
|
||||
} else if mac.path == sym!(eprint) {
|
||||
span_lint(cx, PRINT_STDERR, mac.span(), "use of `eprint!`");
|
||||
self.lint_print_with_newline(cx, mac);
|
||||
} else if mac.path == sym!(eprintln) {
|
||||
span_lint(cx, PRINT_STDERR, mac.span(), "use of `eprintln!`");
|
||||
self.lint_println_empty_string(cx, mac);
|
||||
} else if mac.path == sym!(write) {
|
||||
if let (Some(fmt_str), _) = self.check_tts(cx, mac.args.inner_tokens(), true) {
|
||||
if check_newlines(&fmt_str) {
|
||||
|
|
@ -487,6 +481,45 @@ impl Write {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn lint_println_empty_string(&self, cx: &EarlyContext<'_>, mac: &MacCall) {
|
||||
if let (Some(fmt_str), _) = self.check_tts(cx, mac.args.inner_tokens(), false) {
|
||||
if fmt_str.symbol == Symbol::intern("") {
|
||||
let name = mac.path.segments[0].ident.name;
|
||||
span_lint_and_sugg(
|
||||
cx,
|
||||
PRINTLN_EMPTY_STRING,
|
||||
mac.span(),
|
||||
&format!("using `{}!(\"\")`", name),
|
||||
"replace it with",
|
||||
format!("{}!()", name),
|
||||
Applicability::MachineApplicable,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn lint_print_with_newline(&self, cx: &EarlyContext<'_>, mac: &MacCall) {
|
||||
if let (Some(fmt_str), _) = self.check_tts(cx, mac.args.inner_tokens(), false) {
|
||||
if check_newlines(&fmt_str) {
|
||||
let name = mac.path.segments[0].ident.name;
|
||||
let suggested = format!("{}ln", name);
|
||||
span_lint_and_then(
|
||||
cx,
|
||||
PRINT_WITH_NEWLINE,
|
||||
mac.span(),
|
||||
&format!("using `{}!()` with a format string that ends in a single newline", name),
|
||||
|err| {
|
||||
err.multipart_suggestion(
|
||||
&format!("use `{}!` instead", suggested),
|
||||
vec![(mac.path.span, suggested), (newline_span(&fmt_str), String::new())],
|
||||
Applicability::MachineApplicable,
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks if the format string contains a single newline that terminates it.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue