@@ -51,15 +51,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
51
51
if unwrap_args. len( ) > 0 ;
52
52
if let ExprKind :: MethodCall ( ref write_fun, _, ref write_args) =
53
53
unwrap_args[ 0 ] . node;
54
- // Obtain the string that should be printed
55
- if write_args. len( ) > 1 ;
56
- if let ExprKind :: Call ( _, ref output_args) = write_args[ 1 ] . node;
57
- if output_args. len( ) > 0 ;
58
- if let ExprKind :: AddrOf ( _, ref output_string_expr) = output_args[ 0 ] . node;
59
- if let ExprKind :: Array ( ref string_exprs) = output_string_expr. node;
60
- if string_exprs. len( ) > 0 ;
61
- if let ExprKind :: Lit ( ref lit) = string_exprs[ 0 ] . node;
62
- if let LitKind :: Str ( ref write_output, _) = lit. node;
63
54
if write_fun. ident. name == "write_fmt" ;
64
55
// match calls to std::io::stdout() / std::io::stderr ()
65
56
if write_args. len( ) > 0 ;
@@ -94,7 +85,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
94
85
// We need to remove the last trailing newline from the string because the
95
86
// underlying `fmt::write` function doesn't know wether `println!` or `print!` was
96
87
// used.
97
- let mut write_output: String = write_output . to_string ( ) ;
88
+ let mut write_output: String = write_output_string ( write_args ) . unwrap ( ) ;
98
89
if write_output. ends_with( '\n' ) {
99
90
write_output. truncate( write_output. len( ) - 1 )
100
91
}
@@ -125,3 +116,22 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
125
116
}
126
117
}
127
118
}
119
+
120
+ // Extract the output string from the given `write_args`.
121
+ fn write_output_string ( write_args : & HirVec < Expr > ) -> Option < String > {
122
+ if_chain ! {
123
+ // Obtain the string that should be printed
124
+ if write_args. len( ) > 1 ;
125
+ if let ExprKind :: Call ( _, ref output_args) = write_args[ 1 ] . node;
126
+ if output_args. len( ) > 0 ;
127
+ if let ExprKind :: AddrOf ( _, ref output_string_expr) = output_args[ 0 ] . node;
128
+ if let ExprKind :: Array ( ref string_exprs) = output_string_expr. node;
129
+ if string_exprs. len( ) > 0 ;
130
+ if let ExprKind :: Lit ( ref lit) = string_exprs[ 0 ] . node;
131
+ if let LitKind :: Str ( ref write_output, _) = lit. node;
132
+ then {
133
+ return Some ( write_output. to_string( ) )
134
+ }
135
+ }
136
+ None
137
+ }
0 commit comments