Skip to content

Commit e547411

Browse files
committed
Auto merge of #9797 - trevarj:print-config, r=xFrednet
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 --- changelog: Enhancement: [print_stdout], [print_stderr]: Can now be enabled in test with the `allow-print-in-tests` config value
2 parents 9e0de8c + ddcfff6 commit e547411

File tree

7 files changed

+61
-3
lines changed

7 files changed

+61
-3
lines changed

clippy_lints/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -876,13 +876,14 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
876876
store.register_late_pass(|_| Box::<only_used_in_recursion::OnlyUsedInRecursion>::default());
877877
let allow_dbg_in_tests = conf.allow_dbg_in_tests;
878878
store.register_late_pass(move |_| Box::new(dbg_macro::DbgMacro::new(allow_dbg_in_tests)));
879+
let allow_print_in_tests = conf.allow_print_in_tests;
880+
store.register_late_pass(move |_| Box::new(write::Write::new(allow_print_in_tests)));
879881
let cargo_ignore_publish = conf.cargo_ignore_publish;
880882
store.register_late_pass(move |_| {
881883
Box::new(cargo::Cargo {
882884
ignore_publish: cargo_ignore_publish,
883885
})
884886
});
885-
store.register_late_pass(|_| Box::<write::Write>::default());
886887
store.register_early_pass(|| Box::new(crate_in_macro_def::CrateInMacroDef));
887888
store.register_early_pass(|| Box::new(empty_structs_with_brackets::EmptyStructsWithBrackets));
888889
store.register_late_pass(|_| Box::new(unnecessary_owned_empty_strings::UnnecessaryOwnedEmptyStrings));

clippy_lints/src/utils/conf.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,10 @@ define_Conf! {
389389
///
390390
/// Whether `dbg!` should be allowed in test functions
391391
(allow_dbg_in_tests: bool = false),
392+
/// Lint: PRINT_STDOUT, PRINT_STDERR.
393+
///
394+
/// Whether print macros (ex. `println!`) should be allowed in test functions
395+
(allow_print_in_tests: bool = false),
392396
/// Lint: RESULT_LARGE_ERR.
393397
///
394398
/// The maximum size of the `Err`-variant in a `Result` returned from a function

clippy_lints/src/write.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
22
use clippy_utils::macros::{root_macro_call_first_node, FormatArgsExpn, MacroCall};
33
use clippy_utils::source::{expand_past_previous_comma, snippet_opt};
4+
use clippy_utils::{is_in_cfg_test, is_in_test_function};
45
use rustc_ast::LitKind;
56
use rustc_errors::Applicability;
67
use rustc_hir::{Expr, ExprKind, HirIdMap, Impl, Item, ItemKind};
@@ -232,6 +233,16 @@ declare_clippy_lint! {
232233
#[derive(Default)]
233234
pub struct Write {
234235
in_debug_impl: bool,
236+
allow_print_in_tests: bool,
237+
}
238+
239+
impl Write {
240+
pub fn new(allow_print_in_tests: bool) -> Self {
241+
Self {
242+
allow_print_in_tests,
243+
..Default::default()
244+
}
245+
}
235246
}
236247

237248
impl_lint_pass!(Write => [
@@ -271,13 +282,15 @@ impl<'tcx> LateLintPass<'tcx> for Write {
271282
.as_ref()
272283
.map_or(false, |crate_name| crate_name == "build_script_build");
273284

285+
let allowed_in_tests = self.allow_print_in_tests
286+
&& (is_in_test_function(cx.tcx, expr.hir_id) || is_in_cfg_test(cx.tcx, expr.hir_id));
274287
match diag_name {
275-
sym::print_macro | sym::println_macro => {
288+
sym::print_macro | sym::println_macro if !allowed_in_tests => {
276289
if !is_build_script {
277290
span_lint(cx, PRINT_STDOUT, macro_call.span, &format!("use of `{name}!`"));
278291
}
279292
},
280-
sym::eprint_macro | sym::eprintln_macro => {
293+
sym::eprint_macro | sym::eprintln_macro if !allowed_in_tests => {
281294
span_lint(cx, PRINT_STDERR, macro_call.span, &format!("use of `{name}!`"));
282295
},
283296
sym::write_macro | sym::writeln_macro => {},

tests/ui-toml/print_macro/clippy.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
allow-print-in-tests = true
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// compile-flags: --test
2+
#![warn(clippy::print_stdout)]
3+
#![warn(clippy::print_stderr)]
4+
5+
fn foo(n: u32) {
6+
print!("{n}");
7+
eprint!("{n}");
8+
}
9+
10+
#[test]
11+
pub fn foo1() {
12+
print!("{}", 1);
13+
eprint!("{}", 1);
14+
}
15+
16+
#[cfg(test)]
17+
fn foo3() {
18+
print!("{}", 1);
19+
eprint!("{}", 1);
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: use of `print!`
2+
--> $DIR/print_macro.rs:6:5
3+
|
4+
LL | print!("{n}");
5+
| ^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::print-stdout` implied by `-D warnings`
8+
9+
error: use of `eprint!`
10+
--> $DIR/print_macro.rs:7:5
11+
|
12+
LL | eprint!("{n}");
13+
| ^^^^^^^^^^^^^^
14+
|
15+
= note: `-D clippy::print-stderr` implied by `-D warnings`
16+
17+
error: aborting due to 2 previous errors
18+

tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of
22
allow-dbg-in-tests
33
allow-expect-in-tests
4+
allow-print-in-tests
45
allow-unwrap-in-tests
56
allowed-scripts
67
arithmetic-side-effects-allowed

0 commit comments

Comments
 (0)