diagnostics: emit unused_doc_comments on fn nest#158514
Conversation
When a doc comment is nested within an item:
fn foo() {
/// doc comment
fn bar() {}
}
When that happens, emit a warning, because they aren't rendered, even
with --document-private-items
warning: unused doc comment
--> src/lib.rs:3:3
|
3 | /// nested fn
| ^^^^^^^^^^^^^
4 | pub fn bar() {
| -------------- rustdoc does not generate documentation for items defined in functions
|
= help: use `//` for a plain comment
= note: `#[warn(unused_doc_comments)]` (part of `#[warn(unused)]`) on by default
|
cc @tgross35 |
|
r? @jackh726 rustbot has assigned @jackh726. Use Why was this reviewer chosen?The reviewer was selected based on:
|
|
r? rustdoc |
This comment has been minimized.
This comment has been minimized.
fe51138 to
9d780f5
Compare
This comment has been minimized.
This comment has been minimized.
9d780f5 to
7a91d72
Compare
This comment has been minimized.
This comment has been minimized.
| } | ||
|
|
||
| fn main() {} | ||
| fn main() { |
There was a problem hiding this comment.
Please add a test for an impl inside a block to ensure it's not emitted for this case
|
Reminder, once the PR becomes ready for a review, use |
7a91d72 to
9ff4703
Compare
There was a problem hiding this comment.
The same holds for const items:
pub const X: () = { /** X */ pub fn x() {} };const _: () = { /** X */ pub fn x() {} };Ideally we would lint here, too, wouldn't we?
There was a problem hiding this comment.
Or rather anonymous constants in general:
type X = [(); { /** X */ pub fn x() {} 0 }];There was a problem hiding this comment.
but be careful about impls where we shouldn't fire even if nested in an anonymous constant / constant item:
const _: () = { /** X */ impl Trait for () {} };
pub trait Trait {}Above, the doc comment does get rendered.
| fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) { | ||
| if let ast::ItemKind::ForeignMod(_) = item.kind { | ||
| warn_if_doc(cx, item.span, "extern blocks", &item.attrs); | ||
| } else if self.in_fn != 0 { |
There was a problem hiding this comment.
I believe this introduces false positives with impl blocks. Does your PR now warn on the following?
fn f() { /** X */ impl Trait for () {} }
pub trait Trait {}Above, the doc comment does get rendered.
This comment has been minimized.
This comment has been minimized.
9ff4703 to
93b8b3d
Compare
|
I don't think we should warn here. These doc comments are still useful: they are rendered by RA when hovering a call site. IOW they are pretty much exactly like a doc comment for a private item, and we don't warn against those either. |
|
The job Click to see the possible cause of the failure (guessed by this bot) |
When a doc comment is nested within an item:
When that happens, emit a warning, because they aren't rendered, even with
--document-private-itemsFixes #158513