mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-10 23:24:24 +00:00
Add allow-print-in-tests config
Add a config, allow-print-in-tests, that can be set in clippy.toml which allows the usage of `[e]print[ln]!` macros in tests. Closes #9795
This commit is contained in:
parent
704e00cb75
commit
ddcfff6d9a
7 changed files with 61 additions and 3 deletions
|
@ -876,13 +876,14 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
|
|||
store.register_late_pass(|_| Box::<only_used_in_recursion::OnlyUsedInRecursion>::default());
|
||||
let allow_dbg_in_tests = conf.allow_dbg_in_tests;
|
||||
store.register_late_pass(move |_| Box::new(dbg_macro::DbgMacro::new(allow_dbg_in_tests)));
|
||||
let allow_print_in_tests = conf.allow_print_in_tests;
|
||||
store.register_late_pass(move |_| Box::new(write::Write::new(allow_print_in_tests)));
|
||||
let cargo_ignore_publish = conf.cargo_ignore_publish;
|
||||
store.register_late_pass(move |_| {
|
||||
Box::new(cargo::Cargo {
|
||||
ignore_publish: cargo_ignore_publish,
|
||||
})
|
||||
});
|
||||
store.register_late_pass(|_| Box::<write::Write>::default());
|
||||
store.register_early_pass(|| Box::new(crate_in_macro_def::CrateInMacroDef));
|
||||
store.register_early_pass(|| Box::new(empty_structs_with_brackets::EmptyStructsWithBrackets));
|
||||
store.register_late_pass(|_| Box::new(unnecessary_owned_empty_strings::UnnecessaryOwnedEmptyStrings));
|
||||
|
|
|
@ -389,6 +389,10 @@ define_Conf! {
|
|||
///
|
||||
/// Whether `dbg!` should be allowed in test functions
|
||||
(allow_dbg_in_tests: bool = false),
|
||||
/// Lint: PRINT_STDOUT, PRINT_STDERR.
|
||||
///
|
||||
/// Whether print macros (ex. `println!`) should be allowed in test functions
|
||||
(allow_print_in_tests: bool = false),
|
||||
/// Lint: RESULT_LARGE_ERR.
|
||||
///
|
||||
/// The maximum size of the `Err`-variant in a `Result` returned from a function
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
|
||||
use clippy_utils::macros::{root_macro_call_first_node, FormatArgsExpn, MacroCall};
|
||||
use clippy_utils::source::{expand_past_previous_comma, snippet_opt};
|
||||
use clippy_utils::{is_in_cfg_test, is_in_test_function};
|
||||
use rustc_ast::LitKind;
|
||||
use rustc_errors::Applicability;
|
||||
use rustc_hir::{Expr, ExprKind, HirIdMap, Impl, Item, ItemKind};
|
||||
|
@ -232,6 +233,16 @@ declare_clippy_lint! {
|
|||
#[derive(Default)]
|
||||
pub struct Write {
|
||||
in_debug_impl: bool,
|
||||
allow_print_in_tests: bool,
|
||||
}
|
||||
|
||||
impl Write {
|
||||
pub fn new(allow_print_in_tests: bool) -> Self {
|
||||
Self {
|
||||
allow_print_in_tests,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl_lint_pass!(Write => [
|
||||
|
@ -271,13 +282,15 @@ impl<'tcx> LateLintPass<'tcx> for Write {
|
|||
.as_ref()
|
||||
.map_or(false, |crate_name| crate_name == "build_script_build");
|
||||
|
||||
let allowed_in_tests = self.allow_print_in_tests
|
||||
&& (is_in_test_function(cx.tcx, expr.hir_id) || is_in_cfg_test(cx.tcx, expr.hir_id));
|
||||
match diag_name {
|
||||
sym::print_macro | sym::println_macro => {
|
||||
sym::print_macro | sym::println_macro if !allowed_in_tests => {
|
||||
if !is_build_script {
|
||||
span_lint(cx, PRINT_STDOUT, macro_call.span, &format!("use of `{name}!`"));
|
||||
}
|
||||
},
|
||||
sym::eprint_macro | sym::eprintln_macro => {
|
||||
sym::eprint_macro | sym::eprintln_macro if !allowed_in_tests => {
|
||||
span_lint(cx, PRINT_STDERR, macro_call.span, &format!("use of `{name}!`"));
|
||||
},
|
||||
sym::write_macro | sym::writeln_macro => {},
|
||||
|
|
1
tests/ui-toml/print_macro/clippy.toml
Normal file
1
tests/ui-toml/print_macro/clippy.toml
Normal file
|
@ -0,0 +1 @@
|
|||
allow-print-in-tests = true
|
20
tests/ui-toml/print_macro/print_macro.rs
Normal file
20
tests/ui-toml/print_macro/print_macro.rs
Normal file
|
@ -0,0 +1,20 @@
|
|||
// compile-flags: --test
|
||||
#![warn(clippy::print_stdout)]
|
||||
#![warn(clippy::print_stderr)]
|
||||
|
||||
fn foo(n: u32) {
|
||||
print!("{n}");
|
||||
eprint!("{n}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn foo1() {
|
||||
print!("{}", 1);
|
||||
eprint!("{}", 1);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn foo3() {
|
||||
print!("{}", 1);
|
||||
eprint!("{}", 1);
|
||||
}
|
18
tests/ui-toml/print_macro/print_macro.stderr
Normal file
18
tests/ui-toml/print_macro/print_macro.stderr
Normal file
|
@ -0,0 +1,18 @@
|
|||
error: use of `print!`
|
||||
--> $DIR/print_macro.rs:6:5
|
||||
|
|
||||
LL | print!("{n}");
|
||||
| ^^^^^^^^^^^^^
|
||||
|
|
||||
= note: `-D clippy::print-stdout` implied by `-D warnings`
|
||||
|
||||
error: use of `eprint!`
|
||||
--> $DIR/print_macro.rs:7:5
|
||||
|
|
||||
LL | eprint!("{n}");
|
||||
| ^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: `-D clippy::print-stderr` implied by `-D warnings`
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of
|
||||
allow-dbg-in-tests
|
||||
allow-expect-in-tests
|
||||
allow-print-in-tests
|
||||
allow-unwrap-in-tests
|
||||
allowed-scripts
|
||||
arithmetic-side-effects-allowed
|
||||
|
|
Loading…
Reference in a new issue