11use clippy_utils:: diagnostics:: span_lint_and_sugg;
22use clippy_utils:: macros:: { find_format_args, format_args_inputs_span} ;
33use clippy_utils:: source:: snippet_with_applicability;
4- use clippy_utils:: { is_expn_of, match_function_call , paths } ;
4+ use clippy_utils:: { is_expn_of, path_def_id } ;
55use if_chain:: if_chain;
66use rustc_errors:: Applicability ;
77use rustc_hir:: def:: Res ;
@@ -47,18 +47,19 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitWrite {
4747 if let ExprKind :: MethodCall ( unwrap_fun, write_call, [ ] , _) = expr. kind
4848 && unwrap_fun. ident . name == sym:: unwrap
4949 // match call to write_fmt
50- && let ExprKind :: MethodCall ( write_fun, write_recv, [ write_arg] , _) = look_in_block ( cx, & write_call. kind )
50+ && let ExprKind :: MethodCall ( write_fun, write_recv, [ write_arg] , _) = * look_in_block ( cx, & write_call. kind )
51+ && let ExprKind :: Call ( write_recv_path, _) = write_recv. kind
5152 && write_fun. ident . name == sym ! ( write_fmt)
52- // match calls to std::io::stdout() / std::io::stderr ()
53- && let Some ( dest_name) = if match_function_call ( cx, write_recv, & paths:: STDOUT ) . is_some ( ) {
54- Some ( "stdout" )
55- } else if match_function_call ( cx, write_recv, & paths:: STDERR ) . is_some ( ) {
56- Some ( "stderr" )
57- } else {
58- None
59- }
60- && let Some ( format_args) = find_format_args ( cx, write_arg, ExpnId :: root ( ) )
53+ && let Some ( def_id) = path_def_id ( cx, write_recv_path)
6154 {
55+ // match calls to std::io::stdout() / std::io::stderr ()
56+ let ( dest_name, prefix) = match cx. tcx . get_diagnostic_name ( def_id) {
57+ Some ( sym:: io_stdout) => ( "stdout" , "" ) ,
58+ Some ( sym:: io_stderr) => ( "stderr" , "e" ) ,
59+ _ => return ,
60+ } ;
61+ let Some ( format_args) = find_format_args ( cx, write_arg, ExpnId :: root ( ) ) else { return ; } ;
62+
6263 // ordering is important here, since `writeln!` uses `write!` internally
6364 let calling_macro = if is_expn_of ( write_call. span , "writeln" ) . is_some ( ) {
6465 Some ( "writeln" )
@@ -67,11 +68,6 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitWrite {
6768 } else {
6869 None
6970 } ;
70- let prefix = if dest_name == "stderr" {
71- "e"
72- } else {
73- ""
74- } ;
7571
7672 // We need to remove the last trailing newline from the string because the
7773 // underlying `fmt::write` function doesn't know whether `println!` or `print!` was
0 commit comments