Skip to content

Commit 2ee6196

Browse files
committed
validate usage of crate-level doc attributes
1 parent e22dab3 commit 2ee6196

File tree

5 files changed

+118
-2
lines changed

5 files changed

+118
-2
lines changed

compiler/rustc_passes/messages.ftl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ passes_doc_alias_not_string_literal =
126126
passes_doc_alias_start_end =
127127
{$attr_str} cannot start or end with ' '
128128
129+
passes_doc_attr_expects_no_value =
130+
`doc({$attr_name})` does not accept a value
131+
.suggestion = use `doc({$attr_name})`
132+
133+
passes_doc_attr_expects_string =
134+
`doc({$attr_name})` expects a string value
135+
.suggestion = use `doc({$attr_name} = "...")`
136+
129137
passes_doc_attr_not_crate_level =
130138
`#![doc({$attr_name} = "...")]` isn't allowed as a crate-level attribute
131139

compiler/rustc_passes/src/check_attr.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,28 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
11221122
true
11231123
}
11241124

1125+
fn check_doc_attr_string_value(&self, meta: &MetaItemInner, hir_id: HirId) {
1126+
if meta.value_str().is_none() {
1127+
self.tcx.emit_node_span_lint(
1128+
INVALID_DOC_ATTRIBUTES,
1129+
hir_id,
1130+
meta.span(),
1131+
errors::DocAttrExpectsString { attr_name: meta.name().unwrap() },
1132+
);
1133+
}
1134+
}
1135+
1136+
fn check_doc_attr_no_value(&self, meta: &MetaItemInner, hir_id: HirId) {
1137+
if !meta.is_word() {
1138+
self.tcx.emit_node_span_lint(
1139+
INVALID_DOC_ATTRIBUTES,
1140+
hir_id,
1141+
meta.span(),
1142+
errors::DocAttrExpectsNoValue { attr_name: meta.name().unwrap() },
1143+
);
1144+
}
1145+
}
1146+
11251147
/// Checks that `doc(test(...))` attribute contains only valid attributes and are at the right place.
11261148
fn check_test_attr(
11271149
&self,
@@ -1292,10 +1314,15 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
12921314
| sym::html_logo_url
12931315
| sym::html_playground_url
12941316
| sym::issue_tracker_base_url
1295-
| sym::html_root_url
1296-
| sym::html_no_source,
1317+
| sym::html_root_url,
12971318
) => {
12981319
self.check_attr_crate_level(attr_span, style, meta, hir_id);
1320+
self.check_doc_attr_string_value(meta, hir_id);
1321+
}
1322+
1323+
Some(sym::html_no_source) => {
1324+
self.check_attr_crate_level(attr_span, style, meta, hir_id);
1325+
self.check_doc_attr_no_value(meta, hir_id);
12991326
}
13001327

13011328
Some(sym::auto_cfg) => {

compiler/rustc_passes/src/errors.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ pub(crate) struct IncorrectDoNotRecommendLocation;
2424
#[diag(passes_incorrect_do_not_recommend_args)]
2525
pub(crate) struct DoNotRecommendDoesNotExpectArgs;
2626

27+
#[derive(LintDiagnostic)]
28+
#[diag(passes_doc_attr_expects_string)]
29+
pub(crate) struct DocAttrExpectsString {
30+
pub(crate) attr_name: Symbol,
31+
}
32+
33+
#[derive(LintDiagnostic)]
34+
#[diag(passes_doc_attr_expects_no_value)]
35+
pub(crate) struct DocAttrExpectsNoValue {
36+
pub(crate) attr_name: Symbol,
37+
}
38+
2739
#[derive(Diagnostic)]
2840
#[diag(passes_autodiff_attr)]
2941
pub(crate) struct AutoDiffAttr {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// regression test for https://github.com/rust-lang/rust/issues/149187
2+
3+
#![doc(html_favicon_url)] //~ ERROR: `doc(html_favicon_url)` expects a string value [invalid_doc_attributes]
4+
#![doc(html_logo_url)] //~ ERROR: `doc(html_logo_url)` expects a string value [invalid_doc_attributes]
5+
#![doc(html_playground_url)] //~ ERROR: `doc(html_playground_url)` expects a string value [invalid_doc_attributes]
6+
#![doc(issue_tracker_base_url)] //~ ERROR expects a string value
7+
#![doc(html_favicon_url = 1)] //~ ERROR expects a string value
8+
#![doc(html_logo_url = 2)] //~ ERROR expects a string value
9+
#![doc(html_playground_url = 3)] //~ ERROR expects a string value
10+
#![doc(issue_tracker_base_url = 4)] //~ ERROR expects a string value
11+
#![doc(html_no_source = "asdf")] //~ ERROR `doc(html_no_source)` does not accept a value [invalid_doc_attributes]
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
error: `doc(html_favicon_url)` expects a string value
2+
--> $DIR/bad-render-options.rs:3:8
3+
|
4+
LL | #![doc(html_favicon_url)]
5+
| ^^^^^^^^^^^^^^^^
6+
|
7+
= note: `#[deny(invalid_doc_attributes)]` on by default
8+
9+
error: `doc(html_logo_url)` expects a string value
10+
--> $DIR/bad-render-options.rs:4:8
11+
|
12+
LL | #![doc(html_logo_url)]
13+
| ^^^^^^^^^^^^^
14+
15+
error: `doc(html_playground_url)` expects a string value
16+
--> $DIR/bad-render-options.rs:5:8
17+
|
18+
LL | #![doc(html_playground_url)]
19+
| ^^^^^^^^^^^^^^^^^^^
20+
21+
error: `doc(issue_tracker_base_url)` expects a string value
22+
--> $DIR/bad-render-options.rs:6:8
23+
|
24+
LL | #![doc(issue_tracker_base_url)]
25+
| ^^^^^^^^^^^^^^^^^^^^^^
26+
27+
error: `doc(html_favicon_url)` expects a string value
28+
--> $DIR/bad-render-options.rs:7:8
29+
|
30+
LL | #![doc(html_favicon_url = 1)]
31+
| ^^^^^^^^^^^^^^^^^^^^
32+
33+
error: `doc(html_logo_url)` expects a string value
34+
--> $DIR/bad-render-options.rs:8:8
35+
|
36+
LL | #![doc(html_logo_url = 2)]
37+
| ^^^^^^^^^^^^^^^^^
38+
39+
error: `doc(html_playground_url)` expects a string value
40+
--> $DIR/bad-render-options.rs:9:8
41+
|
42+
LL | #![doc(html_playground_url = 3)]
43+
| ^^^^^^^^^^^^^^^^^^^^^^^
44+
45+
error: `doc(issue_tracker_base_url)` expects a string value
46+
--> $DIR/bad-render-options.rs:10:8
47+
|
48+
LL | #![doc(issue_tracker_base_url = 4)]
49+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
50+
51+
error: `doc(html_no_source)` does not accept a value
52+
--> $DIR/bad-render-options.rs:11:8
53+
|
54+
LL | #![doc(html_no_source = "asdf")]
55+
| ^^^^^^^^^^^^^^^^^^^^^^^
56+
57+
error: aborting due to 9 previous errors
58+

0 commit comments

Comments
 (0)