Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion clippy_lints/src/explicit_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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")
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/explicit_write_in_test.rs
Original file line number Diff line number Diff line change
@@ -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.");
}
Empty file.