@@ -67,62 +67,39 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitWrite {
6767 // We need to remove the last trailing newline from the string because the
6868 // underlying `fmt::write` function doesn't know whether `println!` or `print!` was
6969 // used.
70+ let ( used, sugg_mac) = if let Some ( macro_name) = calling_macro {
71+ (
72+ format!( "{}!({}(), ...)" , macro_name, dest_name) ,
73+ macro_name. replace( "write" , "print" ) ,
74+ )
75+ } else {
76+ (
77+ format!( "{}().write_fmt(...)" , dest_name) ,
78+ "print" . into( ) ,
79+ )
80+ } ;
81+ let msg = format!( "use of `{}.unwrap()`" , used) ;
7082 if let [ write_output] = * format_args. format_string_symbols {
7183 let mut write_output = write_output. to_string( ) ;
7284 if write_output. ends_with( '\n' ) {
7385 write_output. pop( ) ;
7486 }
7587
76- if let Some ( macro_name) = calling_macro {
77- span_lint_and_sugg(
78- cx,
79- EXPLICIT_WRITE ,
80- expr. span,
81- & format!(
82- "use of `{}!({}(), ...).unwrap()`" ,
83- macro_name,
84- dest_name
85- ) ,
86- "try this" ,
87- format!( "{}{}!(\" {}\" )" , prefix, macro_name. replace( "write" , "print" ) , write_output. escape_default( ) ) ,
88- Applicability :: MachineApplicable
89- ) ;
90- } else {
91- span_lint_and_sugg(
92- cx,
93- EXPLICIT_WRITE ,
94- expr. span,
95- & format!( "use of `{}().write_fmt(...).unwrap()`" , dest_name) ,
96- "try this" ,
97- format!( "{}print!(\" {}\" )" , prefix, write_output. escape_default( ) ) ,
98- Applicability :: MachineApplicable
99- ) ;
100- }
88+ let sugg = format!( "{}{}!(\" {}\" )" , prefix, sugg_mac, write_output. escape_default( ) ) ;
89+ span_lint_and_sugg(
90+ cx,
91+ EXPLICIT_WRITE ,
92+ expr. span,
93+ & msg,
94+ "try this" ,
95+ sugg,
96+ Applicability :: MachineApplicable
97+ ) ;
10198 } else {
10299 // We don't have a proper suggestion
103- if let Some ( macro_name) = calling_macro {
104- span_lint(
105- cx,
106- EXPLICIT_WRITE ,
107- expr. span,
108- & format!(
109- "use of `{}!({}(), ...).unwrap()`. Consider using `{}{}!` instead" ,
110- macro_name,
111- dest_name,
112- prefix,
113- macro_name. replace( "write" , "print" )
114- )
115- ) ;
116- } else {
117- span_lint(
118- cx,
119- EXPLICIT_WRITE ,
120- expr. span,
121- & format!( "use of `{}().write_fmt(...).unwrap()`. Consider using `{}print!` instead" , dest_name, prefix) ,
122- ) ;
123- }
100+ let msg = format!( "{}. Consider using `{}{}!` instead" , msg, prefix, sugg_mac) ;
101+ span_lint( cx, EXPLICIT_WRITE , expr. span, & msg) ;
124102 }
125-
126103 }
127104 }
128105 }
0 commit comments