1
1
use clippy_utils:: diagnostics:: { span_lint, span_lint_and_note} ;
2
2
use clippy_utils:: ty:: { implements_trait, is_type_diagnostic_item} ;
3
- use clippy_utils:: {
4
- is_entrypoint_fn, is_expn_of, match_panic_def_id, method_chain_args, return_ty,
5
- } ;
3
+ use clippy_utils:: { is_entrypoint_fn, is_expn_of, match_panic_def_id, method_chain_args, return_ty} ;
6
4
use if_chain:: if_chain;
7
5
use itertools:: Itertools ;
8
6
use rustc_ast:: ast:: { Async , AttrKind , Attribute , FnKind , FnRetTy , ItemKind } ;
@@ -197,7 +195,10 @@ pub struct DocMarkdown {
197
195
198
196
impl DocMarkdown {
199
197
pub fn new ( valid_idents : FxHashSet < String > ) -> Self {
200
- Self { valid_idents, in_trait_impl : false }
198
+ Self {
199
+ valid_idents,
200
+ in_trait_impl : false ,
201
+ }
201
202
}
202
203
}
203
204
@@ -216,9 +217,7 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown {
216
217
let headers = check_attrs ( cx, & self . valid_idents , attrs) ;
217
218
match item. kind {
218
219
hir:: ItemKind :: Fn ( ref sig, _, body_id) => {
219
- if !( is_entrypoint_fn ( cx, item. def_id . to_def_id ( ) )
220
- || in_external_macro ( cx. tcx . sess , item. span ) )
221
- {
220
+ if !( is_entrypoint_fn ( cx, item. def_id . to_def_id ( ) ) || in_external_macro ( cx. tcx . sess , item. span ) ) {
222
221
let body = cx. tcx . hir ( ) . body ( body_id) ;
223
222
let mut fpu = FindPanicUnwrap {
224
223
cx,
@@ -236,11 +235,11 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown {
236
235
fpu. panic_span ,
237
236
) ;
238
237
}
239
- }
238
+ } ,
240
239
hir:: ItemKind :: Impl ( ref impl_) => {
241
240
self . in_trait_impl = impl_. of_trait . is_some ( ) ;
242
- }
243
- _ => { }
241
+ } ,
242
+ _ => { } ,
244
243
}
245
244
}
246
245
@@ -300,7 +299,12 @@ fn lint_for_missing_headers<'tcx>(
300
299
return ; // Private functions do not require doc comments
301
300
}
302
301
if !headers. safety && sig. header . unsafety == hir:: Unsafety :: Unsafe {
303
- span_lint ( cx, MISSING_SAFETY_DOC , span, "unsafe function's docs miss `# Safety` section" ) ;
302
+ span_lint (
303
+ cx,
304
+ MISSING_SAFETY_DOC ,
305
+ span,
306
+ "unsafe function's docs miss `# Safety` section" ,
307
+ ) ;
304
308
}
305
309
if !headers. panics && panic_span. is_some ( ) {
306
310
span_lint_and_note (
@@ -353,11 +357,7 @@ fn lint_for_missing_headers<'tcx>(
353
357
/// the spans but this function is inspired from the later.
354
358
#[ allow( clippy:: cast_possible_truncation) ]
355
359
#[ must_use]
356
- pub fn strip_doc_comment_decoration (
357
- doc : & str ,
358
- comment_kind : CommentKind ,
359
- span : Span ,
360
- ) -> ( String , Vec < ( usize , Span ) > ) {
360
+ pub fn strip_doc_comment_decoration ( doc : & str , comment_kind : CommentKind , span : Span ) -> ( String , Vec < ( usize , Span ) > ) {
361
361
// one-line comments lose their prefix
362
362
if comment_kind == CommentKind :: Line {
363
363
let mut doc = doc. to_owned ( ) ;
@@ -405,24 +405,23 @@ struct DocHeaders {
405
405
panics : bool ,
406
406
}
407
407
408
- fn check_attrs < ' a > (
409
- cx : & LateContext < ' _ > ,
410
- valid_idents : & FxHashSet < String > ,
411
- attrs : & ' a [ Attribute ] ,
412
- ) -> DocHeaders {
408
+ fn check_attrs < ' a > ( cx : & LateContext < ' _ > , valid_idents : & FxHashSet < String > , attrs : & ' a [ Attribute ] ) -> DocHeaders {
413
409
let mut doc = String :: new ( ) ;
414
410
let mut spans = vec ! [ ] ;
415
411
416
412
for attr in attrs {
417
413
if let AttrKind :: DocComment ( comment_kind, comment) = attr. kind {
418
- let ( comment, current_spans) =
419
- strip_doc_comment_decoration ( & comment. as_str ( ) , comment_kind, attr. span ) ;
414
+ let ( comment, current_spans) = strip_doc_comment_decoration ( & comment. as_str ( ) , comment_kind, attr. span ) ;
420
415
spans. extend_from_slice ( & current_spans) ;
421
416
doc. push_str ( & comment) ;
422
417
} else if attr. has_name ( sym:: doc) {
423
418
// ignore mix of sugared and non-sugared doc
424
419
// don't trigger the safety or errors check
425
- return DocHeaders { safety : true , errors : true , panics : true } ;
420
+ return DocHeaders {
421
+ safety : true ,
422
+ errors : true ,
423
+ panics : true ,
424
+ } ;
426
425
}
427
426
}
428
427
@@ -434,7 +433,11 @@ fn check_attrs<'a>(
434
433
}
435
434
436
435
if doc. is_empty ( ) {
437
- return DocHeaders { safety : false , errors : false , panics : false } ;
436
+ return DocHeaders {
437
+ safety : false ,
438
+ errors : false ,
439
+ panics : false ,
440
+ } ;
438
441
}
439
442
440
443
let parser = pulldown_cmark:: Parser :: new ( & doc) . into_offset_iter ( ) ;
@@ -450,7 +453,7 @@ fn check_attrs<'a>(
450
453
let mut previous = previous. to_string ( ) ;
451
454
previous. push_str ( & current) ;
452
455
Ok ( ( Text ( previous. into ( ) ) , previous_range) )
453
- }
456
+ } ,
454
457
( previous, current) => Err ( ( ( previous, previous_range) , ( current, current_range) ) ) ,
455
458
}
456
459
} ) ;
@@ -472,7 +475,11 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
472
475
} ;
473
476
use pulldown_cmark:: Tag :: { CodeBlock , Heading , Link } ;
474
477
475
- let mut headers = DocHeaders { safety : false , errors : false , panics : false } ;
478
+ let mut headers = DocHeaders {
479
+ safety : false ,
480
+ errors : false ,
481
+ panics : false ,
482
+ } ;
476
483
let mut in_code = false ;
477
484
let mut in_link = None ;
478
485
let mut in_heading = false ;
@@ -496,11 +503,11 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
496
503
}
497
504
}
498
505
}
499
- }
506
+ } ,
500
507
End ( CodeBlock ( _) ) => {
501
508
in_code = false ;
502
509
is_rust = false ;
503
- }
510
+ } ,
504
511
Start ( Link ( _, url, _) ) => in_link = Some ( url) ,
505
512
End ( Link ( ..) ) => in_link = None ,
506
513
Start ( Heading ( _) ) => in_heading = true ,
@@ -534,7 +541,7 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
534
541
535
542
check_text ( cx, valid_idents, & text, span) ;
536
543
}
537
- }
544
+ } ,
538
545
}
539
546
}
540
547
headers
@@ -547,21 +554,19 @@ fn check_code(cx: &LateContext<'_>, text: &str, edition: Edition, span: Span) {
547
554
let filename = FileName :: anon_source_code ( code) ;
548
555
549
556
let sm = Lrc :: new ( SourceMap :: new ( FilePathMapping :: empty ( ) ) ) ;
550
- let emitter =
551
- EmitterWriter :: new ( box io:: sink ( ) , None , false , false , false , None , false ) ;
557
+ let emitter = EmitterWriter :: new ( box io:: sink ( ) , None , false , false , false , None , false ) ;
552
558
let handler = Handler :: with_emitter ( false , None , box emitter) ;
553
559
let sess = ParseSess :: with_span_handler ( handler, sm) ;
554
560
555
- let mut parser =
556
- match maybe_new_parser_from_source_str ( & sess, filename, code. into ( ) ) {
557
- Ok ( p) => p,
558
- Err ( errs) => {
559
- for mut err in errs {
560
- err. cancel ( ) ;
561
- }
562
- return false ;
561
+ let mut parser = match maybe_new_parser_from_source_str ( & sess, filename, code. into ( ) ) {
562
+ Ok ( p) => p,
563
+ Err ( errs) => {
564
+ for mut err in errs {
565
+ err. cancel ( ) ;
563
566
}
564
- } ;
567
+ return false ;
568
+ } ,
569
+ } ;
565
570
566
571
let mut relevant_main_found = false ;
567
572
loop {
@@ -573,9 +578,7 @@ fn check_code(cx: &LateContext<'_>, text: &str, edition: Edition, span: Span) {
573
578
| ItemKind :: ExternCrate ( ..)
574
579
| ItemKind :: ForeignMod ( ..) => return false ,
575
580
// We found a main function ...
576
- ItemKind :: Fn ( box FnKind ( _, sig, _, Some ( block) ) )
577
- if item. ident . name == sym:: main =>
578
- {
581
+ ItemKind :: Fn ( box FnKind ( _, sig, _, Some ( block) ) ) if item. ident . name == sym:: main => {
579
582
let is_async = matches ! ( sig. header. asyncness, Async :: Yes { .. } ) ;
580
583
let returns_nothing = match & sig. decl . output {
581
584
FnRetTy :: Default ( ..) => true ,
@@ -590,16 +593,16 @@ fn check_code(cx: &LateContext<'_>, text: &str, edition: Edition, span: Span) {
590
593
// This main function should not be linted, we're done
591
594
return false ;
592
595
}
593
- }
596
+ } ,
594
597
// Another function was found; this case is ignored too
595
598
ItemKind :: Fn ( ..) => return false ,
596
- _ => { }
599
+ _ => { } ,
597
600
} ,
598
601
Ok ( None ) => break ,
599
602
Err ( mut e) => {
600
603
e. cancel ( ) ;
601
604
return false ;
602
- }
605
+ } ,
603
606
}
604
607
}
605
608
@@ -719,9 +722,7 @@ impl<'a, 'tcx> Visitor<'tcx> for FindPanicUnwrap<'a, 'tcx> {
719
722
}
720
723
721
724
// check for `assert_eq` or `assert_ne`
722
- if is_expn_of ( expr. span , "assert_eq" ) . is_some ( )
723
- || is_expn_of ( expr. span , "assert_ne" ) . is_some ( )
724
- {
725
+ if is_expn_of ( expr. span , "assert_eq" ) . is_some ( ) || is_expn_of ( expr. span , "assert_ne" ) . is_some ( ) {
725
726
self . panic_span = Some ( expr. span ) ;
726
727
}
727
728
0 commit comments