Allow explicit_write in tests (#15862)

Resolves rust-lang/rust-clippy#15780.

I didn't get any feedback on that issue. So I hope it is okay that I
charged ahead and addressed it.

changelog: Allow `explicit_write` in tests
This commit is contained in:
Jason Newcomb 2025-10-15 01:44:50 +00:00 committed by GitHub
commit d230acd9cf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 15 additions and 1 deletions

View file

@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::macros::{FormatArgsStorage, format_args_inputs_span};
use clippy_utils::res::MaybeResPath;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::{is_expn_of, sym};
use clippy_utils::{is_expn_of, is_in_test, sym};
use rustc_errors::Applicability;
use rustc_hir::def::Res;
use rustc_hir::{BindingMode, Block, BlockCheckMode, Expr, ExprKind, Node, PatKind, QPath, Stmt, StmtKind};
@ -72,6 +72,11 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitWrite {
return;
};
// Performing an explicit write in a test circumvent's libtest's capture of stdio and stdout.
if is_in_test(cx.tcx, expr.hir_id) {
return;
}
// ordering is important here, since `writeln!` uses `write!` internally
let calling_macro = if is_expn_of(write_call.span, sym::writeln).is_some() {
Some("writeln")

View file

@ -0,0 +1,9 @@
//@ check-pass
#![warn(clippy::explicit_write)]
#[test]
fn test() {
use std::io::Write;
writeln!(std::io::stderr(), "I am an explicit write.").unwrap();
eprintln!("I am not an explicit write.");
}

View file