@@ -49,7 +49,7 @@ pub struct DocFragment {
4949 pub doc : Symbol ,
5050 pub kind : DocFragmentKind ,
5151 pub indent : usize ,
52- /// Because we temper with the spans context, this information cannot be correctly retrieved
52+ /// Because we tamper with the spans context, this information cannot be correctly retrieved
5353 /// later on. So instead, we compute it and store it here.
5454 pub from_expansion : bool ,
5555}
@@ -509,16 +509,20 @@ fn collect_link_data<'input, F: BrokenLinkCallback<'input>>(
509509 display_text. map ( String :: into_boxed_str)
510510}
511511
512- /// Returns a span encompassing all the document fragments.
512+ /// Returns a tuple containing a span encompassing all the document fragments and a boolean that is
513+ /// `true` if any of the fragments are from a macro expansion.
513514pub fn span_of_fragments_with_expansion ( fragments : & [ DocFragment ] ) -> Option < ( Span , bool ) > {
514- let Some ( first_fragment) = fragments. first ( ) else { return None } ;
515+ let ( first_fragment, last_fragment) = match fragments {
516+ [ ] => return None ,
517+ [ first, .., last] => ( first, last) ,
518+ [ first] => ( first, first) ,
519+ } ;
515520 if first_fragment. span == DUMMY_SP {
516521 return None ;
517522 }
518- let last_fragment = fragments. last ( ) . expect ( "no doc strings provided" ) ;
519523 Some ( (
520524 first_fragment. span . to ( last_fragment. span ) ,
521- first_fragment . from_expansion || last_fragment . from_expansion ,
525+ fragments . iter ( ) . any ( |frag| frag . from_expansion ) ,
522526 ) )
523527}
524528
@@ -538,12 +542,16 @@ pub fn span_of_fragments(fragments: &[DocFragment]) -> Option<Span> {
538542/// This method will return `Some` only if one of the following is true:
539543///
540544/// - The doc is made entirely from sugared doc comments, which cannot contain escapes
541- /// - The doc is entirely from a single doc fragment with a string literal exactly equal to `markdown`.
545+ /// - The doc is entirely from a single doc fragment with a string literal exactly equal to
546+ /// `markdown`.
542547/// - The doc comes from `include_str!`
543- /// - The doc includes exactly one substring matching `markdown[md_range]` which is contained in a single doc fragment.
548+ /// - The doc includes exactly one substring matching `markdown[md_range]` which is contained in a
549+ /// single doc fragment.
550+ ///
551+ /// This function is defined in the compiler so it can be used by both `rustdoc` and `clippy`.
544552///
545- /// This function is defined in the compiler so it can be used by
546- /// both `rustdoc` and `clippy` .
553+ /// It returns a tuple containing a span encompassing all the document fragments and a boolean that
554+ /// is `true` if any of the *matched* fragments are from a macro expansion .
547555pub fn source_span_for_markdown_range (
548556 tcx : TyCtxt < ' _ > ,
549557 markdown : & str ,
@@ -678,12 +686,13 @@ pub fn source_span_for_markdown_range_inner(
678686 }
679687 }
680688
681- let ( span, from_expansion) = span_of_fragments_with_expansion ( fragments) ?;
689+ let ( span, _) = span_of_fragments_with_expansion ( fragments) ?;
690+ let src_span = span. from_inner ( InnerSpan :: new (
691+ md_range. start + start_bytes,
692+ md_range. end + start_bytes + end_bytes,
693+ ) ) ;
682694 Some ( (
683- span. from_inner ( InnerSpan :: new (
684- md_range. start + start_bytes,
685- md_range. end + start_bytes + end_bytes,
686- ) ) ,
687- from_expansion,
695+ src_span,
696+ fragments. iter ( ) . any ( |frag| frag. span . overlaps ( src_span) && frag. from_expansion ) ,
688697 ) )
689698}
0 commit comments