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