Skip to content

Commit d230acd

Browse files
authored
Allow explicit_write in tests (rust-lang#15862)
Resolves rust-lang#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
2 parents b26a1aa + 1bd8cad commit d230acd

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

clippy_lints/src/explicit_write.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::macros::{FormatArgsStorage, format_args_inputs_span};
33
use clippy_utils::res::MaybeResPath;
44
use clippy_utils::source::snippet_with_applicability;
5-
use clippy_utils::{is_expn_of, sym};
5+
use clippy_utils::{is_expn_of, is_in_test, sym};
66
use rustc_errors::Applicability;
77
use rustc_hir::def::Res;
88
use rustc_hir::{BindingMode, Block, BlockCheckMode, Expr, ExprKind, Node, PatKind, QPath, Stmt, StmtKind};
@@ -72,6 +72,11 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitWrite {
7272
return;
7373
};
7474

75+
// Performing an explicit write in a test circumvent's libtest's capture of stdio and stdout.
76+
if is_in_test(cx.tcx, expr.hir_id) {
77+
return;
78+
}
79+
7580
// ordering is important here, since `writeln!` uses `write!` internally
7681
let calling_macro = if is_expn_of(write_call.span, sym::writeln).is_some() {
7782
Some("writeln")

tests/ui/explicit_write_in_test.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//@ check-pass
2+
#![warn(clippy::explicit_write)]
3+
4+
#[test]
5+
fn test() {
6+
use std::io::Write;
7+
writeln!(std::io::stderr(), "I am an explicit write.").unwrap();
8+
eprintln!("I am not an explicit write.");
9+
}

tests/ui/explicit_write_in_test.stderr

Whitespace-only changes.

0 commit comments

Comments
 (0)