-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[rustdoc] Do not emit redundant_explicit_links lint if the doc comment comes from expansion #141648
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
[rustdoc] Do not emit redundant_explicit_links lint if the doc comment comes from expansion #141648
Conversation
…t comes from expansion
This comment has been minimized.
This comment has been minimized.
…ing from expansion
5e8b79b
to
ec9530a
Compare
This comment has been minimized.
This comment has been minimized.
Some changes occurred in src/tools/clippy cc @rust-lang/clippy |
A job failed! Check out the build log: (web) (plain) Click to see the possible cause of the failure (guessed by this bot)
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mostly nits about comments, but a few concerns about the actual logic as well.
@@ -45,6 +45,9 @@ pub struct DocFragment { | |||
pub doc: Symbol, | |||
pub kind: DocFragmentKind, | |||
pub indent: usize, | |||
/// Because we temper with the spans context, this information cannot be correctly retrieved |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Because we temper with the spans context, this information cannot be correctly retrieved | |
/// Because we tamper with the spans context, this information cannot be correctly retrieved |
@@ -497,16 +501,21 @@ fn collect_link_data<'input, F: BrokenLinkCallback<'input>>( | |||
} | |||
|
|||
/// Returns a span encompassing all the document fragments. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Returns a span encompassing all the document fragments. | |
/// Returns a span encompassing all the document fragments. | |
/// | |
/// Also returns a boolean that is `true` if the fragments are from a macro expansion. |
Boolean return values can sometimes be unclear which state represents what,
so I think it's safest to always document it.
let last_fragment = fragments.last().expect("no doc strings provided"); | ||
Some(( | ||
first_fragment.span.to(last_fragment.span), | ||
first_fragment.from_expansion || last_fragment.from_expansion, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
first_fragment.from_expansion || last_fragment.from_expansion, | |
fragments.iter().any(|frag| frag.from_expansion) |
In case only the middle fragment is from an expansion (we should also add a testcase for this).
return None; | ||
} | ||
let end = fragments.last().expect("no doc strings provided").span; | ||
Some(start.to(end)) | ||
let last_fragment = fragments.last().expect("no doc strings provided"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: this expect
is now unreachable, since any list with a first element must also have a last element.
@@ -531,7 +540,7 @@ pub fn source_span_for_markdown_range( | |||
markdown: &str, | |||
md_range: &Range<usize>, | |||
fragments: &[DocFragment], | |||
) -> Option<Span> { | |||
) -> Option<(Span, bool)> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should also document the meaning of the boolean return on this function too.
let (explicit_span, from_expansion) = source_span_for_markdown_range( | ||
cx.tcx, | ||
doc, | ||
&offset_explicit_range(doc, link_range.clone(), b'[', b']'), | ||
&item.attrs.doc_strings, | ||
)?; | ||
let display_span = source_span_for_markdown_range( | ||
if from_expansion { | ||
return None; | ||
} | ||
let (display_span, _) = source_span_for_markdown_range( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why are we checking from_expansion
in one of these cases but not the other? We should probably be checking both, since the original source_span_for_markdown_range
can fail, and it's possible both halves of the link are in different fragments, and only one the latter is macro generated.
alternatively, if we really cared about saving branches, we could probably get the display span by subtracting the explicit span from the total span, eliminating a call to source_span_for_markdown_range
.
also, the use of ?
here will cause false negatives if source_span_for_markdown_range
fails, which is quite common since it uses quite course heuristics.
For example, this produces no warnings:
/// [bar](bar)
#[doc = ""]
pub fn foo() {}
pub fn bar() {}
Technically this is an unrelated and preexisting bug, so I can spin this off into a separate issue and send a followup PR if you want, but it might be easier to just squash it now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, gonna handle this case here.
#[doc = mac1!()] | ||
pub struct Foo; | ||
|
||
// Should not lint. | ||
mac2!{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to also see some tests mixing macro generated fragments with non-generated fragments on the same item.
Fixes #141553.
The problem was that we change the context for the attributes in some cases to get better error output, preventing us to detect if the attribute comes from expansion. Most of the changes are about keeping track of the "does this span comes from expansion" information.
r? @Manishearth