1
1
use clippy_utils:: diagnostics:: span_lint_and_sugg;
2
2
use clippy_utils:: macros:: { find_format_args, format_args_inputs_span} ;
3
3
use 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 } ;
5
5
use if_chain:: if_chain;
6
6
use rustc_errors:: Applicability ;
7
7
use rustc_hir:: def:: Res ;
@@ -47,18 +47,19 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitWrite {
47
47
if let ExprKind :: MethodCall ( unwrap_fun, write_call, [ ] , _) = expr. kind
48
48
&& unwrap_fun. ident . name == sym:: unwrap
49
49
// 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
51
52
&& 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)
61
54
{
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
+
62
63
// ordering is important here, since `writeln!` uses `write!` internally
63
64
let calling_macro = if is_expn_of ( write_call. span , "writeln" ) . is_some ( ) {
64
65
Some ( "writeln" )
@@ -67,11 +68,6 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitWrite {
67
68
} else {
68
69
None
69
70
} ;
70
- let prefix = if dest_name == "stderr" {
71
- "e"
72
- } else {
73
- ""
74
- } ;
75
71
76
72
// We need to remove the last trailing newline from the string because the
77
73
// underlying `fmt::write` function doesn't know whether `println!` or `print!` was
0 commit comments