@@ -6,7 +6,6 @@ use regex::Regex;
6
6
use std:: collections:: HashMap ;
7
7
use std:: ffi:: OsStr ;
8
8
use std:: fs;
9
- use std:: io:: prelude:: * ;
10
9
use std:: path:: { Path , PathBuf } ;
11
10
use walkdir:: WalkDir ;
12
11
@@ -172,9 +171,7 @@ pub fn gather_all() -> impl Iterator<Item = Lint> {
172
171
}
173
172
174
173
fn gather_from_file ( dir_entry : & walkdir:: DirEntry ) -> impl Iterator < Item = Lint > {
175
- let mut file = fs:: File :: open ( dir_entry. path ( ) ) . unwrap ( ) ;
176
- let mut content = String :: new ( ) ;
177
- file. read_to_string ( & mut content) . unwrap ( ) ;
174
+ let content = fs:: read_to_string ( dir_entry. path ( ) ) . unwrap ( ) ;
178
175
let mut filename = dir_entry. path ( ) . file_stem ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ;
179
176
// If the lints are stored in mod.rs, we get the module name from
180
177
// the containing directory:
@@ -209,7 +206,7 @@ fn lint_files() -> impl Iterator<Item = walkdir::DirEntry> {
209
206
let path = clippy_project_root ( ) . join ( "clippy_lints/src" ) ;
210
207
WalkDir :: new ( path)
211
208
. into_iter ( )
212
- . filter_map ( std :: result :: Result :: ok)
209
+ . filter_map ( Result :: ok)
213
210
. filter ( |f| f. path ( ) . extension ( ) == Some ( OsStr :: new ( "rs" ) ) )
214
211
}
215
212
@@ -225,7 +222,6 @@ pub struct FileChange {
225
222
/// `path` is the relative path to the file on which you want to perform the replacement.
226
223
///
227
224
/// See `replace_region_in_text` for documentation of the other options.
228
- #[ allow( clippy:: expect_fun_call) ]
229
225
pub fn replace_region_in_file < F > (
230
226
path : & Path ,
231
227
start : & str ,
@@ -235,22 +231,15 @@ pub fn replace_region_in_file<F>(
235
231
replacements : F ,
236
232
) -> FileChange
237
233
where
238
- F : Fn ( ) -> Vec < String > ,
234
+ F : FnOnce ( ) -> Vec < String > ,
239
235
{
240
- let path = clippy_project_root ( ) . join ( path) ;
241
- let mut f = fs:: File :: open ( & path) . expect ( & format ! ( "File not found: {}" , path. to_string_lossy( ) ) ) ;
242
- let mut contents = String :: new ( ) ;
243
- f. read_to_string ( & mut contents)
244
- . expect ( "Something went wrong reading the file" ) ;
236
+ let contents = fs:: read_to_string ( path) . unwrap_or_else ( |e| panic ! ( "Cannot read from {}: {}" , path. display( ) , e) ) ;
245
237
let file_change = replace_region_in_text ( & contents, start, end, replace_start, replacements) ;
246
238
247
239
if write_back {
248
- let mut f = fs:: File :: create ( & path) . expect ( & format ! ( "File not found: {}" , path. to_string_lossy( ) ) ) ;
249
- f. write_all ( file_change. new_lines . as_bytes ( ) )
250
- . expect ( "Unable to write file" ) ;
251
- // Ensure we write the changes with a trailing newline so that
252
- // the file has the proper line endings.
253
- f. write_all ( b"\n " ) . expect ( "Unable to write file" ) ;
240
+ if let Err ( e) = fs:: write ( path, file_change. new_lines . as_bytes ( ) ) {
241
+ panic ! ( "Cannot write to {}: {}" , path. display( ) , e) ;
242
+ }
254
243
}
255
244
file_change
256
245
}
@@ -273,31 +262,32 @@ where
273
262
///
274
263
/// ```
275
264
/// let the_text = "replace_start\nsome text\nthat will be replaced\nreplace_end";
276
- /// let result = clippy_dev::replace_region_in_text(the_text, r#"replace_start"#, r#"replace_end"#, false, || {
277
- /// vec!["a different".to_string(), "text".to_string()]
278
- /// })
279
- /// .new_lines;
265
+ /// let result =
266
+ /// clippy_dev::replace_region_in_text(the_text, "replace_start", "replace_end", false, || {
267
+ /// vec!["a different".to_string(), "text".to_string()]
268
+ /// })
269
+ /// .new_lines;
280
270
/// assert_eq!("replace_start\na different\ntext\nreplace_end", result);
281
271
/// ```
282
272
pub fn replace_region_in_text < F > ( text : & str , start : & str , end : & str , replace_start : bool , replacements : F ) -> FileChange
283
273
where
284
- F : Fn ( ) -> Vec < String > ,
274
+ F : FnOnce ( ) -> Vec < String > ,
285
275
{
286
- let lines = text . lines ( ) ;
276
+ let replace_it = replacements ( ) ;
287
277
let mut in_old_region = false ;
288
278
let mut found = false ;
289
279
let mut new_lines = vec ! [ ] ;
290
280
let start = Regex :: new ( start) . unwrap ( ) ;
291
281
let end = Regex :: new ( end) . unwrap ( ) ;
292
282
293
- for line in lines . clone ( ) {
283
+ for line in text . lines ( ) {
294
284
if in_old_region {
295
- if end. is_match ( & line) {
285
+ if end. is_match ( line) {
296
286
in_old_region = false ;
297
- new_lines. extend ( replacements ( ) ) ;
287
+ new_lines. extend ( replace_it . clone ( ) ) ;
298
288
new_lines. push ( line. to_string ( ) ) ;
299
289
}
300
- } else if start. is_match ( & line) {
290
+ } else if start. is_match ( line) {
301
291
if !replace_start {
302
292
new_lines. push ( line. to_string ( ) ) ;
303
293
}
@@ -315,10 +305,12 @@ where
315
305
eprintln ! ( "error: regex `{:?}` not found. You may have to update it." , start) ;
316
306
}
317
307
318
- FileChange {
319
- changed : lines . ne ( new_lines . clone ( ) ) ,
320
- new_lines : new_lines . join ( " \n " ) ,
308
+ let mut new_lines = new_lines . join ( " \n " ) ;
309
+ if text . ends_with ( '\n' ) {
310
+ new_lines. push ( '\n' ) ;
321
311
}
312
+ let changed = new_lines != text;
313
+ FileChange { changed, new_lines }
322
314
}
323
315
324
316
/// Returns the path to the Clippy project directory
0 commit comments