@@ -49,7 +49,7 @@ pub struct DocFragment {
49
49
pub doc : Symbol ,
50
50
pub kind : DocFragmentKind ,
51
51
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
53
53
/// later on. So instead, we compute it and store it here.
54
54
pub from_expansion : bool ,
55
55
}
@@ -509,16 +509,20 @@ fn collect_link_data<'input, F: BrokenLinkCallback<'input>>(
509
509
display_text. map ( String :: into_boxed_str)
510
510
}
511
511
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.
513
514
pub 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
+ } ;
515
520
if first_fragment. span == DUMMY_SP {
516
521
return None ;
517
522
}
518
- let last_fragment = fragments. last ( ) . expect ( "no doc strings provided" ) ;
519
523
Some ( (
520
524
first_fragment. span . to ( last_fragment. span ) ,
521
- first_fragment . from_expansion || last_fragment . from_expansion ,
525
+ fragments . iter ( ) . any ( |frag| frag . from_expansion ) ,
522
526
) )
523
527
}
524
528
@@ -538,12 +542,16 @@ pub fn span_of_fragments(fragments: &[DocFragment]) -> Option<Span> {
538
542
/// This method will return `Some` only if one of the following is true:
539
543
///
540
544
/// - 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`.
542
547
/// - 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`.
544
552
///
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 .
547
555
pub fn source_span_for_markdown_range (
548
556
tcx : TyCtxt < ' _ > ,
549
557
markdown : & str ,
@@ -678,12 +686,13 @@ pub fn source_span_for_markdown_range_inner(
678
686
}
679
687
}
680
688
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
+ ) ) ;
682
694
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 ) ,
688
697
) )
689
698
}
0 commit comments