Skip to content

Commit 827f24c

Browse files
committed
validate usage of crate-level doc attributes
1 parent e22dab3 commit 827f24c

File tree

5 files changed

+126
-1
lines changed

5 files changed

+126
-1
lines changed

compiler/rustc_passes/messages.ftl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ passes_diagnostic_diagnostic_on_unimplemented_only_for_traits =
102102
passes_diagnostic_item_first_defined =
103103
the diagnostic item is first defined here
104104
105+
passes_doc_attr_expects_no_value =
106+
`doc({$attr_name})` does not accept a value.
107+
.suggestion = use `doc({$attr_name})`
108+
109+
passes_doc_attr_expects_string =
110+
`doc({$attr_name})` expects a string value.
111+
.suggestion = use `doc({$attr_name} = "...")`
112+
105113
passes_doc_alias_bad_char =
106114
{$char_} character isn't allowed in {$attr_str}
107115

compiler/rustc_passes/src/check_attr.rs

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

1125+
fn check_doc_attr_string_value(
1126+
&self,
1127+
meta: &MetaItemInner,
1128+
hir_id: HirId,
1129+
) {
1130+
if meta.value_str().is_none() {
1131+
self.tcx.emit_node_span_lint(
1132+
INVALID_DOC_ATTRIBUTES,
1133+
hir_id,
1134+
meta.span(),
1135+
errors::DocAttrExpectsString{ attr_name: meta.name().unwrap() },
1136+
);
1137+
}
1138+
}
1139+
1140+
fn check_doc_attr_no_value(
1141+
&self,
1142+
meta: &MetaItemInner,
1143+
hir_id: HirId,
1144+
) {
1145+
if !meta.is_word() {
1146+
self.tcx.emit_node_span_lint(
1147+
INVALID_DOC_ATTRIBUTES,
1148+
hir_id,
1149+
meta.span(),
1150+
errors::DocAttrExpectsNoValue{ attr_name: meta.name().unwrap() },
1151+
);
1152+
}
1153+
}
1154+
11251155
/// Checks that `doc(test(...))` attribute contains only valid attributes and are at the right place.
11261156
fn check_test_attr(
11271157
&self,
@@ -1293,9 +1323,14 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
12931323
| sym::html_playground_url
12941324
| sym::issue_tracker_base_url
12951325
| sym::html_root_url
1296-
| sym::html_no_source,
12971326
) => {
12981327
self.check_attr_crate_level(attr_span, style, meta, hir_id);
1328+
self.check_doc_attr_string_value(meta, hir_id);
1329+
}
1330+
1331+
Some(sym::html_no_source) => {
1332+
self.check_attr_crate_level(attr_span, style, meta, hir_id);
1333+
self.check_doc_attr_no_value(meta, hir_id);
12991334
}
13001335

13011336
Some(sym::auto_cfg) => {

compiler/rustc_passes/src/errors.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ 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+
39+
2740
#[derive(Diagnostic)]
2841
#[diag(passes_autodiff_attr)]
2942
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)