11use clippy_utils:: diagnostics:: { span_lint, span_lint_and_note} ;
22use 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+ } ;
46use if_chain:: if_chain;
57use itertools:: Itertools ;
68use rustc_ast:: ast:: { Async , AttrKind , Attribute , FnKind , FnRetTy , ItemKind } ;
@@ -195,10 +197,7 @@ pub struct DocMarkdown {
195197
196198impl DocMarkdown {
197199 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 }
202201 }
203202}
204203
@@ -217,11 +216,13 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown {
217216 let headers = check_attrs ( cx, & self . valid_idents , attrs) ;
218217 match item. kind {
219218 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+ {
221222 let body = cx. tcx . hir ( ) . body ( body_id) ;
222223 let mut fpu = FindPanicUnwrap {
223224 cx,
224- typeck_results : cx. tcx . typeck ( item. def_id ) ,
225+ typeck_results : cx. tcx . typeck ( item. def_id . def_id ) ,
225226 panic_span : None ,
226227 } ;
227228 fpu. visit_expr ( & body. value ) ;
@@ -235,11 +236,11 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown {
235236 fpu. panic_span ,
236237 ) ;
237238 }
238- } ,
239+ }
239240 hir:: ItemKind :: Impl ( ref impl_) => {
240241 self . in_trait_impl = impl_. of_trait . is_some ( ) ;
241- } ,
242- _ => { } ,
242+ }
243+ _ => { }
243244 }
244245 }
245246
@@ -269,7 +270,7 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown {
269270 let body = cx. tcx . hir ( ) . body ( body_id) ;
270271 let mut fpu = FindPanicUnwrap {
271272 cx,
272- typeck_results : cx. tcx . typeck ( item. def_id ) ,
273+ typeck_results : cx. tcx . typeck ( item. def_id . def_id ) ,
273274 panic_span : None ,
274275 } ;
275276 fpu. visit_expr ( & body. value ) ;
@@ -299,12 +300,7 @@ fn lint_for_missing_headers<'tcx>(
299300 return ; // Private functions do not require doc comments
300301 }
301302 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" ) ;
308304 }
309305 if !headers. panics && panic_span. is_some ( ) {
310306 span_lint_and_note (
@@ -357,7 +353,11 @@ fn lint_for_missing_headers<'tcx>(
357353/// the spans but this function is inspired from the later.
358354#[ allow( clippy:: cast_possible_truncation) ]
359355#[ 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 ) > ) {
361361 // one-line comments lose their prefix
362362 if comment_kind == CommentKind :: Line {
363363 let mut doc = doc. to_owned ( ) ;
@@ -405,23 +405,24 @@ struct DocHeaders {
405405 panics : bool ,
406406}
407407
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 {
409413 let mut doc = String :: new ( ) ;
410414 let mut spans = vec ! [ ] ;
411415
412416 for attr in attrs {
413417 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 ) ;
415420 spans. extend_from_slice ( & current_spans) ;
416421 doc. push_str ( & comment) ;
417422 } else if attr. has_name ( sym:: doc) {
418423 // ignore mix of sugared and non-sugared doc
419424 // 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 } ;
425426 }
426427 }
427428
@@ -433,11 +434,7 @@ fn check_attrs<'a>(cx: &LateContext<'_>, valid_idents: &FxHashSet<String>, attrs
433434 }
434435
435436 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 } ;
441438 }
442439
443440 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
453450 let mut previous = previous. to_string ( ) ;
454451 previous. push_str ( & current) ;
455452 Ok ( ( Text ( previous. into ( ) ) , previous_range) )
456- } ,
453+ }
457454 ( previous, current) => Err ( ( ( previous, previous_range) , ( current, current_range) ) ) ,
458455 }
459456 } ) ;
@@ -475,11 +472,7 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
475472 } ;
476473 use pulldown_cmark:: Tag :: { CodeBlock , Heading , Link } ;
477474
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 } ;
483476 let mut in_code = false ;
484477 let mut in_link = None ;
485478 let mut in_heading = false ;
@@ -503,11 +496,11 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
503496 }
504497 }
505498 }
506- } ,
499+ }
507500 End ( CodeBlock ( _) ) => {
508501 in_code = false ;
509502 is_rust = false ;
510- } ,
503+ }
511504 Start ( Link ( _, url, _) ) => in_link = Some ( url) ,
512505 End ( Link ( ..) ) => in_link = None ,
513506 Start ( Heading ( _) ) => in_heading = true ,
@@ -541,7 +534,7 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
541534
542535 check_text ( cx, valid_idents, & text, span) ;
543536 }
544- } ,
537+ }
545538 }
546539 }
547540 headers
@@ -554,19 +547,21 @@ fn check_code(cx: &LateContext<'_>, text: &str, edition: Edition, span: Span) {
554547 let filename = FileName :: anon_source_code ( code) ;
555548
556549 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 ) ;
558552 let handler = Handler :: with_emitter ( false , None , box emitter) ;
559553 let sess = ParseSess :: with_span_handler ( handler, sm) ;
560554
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 ;
566563 }
567- return false ;
568- } ,
569- } ;
564+ } ;
570565
571566 let mut relevant_main_found = false ;
572567 loop {
@@ -578,7 +573,9 @@ fn check_code(cx: &LateContext<'_>, text: &str, edition: Edition, span: Span) {
578573 | ItemKind :: ExternCrate ( ..)
579574 | ItemKind :: ForeignMod ( ..) => return false ,
580575 // 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+ {
582579 let is_async = matches ! ( sig. header. asyncness, Async :: Yes { .. } ) ;
583580 let returns_nothing = match & sig. decl . output {
584581 FnRetTy :: Default ( ..) => true ,
@@ -593,16 +590,16 @@ fn check_code(cx: &LateContext<'_>, text: &str, edition: Edition, span: Span) {
593590 // This main function should not be linted, we're done
594591 return false ;
595592 }
596- } ,
593+ }
597594 // Another function was found; this case is ignored too
598595 ItemKind :: Fn ( ..) => return false ,
599- _ => { } ,
596+ _ => { }
600597 } ,
601598 Ok ( None ) => break ,
602599 Err ( mut e) => {
603600 e. cancel ( ) ;
604601 return false ;
605- } ,
602+ }
606603 }
607604 }
608605
@@ -722,7 +719,9 @@ impl<'a, 'tcx> Visitor<'tcx> for FindPanicUnwrap<'a, 'tcx> {
722719 }
723720
724721 // 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+ {
726725 self . panic_span = Some ( expr. span ) ;
727726 }
728727
0 commit comments