@@ -22,6 +22,8 @@ use std::path::Path;
22
22
use rustfix:: diagnostics:: Diagnostic ;
23
23
use failure:: { Error , ResultExt } ;
24
24
25
+ use diagnostics:: Message ;
26
+
25
27
mod cli;
26
28
mod lock;
27
29
mod diagnostics;
@@ -65,11 +67,11 @@ fn cargo_fix_rustc() -> Result<(), Error> {
65
67
// To that end we only actually try to fix things if it looks like we're
66
68
// compiling a Rust file and it *doesn't* have an absolute filename. That's
67
69
// not the best heuristic but matches what Cargo does today at least.
68
- let mut files_to_restore = HashMap :: new ( ) ;
70
+ let mut fixes = FixedCrate :: default ( ) ;
69
71
if let Some ( path) = filename {
70
72
if !Path :: new ( & path) . is_absolute ( ) {
71
73
trace ! ( "start rustfixing {:?}" , path) ;
72
- files_to_restore = rustfix_crate ( rustc. as_ref ( ) , & path) ?;
74
+ fixes = rustfix_crate ( rustc. as_ref ( ) , & path) ?;
73
75
}
74
76
}
75
77
@@ -84,9 +86,15 @@ fn cargo_fix_rustc() -> Result<(), Error> {
84
86
let mut cmd = Command :: new ( & rustc) ;
85
87
cmd. args ( env:: args ( ) . skip ( 1 ) ) ;
86
88
cmd. arg ( "--cap-lints=warn" ) ;
87
- if files_to_restore . len ( ) > 0 {
89
+ if fixes . original_files . len ( ) > 0 {
88
90
let output = cmd. output ( ) . context ( "failed to spawn rustc" ) ?;
89
91
92
+ if output. status . success ( ) {
93
+ for message in fixes. messages . drain ( ..) {
94
+ message. post ( ) ?;
95
+ }
96
+ }
97
+
90
98
// If we succeeded then we'll want to commit to the changes we made, if
91
99
// any. If stderr is empty then there's no need for the final exec at
92
100
// the end, we just bail out here.
@@ -98,7 +106,7 @@ fn cargo_fix_rustc() -> Result<(), Error> {
98
106
// user's code with our changes. Back out everything and fall through
99
107
// below to recompile again.
100
108
if !output. status . success ( ) {
101
- for ( k, v) in files_to_restore {
109
+ for ( k, v) in fixes . original_files {
102
110
File :: create ( & k)
103
111
. and_then ( |mut f| f. write_all ( v. as_bytes ( ) ) )
104
112
. with_context ( |_| format ! ( "failed to write file `{}`" , k) ) ?;
@@ -109,7 +117,13 @@ fn cargo_fix_rustc() -> Result<(), Error> {
109
117
exit_with ( cmd. status ( ) . context ( "failed to spawn rustc" ) ?) ;
110
118
}
111
119
112
- fn rustfix_crate ( rustc : & Path , filename : & str ) -> Result < HashMap < String , String > , Error > {
120
+ #[ derive( Default ) ]
121
+ struct FixedCrate {
122
+ messages : Vec < Message > ,
123
+ original_files : HashMap < String , String > ,
124
+ }
125
+
126
+ fn rustfix_crate ( rustc : & Path , filename : & str ) -> Result < FixedCrate , Error > {
113
127
// If not empty, filter by these lints
114
128
//
115
129
// TODO: Implement a way to specify this
@@ -143,7 +157,7 @@ fn rustfix_crate(rustc: &Path, filename: &str) -> Result<HashMap<String, String>
143
157
filename,
144
158
output. status. code( )
145
159
) ;
146
- return Ok ( HashMap :: new ( ) ) ;
160
+ return Ok ( Default :: default ( ) )
147
161
}
148
162
149
163
// Sift through the output of the compiler to look for JSON messages
@@ -194,7 +208,8 @@ fn rustfix_crate(rustc: &Path, filename: &str) -> Result<HashMap<String, String>
194
208
num_suggestion, filename
195
209
) ;
196
210
197
- let mut old_files = HashMap :: with_capacity ( file_map. len ( ) ) ;
211
+ let mut original_files = HashMap :: with_capacity ( file_map. len ( ) ) ;
212
+ let mut messages = Vec :: new ( ) ;
198
213
for ( file, suggestions) in file_map {
199
214
// Attempt to read the source code for this file. If this fails then
200
215
// that'd be pretty surprising, so log a message and otherwise keep
@@ -207,16 +222,19 @@ fn rustfix_crate(rustc: &Path, filename: &str) -> Result<HashMap<String, String>
207
222
let num_suggestions = suggestions. len ( ) ;
208
223
debug ! ( "applying {} fixes to {}" , num_suggestions, file) ;
209
224
210
- diagnostics :: Message :: fixing ( & file, num_suggestion ) . post ( ) ? ;
225
+ messages . push ( Message :: fixing ( & file, num_suggestions ) ) ;
211
226
212
227
let new_code = rustfix:: apply_suggestions ( & code, & suggestions) ?;
213
228
File :: create ( & file)
214
229
. and_then ( |mut f| f. write_all ( new_code. as_bytes ( ) ) )
215
230
. with_context ( |_| format ! ( "failed to write file `{}`" , file) ) ?;
216
- old_files . insert ( file, code) ;
231
+ original_files . insert ( file, code) ;
217
232
}
218
233
219
- Ok ( old_files)
234
+ Ok ( FixedCrate {
235
+ messages,
236
+ original_files,
237
+ } )
220
238
}
221
239
222
240
fn exit_with ( status : ExitStatus ) -> ! {
0 commit comments