17
17
//! `// ignore-tidy-CHECK-NAME`.
18
18
19
19
use crate :: walk:: { filter_dirs, walk} ;
20
- use regex:: Regex ;
20
+ use regex:: { Regex , RegexSet } ;
21
21
use std:: path:: Path ;
22
22
23
23
/// Error code markdown is restricted to 80 columns because they can be
@@ -225,6 +225,7 @@ pub fn check(path: &Path, bad: &mut bool) {
225
225
. chain ( PROBLEMATIC_CONSTS . iter ( ) . map ( |v| format ! ( "{:x}" , v) ) )
226
226
. chain ( PROBLEMATIC_CONSTS . iter ( ) . map ( |v| format ! ( "{:X}" , v) ) )
227
227
. collect ( ) ;
228
+ let problematic_regex = RegexSet :: new ( problematic_consts_strings. as_slice ( ) ) . unwrap ( ) ;
228
229
walk ( path, & mut skip, & mut |entry, contents| {
229
230
let file = entry. path ( ) ;
230
231
let filename = file. file_name ( ) . unwrap ( ) . to_string_lossy ( ) ;
@@ -281,7 +282,27 @@ pub fn check(path: &Path, bad: &mut bool) {
281
282
let mut trailing_new_lines = 0 ;
282
283
let mut lines = 0 ;
283
284
let mut last_safety_comment = false ;
285
+ let is_test = file. components ( ) . any ( |c| c. as_os_str ( ) == "tests" ) ;
286
+ // scanning the whole file for multiple needles at once is more efficient than
287
+ // executing lines times needles separate searches.
288
+ let any_problematic_line = problematic_regex. is_match ( contents) ;
284
289
for ( i, line) in contents. split ( '\n' ) . enumerate ( ) {
290
+ if line. is_empty ( ) {
291
+ if i == 0 {
292
+ leading_new_lines = true ;
293
+ }
294
+ trailing_new_lines += 1 ;
295
+ continue ;
296
+ } else {
297
+ trailing_new_lines = 0 ;
298
+ }
299
+
300
+ let trimmed = line. trim ( ) ;
301
+
302
+ if !trimmed. starts_with ( "//" ) {
303
+ lines += 1 ;
304
+ }
305
+
285
306
let mut err = |msg : & str | {
286
307
tidy_error ! ( bad, "{}:{}: {}" , file. display( ) , i + 1 , msg) ;
287
308
} ;
@@ -308,36 +329,38 @@ pub fn check(path: &Path, bad: &mut bool) {
308
329
suppressible_tidy_err ! ( err, skip_cr, "CR character" ) ;
309
330
}
310
331
if filename != "style.rs" {
311
- if line . contains ( "TODO" ) {
332
+ if trimmed . contains ( "TODO" ) {
312
333
err ( "TODO is deprecated; use FIXME" )
313
334
}
314
- if line . contains ( "//" ) && line . contains ( " XXX" ) {
335
+ if trimmed . contains ( "//" ) && trimmed . contains ( " XXX" ) {
315
336
err ( "XXX is deprecated; use FIXME" )
316
337
}
317
- for s in problematic_consts_strings. iter ( ) {
318
- if line. contains ( s) {
319
- err ( "Don't use magic numbers that spell things (consider 0x12345678)" ) ;
338
+ if any_problematic_line {
339
+ for s in problematic_consts_strings. iter ( ) {
340
+ if trimmed. contains ( s) {
341
+ err ( "Don't use magic numbers that spell things (consider 0x12345678)" ) ;
342
+ }
320
343
}
321
344
}
322
345
}
323
- let is_test = || file. components ( ) . any ( |c| c. as_os_str ( ) == "tests" ) ;
324
346
// for now we just check libcore
325
- if line . contains ( "unsafe {" ) && !line . trim ( ) . starts_with ( "//" ) && !last_safety_comment {
326
- if file. components ( ) . any ( |c| c. as_os_str ( ) == "core" ) && !is_test ( ) {
347
+ if trimmed . contains ( "unsafe {" ) && !trimmed . starts_with ( "//" ) && !last_safety_comment {
348
+ if file. components ( ) . any ( |c| c. as_os_str ( ) == "core" ) && !is_test {
327
349
suppressible_tidy_err ! ( err, skip_undocumented_unsafe, "undocumented unsafe" ) ;
328
350
}
329
351
}
330
- if line . contains ( "// SAFETY:" ) {
352
+ if trimmed . contains ( "// SAFETY:" ) {
331
353
last_safety_comment = true ;
332
- } else if line . trim ( ) . starts_with ( "//" ) || line . trim ( ) . is_empty ( ) {
354
+ } else if trimmed . starts_with ( "//" ) || trimmed . is_empty ( ) {
333
355
// keep previous value
334
356
} else {
335
357
last_safety_comment = false ;
336
358
}
337
359
if ( line. starts_with ( "// Copyright" )
338
360
|| line. starts_with ( "# Copyright" )
339
361
|| line. starts_with ( "Copyright" ) )
340
- && ( line. contains ( "Rust Developers" ) || line. contains ( "Rust Project Developers" ) )
362
+ && ( trimmed. contains ( "Rust Developers" )
363
+ || trimmed. contains ( "Rust Project Developers" ) )
341
364
{
342
365
suppressible_tidy_err ! (
343
366
err,
@@ -351,18 +374,6 @@ pub fn check(path: &Path, bad: &mut bool) {
351
374
if filename. ends_with ( ".cpp" ) && line. contains ( "llvm_unreachable" ) {
352
375
err ( LLVM_UNREACHABLE_INFO ) ;
353
376
}
354
- if line. is_empty ( ) {
355
- if i == 0 {
356
- leading_new_lines = true ;
357
- }
358
- trailing_new_lines += 1 ;
359
- } else {
360
- trailing_new_lines = 0 ;
361
- }
362
-
363
- if !line. trim ( ) . starts_with ( "//" ) {
364
- lines += 1 ;
365
- }
366
377
}
367
378
if leading_new_lines {
368
379
let mut err = |_| {
0 commit comments