Skip to content

Commit 720f1f5

Browse files
committed
Allow explicit_write in tests
1 parent 70d5c8c commit 720f1f5

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

clippy_lints/src/explicit_write.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::macros::{FormatArgsStorage, format_args_inputs_span};
33
use clippy_utils::source::snippet_with_applicability;
4-
use clippy_utils::{is_expn_of, path_def_id, sym};
4+
use clippy_utils::{is_expn_of, is_in_test, path_def_id, sym};
55
use rustc_errors::Applicability;
66
use rustc_hir::def::Res;
77
use rustc_hir::{BindingMode, Block, BlockCheckMode, Expr, ExprKind, Node, PatKind, QPath, Stmt, StmtKind};
@@ -52,6 +52,9 @@ impl_lint_pass!(ExplicitWrite => [EXPLICIT_WRITE]);
5252

5353
impl<'tcx> LateLintPass<'tcx> for ExplicitWrite {
5454
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
55+
if is_in_test(cx.tcx, expr.hir_id) {
56+
return;
57+
}
5558
// match call to unwrap
5659
if let ExprKind::MethodCall(unwrap_fun, write_call, [], _) = expr.kind
5760
&& unwrap_fun.ident.name == sym::unwrap

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)