@@ -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
@@ -31,9 +30,10 @@ lazy_static! {
31
30
)
32
31
. unwrap( ) ;
33
32
static ref NL_ESCAPE_RE : Regex = Regex :: new( r#"\\\n\s*"# ) . unwrap( ) ;
34
- pub static ref DOCS_LINK : String = "https://rust-lang.github.io/rust-clippy/master/index.html" . to_string( ) ;
35
33
}
36
34
35
+ pub static DOCS_LINK : & str = "https://rust-lang.github.io/rust-clippy/master/index.html" ;
36
+
37
37
/// Lint data parsed from the Clippy source code.
38
38
#[ derive( Clone , PartialEq , Debug ) ]
39
39
pub struct Lint {
@@ -121,7 +121,7 @@ pub fn gen_changelog_lint_list(lints: Vec<Lint>) -> Vec<String> {
121
121
if l. is_internal ( ) {
122
122
None
123
123
} else {
124
- Some ( format ! ( "[`{}`]: {}#{}" , l. name, DOCS_LINK . clone ( ) , l. name) )
124
+ Some ( format ! ( "[`{}`]: {}#{}" , l. name, DOCS_LINK , l. name) )
125
125
}
126
126
} )
127
127
. collect ( )
@@ -172,9 +172,7 @@ pub fn gather_all() -> impl Iterator<Item = Lint> {
172
172
}
173
173
174
174
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 ( ) ;
175
+ let content = fs:: read_to_string ( dir_entry. path ( ) ) . unwrap ( ) ;
178
176
let mut filename = dir_entry. path ( ) . file_stem ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ;
179
177
// If the lints are stored in mod.rs, we get the module name from
180
178
// the containing directory:
@@ -209,7 +207,7 @@ fn lint_files() -> impl Iterator<Item = walkdir::DirEntry> {
209
207
let path = clippy_project_root ( ) . join ( "clippy_lints/src" ) ;
210
208
WalkDir :: new ( path)
211
209
. into_iter ( )
212
- . filter_map ( std :: result :: Result :: ok)
210
+ . filter_map ( Result :: ok)
213
211
. filter ( |f| f. path ( ) . extension ( ) == Some ( OsStr :: new ( "rs" ) ) )
214
212
}
215
213
@@ -225,7 +223,6 @@ pub struct FileChange {
225
223
/// `path` is the relative path to the file on which you want to perform the replacement.
226
224
///
227
225
/// See `replace_region_in_text` for documentation of the other options.
228
- #[ allow( clippy:: expect_fun_call) ]
229
226
pub fn replace_region_in_file < F > (
230
227
path : & Path ,
231
228
start : & str ,
@@ -235,22 +232,15 @@ pub fn replace_region_in_file<F>(
235
232
replacements : F ,
236
233
) -> FileChange
237
234
where
238
- F : Fn ( ) -> Vec < String > ,
235
+ F : FnOnce ( ) -> Vec < String > ,
239
236
{
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" ) ;
237
+ let contents = fs:: read_to_string ( path) . unwrap_or_else ( |e| panic ! ( "Cannot read from {}: {}" , path. display( ) , e) ) ;
245
238
let file_change = replace_region_in_text ( & contents, start, end, replace_start, replacements) ;
246
239
247
240
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" ) ;
241
+ if let Err ( e) = fs:: write ( path, file_change. new_lines . as_bytes ( ) ) {
242
+ panic ! ( "Cannot write to {}: {}" , path. display( ) , e) ;
243
+ }
254
244
}
255
245
file_change
256
246
}
@@ -273,31 +263,32 @@ where
273
263
///
274
264
/// ```
275
265
/// 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;
266
+ /// let result =
267
+ /// clippy_dev::replace_region_in_text(the_text, "replace_start", "replace_end", false, || {
268
+ /// vec!["a different".to_string(), "text".to_string()]
269
+ /// })
270
+ /// .new_lines;
280
271
/// assert_eq!("replace_start\na different\ntext\nreplace_end", result);
281
272
/// ```
282
273
pub fn replace_region_in_text < F > ( text : & str , start : & str , end : & str , replace_start : bool , replacements : F ) -> FileChange
283
274
where
284
- F : Fn ( ) -> Vec < String > ,
275
+ F : FnOnce ( ) -> Vec < String > ,
285
276
{
286
- let lines = text . lines ( ) ;
277
+ let replace_it = replacements ( ) ;
287
278
let mut in_old_region = false ;
288
279
let mut found = false ;
289
280
let mut new_lines = vec ! [ ] ;
290
281
let start = Regex :: new ( start) . unwrap ( ) ;
291
282
let end = Regex :: new ( end) . unwrap ( ) ;
292
283
293
- for line in lines . clone ( ) {
284
+ for line in text . lines ( ) {
294
285
if in_old_region {
295
- if end. is_match ( & line) {
286
+ if end. is_match ( line) {
296
287
in_old_region = false ;
297
- new_lines. extend ( replacements ( ) ) ;
288
+ new_lines. extend ( replace_it . clone ( ) ) ;
298
289
new_lines. push ( line. to_string ( ) ) ;
299
290
}
300
- } else if start. is_match ( & line) {
291
+ } else if start. is_match ( line) {
301
292
if !replace_start {
302
293
new_lines. push ( line. to_string ( ) ) ;
303
294
}
@@ -315,10 +306,12 @@ where
315
306
eprintln ! ( "error: regex `{:?}` not found. You may have to update it." , start) ;
316
307
}
317
308
318
- FileChange {
319
- changed : lines . ne ( new_lines . clone ( ) ) ,
320
- new_lines : new_lines . join ( " \n " ) ,
309
+ let mut new_lines = new_lines . join ( " \n " ) ;
310
+ if text . ends_with ( '\n' ) {
311
+ new_lines. push ( '\n' ) ;
321
312
}
313
+ let changed = new_lines != text;
314
+ FileChange { changed, new_lines }
322
315
}
323
316
324
317
/// Returns the path to the Clippy project directory
0 commit comments