11use clippy_utils:: diagnostics:: { span_lint, span_lint_and_note} ;
22use 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} ;
64use if_chain:: if_chain;
75use itertools:: Itertools ;
86use rustc_ast:: ast:: { Async , AttrKind , Attribute , FnKind , FnRetTy , ItemKind } ;
@@ -197,7 +195,10 @@ pub struct DocMarkdown {
197195
198196impl DocMarkdown {
199197 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+ }
201202 }
202203}
203204
@@ -216,9 +217,7 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown {
216217 let headers = check_attrs ( cx, & self . valid_idents , attrs) ;
217218 match item. kind {
218219 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 ) ) {
222221 let body = cx. tcx . hir ( ) . body ( body_id) ;
223222 let mut fpu = FindPanicUnwrap {
224223 cx,
@@ -236,11 +235,11 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown {
236235 fpu. panic_span ,
237236 ) ;
238237 }
239- }
238+ } ,
240239 hir:: ItemKind :: Impl ( ref impl_) => {
241240 self . in_trait_impl = impl_. of_trait . is_some ( ) ;
242- }
243- _ => { }
241+ } ,
242+ _ => { } ,
244243 }
245244 }
246245
@@ -300,7 +299,12 @@ fn lint_for_missing_headers<'tcx>(
300299 return ; // Private functions do not require doc comments
301300 }
302301 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+ ) ;
304308 }
305309 if !headers. panics && panic_span. is_some ( ) {
306310 span_lint_and_note (
@@ -353,11 +357,7 @@ fn lint_for_missing_headers<'tcx>(
353357/// the spans but this function is inspired from the later.
354358#[ allow( clippy:: cast_possible_truncation) ]
355359#[ 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 ) > ) {
361361 // one-line comments lose their prefix
362362 if comment_kind == CommentKind :: Line {
363363 let mut doc = doc. to_owned ( ) ;
@@ -405,24 +405,23 @@ struct DocHeaders {
405405 panics : bool ,
406406}
407407
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 {
413409 let mut doc = String :: new ( ) ;
414410 let mut spans = vec ! [ ] ;
415411
416412 for attr in attrs {
417413 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 ) ;
420415 spans. extend_from_slice ( & current_spans) ;
421416 doc. push_str ( & comment) ;
422417 } else if attr. has_name ( sym:: doc) {
423418 // ignore mix of sugared and non-sugared doc
424419 // 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+ } ;
426425 }
427426 }
428427
@@ -434,7 +433,11 @@ fn check_attrs<'a>(
434433 }
435434
436435 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+ } ;
438441 }
439442
440443 let parser = pulldown_cmark:: Parser :: new ( & doc) . into_offset_iter ( ) ;
@@ -450,7 +453,7 @@ fn check_attrs<'a>(
450453 let mut previous = previous. to_string ( ) ;
451454 previous. push_str ( & current) ;
452455 Ok ( ( Text ( previous. into ( ) ) , previous_range) )
453- }
456+ } ,
454457 ( previous, current) => Err ( ( ( previous, previous_range) , ( current, current_range) ) ) ,
455458 }
456459 } ) ;
@@ -472,7 +475,11 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
472475 } ;
473476 use pulldown_cmark:: Tag :: { CodeBlock , Heading , Link } ;
474477
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+ } ;
476483 let mut in_code = false ;
477484 let mut in_link = None ;
478485 let mut in_heading = false ;
@@ -496,11 +503,11 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
496503 }
497504 }
498505 }
499- }
506+ } ,
500507 End ( CodeBlock ( _) ) => {
501508 in_code = false ;
502509 is_rust = false ;
503- }
510+ } ,
504511 Start ( Link ( _, url, _) ) => in_link = Some ( url) ,
505512 End ( Link ( ..) ) => in_link = None ,
506513 Start ( Heading ( _) ) => in_heading = true ,
@@ -534,7 +541,7 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
534541
535542 check_text ( cx, valid_idents, & text, span) ;
536543 }
537- }
544+ } ,
538545 }
539546 }
540547 headers
@@ -547,21 +554,19 @@ fn check_code(cx: &LateContext<'_>, text: &str, edition: Edition, span: Span) {
547554 let filename = FileName :: anon_source_code ( code) ;
548555
549556 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 ) ;
552558 let handler = Handler :: with_emitter ( false , None , box emitter) ;
553559 let sess = ParseSess :: with_span_handler ( handler, sm) ;
554560
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 ( ) ;
563566 }
564- } ;
567+ return false ;
568+ } ,
569+ } ;
565570
566571 let mut relevant_main_found = false ;
567572 loop {
@@ -573,9 +578,7 @@ fn check_code(cx: &LateContext<'_>, text: &str, edition: Edition, span: Span) {
573578 | ItemKind :: ExternCrate ( ..)
574579 | ItemKind :: ForeignMod ( ..) => return false ,
575580 // 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 => {
579582 let is_async = matches ! ( sig. header. asyncness, Async :: Yes { .. } ) ;
580583 let returns_nothing = match & sig. decl . output {
581584 FnRetTy :: Default ( ..) => true ,
@@ -590,16 +593,16 @@ fn check_code(cx: &LateContext<'_>, text: &str, edition: Edition, span: Span) {
590593 // This main function should not be linted, we're done
591594 return false ;
592595 }
593- }
596+ } ,
594597 // Another function was found; this case is ignored too
595598 ItemKind :: Fn ( ..) => return false ,
596- _ => { }
599+ _ => { } ,
597600 } ,
598601 Ok ( None ) => break ,
599602 Err ( mut e) => {
600603 e. cancel ( ) ;
601604 return false ;
602- }
605+ } ,
603606 }
604607 }
605608
@@ -719,9 +722,7 @@ impl<'a, 'tcx> Visitor<'tcx> for FindPanicUnwrap<'a, 'tcx> {
719722 }
720723
721724 // 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 ( ) {
725726 self . panic_span = Some ( expr. span ) ;
726727 }
727728
0 commit comments