@@ -12,7 +12,7 @@ use crate::rustc::hir::*;
12
12
use crate :: rustc:: lint:: { LateContext , LateLintPass , LintArray , LintPass } ;
13
13
use crate :: rustc:: { declare_tool_lint, lint_array} ;
14
14
use if_chain:: if_chain;
15
- use crate :: utils:: { is_expn_of, match_def_path, resolve_node, span_lint_and_sugg} ;
15
+ use crate :: utils:: { is_expn_of, match_def_path, resolve_node, span_lint , span_lint_and_sugg} ;
16
16
use crate :: utils:: opt_def_id;
17
17
use crate :: syntax:: ast:: LitKind ;
18
18
@@ -86,35 +86,61 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
86
86
} ;
87
87
88
88
// We need to remove the last trailing newline from the string because the
89
- // underlying `fmt::write` function doesn't know wether `println!` or `print!` was
89
+ // underlying `fmt::write` function doesn't know whether `println!` or `print!` was
90
90
// used.
91
- let mut write_output: String = write_output_string( write_args) . unwrap( ) ;
92
- if write_output. ends_with( '\n' ) {
93
- write_output. truncate( write_output. len( ) - 1 )
94
- }
95
- if let Some ( macro_name) = calling_macro {
96
- span_lint_and_sugg(
97
- cx,
98
- EXPLICIT_WRITE ,
99
- expr. span,
100
- & format!(
101
- "use of `{}!({}(), ...).unwrap()`" ,
102
- macro_name,
103
- dest_name
104
- ) ,
105
- "try this" ,
106
- format!( "{}{}!(\" {}\" )" , prefix, macro_name. replace( "write" , "print" ) , write_output. escape_default( ) )
107
- ) ;
91
+ if let Some ( mut write_output) = write_output_string( write_args) {
92
+ if write_output. ends_with( '\n' ) {
93
+ write_output. pop( ) ;
94
+ }
95
+
96
+ if let Some ( macro_name) = calling_macro {
97
+ span_lint_and_sugg(
98
+ cx,
99
+ EXPLICIT_WRITE ,
100
+ expr. span,
101
+ & format!(
102
+ "use of `{}!({}(), ...).unwrap()`" ,
103
+ macro_name,
104
+ dest_name
105
+ ) ,
106
+ "try this" ,
107
+ format!( "{}{}!(\" {}\" )" , prefix, macro_name. replace( "write" , "print" ) , write_output. escape_default( ) )
108
+ ) ;
109
+ } else {
110
+ span_lint_and_sugg(
111
+ cx,
112
+ EXPLICIT_WRITE ,
113
+ expr. span,
114
+ & format!( "use of `{}().write_fmt(...).unwrap()`" , dest_name) ,
115
+ "try this" ,
116
+ format!( "{}print!(\" {}\" )" , prefix, write_output. escape_default( ) )
117
+ ) ;
118
+ }
108
119
} else {
109
- span_lint_and_sugg(
110
- cx,
111
- EXPLICIT_WRITE ,
112
- expr. span,
113
- & format!( "use of `{}().write_fmt(...).unwrap()`" , dest_name) ,
114
- "try this" ,
115
- format!( "{}print!(\" {}\" )" , prefix, write_output. escape_default( ) )
116
- ) ;
120
+ // We don't have a proper suggestion
121
+ if let Some ( macro_name) = calling_macro {
122
+ span_lint(
123
+ cx,
124
+ EXPLICIT_WRITE ,
125
+ expr. span,
126
+ & format!(
127
+ "use of `{}!({}(), ...).unwrap()`. Consider using `{}{}!` instead" ,
128
+ macro_name,
129
+ dest_name,
130
+ prefix,
131
+ macro_name. replace( "write" , "print" )
132
+ )
133
+ ) ;
134
+ } else {
135
+ span_lint(
136
+ cx,
137
+ EXPLICIT_WRITE ,
138
+ expr. span,
139
+ & format!( "use of `{}().write_fmt(...).unwrap()`. Consider using `{}print!` instead" , dest_name, prefix) ,
140
+ ) ;
141
+ }
117
142
}
143
+
118
144
}
119
145
}
120
146
}
0 commit comments