Skip to content
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

Emit a warning on generic parameters with doc comments #91397

Merged
merged 1 commit into from
Dec 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,10 @@ impl EarlyLintPass for UnusedDocComment {
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) {
warn_if_doc(cx, expr.span, "expressions", &expr.attrs);
}

fn check_generic_param(&mut self, cx: &EarlyContext<'_>, param: &ast::GenericParam) {
warn_if_doc(cx, param.ident.span, "generic parameters", &param.attrs);
}
}

declare_lint! {
Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/lint/unused/unused-doc-comments-edge-cases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ fn doc_comment_on_expr(num: u8) -> bool {
num == 3
}

fn doc_comment_on_generic<#[doc = "x"] T>(val: T) {}
//~^ ERROR: unused doc comment
Comment on lines +29 to +30
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it's also worth adding tests for

fn doc_comment_on_generic<
    /// x
    T>(val: T) {}
//~^^ ERROR: unused doc comment

and

fn doc_comment_on_generic</** foo */ T>(val: T) {}
//~^ ERROR: unused doc comment

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, GitHub didn't show me that this was already approved.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to test those, anyway - rustc represents them both as #[doc] under the hood. There are plenty of other tests that would fail if that weren't the case.


fn main() {}
10 changes: 9 additions & 1 deletion src/test/ui/lint/unused/unused-doc-comments-edge-cases.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ LL | num == 3
|
= help: use `//` for a plain comment

error: unused doc comment
--> $DIR/unused-doc-comments-edge-cases.rs:29:27
|
LL | fn doc_comment_on_generic<#[doc = "x"] T>(val: T) {}
| ^^^^^^^^^^^^ - rustdoc does not generate documentation for generic parameters
|
= help: use `//` for a plain comment

error[E0308]: mismatched types
--> $DIR/unused-doc-comments-edge-cases.rs:14:9
|
Expand All @@ -55,7 +63,7 @@ help: you might have meant to return this value
LL | return true;
| ++++++ +

error: aborting due to 5 previous errors
error: aborting due to 6 previous errors

Some errors have detailed explanations: E0308, E0658.
For more information about an error, try `rustc --explain E0308`.