Skip to content

Commit fdcf1f2

Browse files
authored
Rollup merge of #149197 - lolbinarycat:rustdoc-bad-render-attr, r=GuillaumeGomez
validate usage of crate-level doc attributes fixes #149187 r? `@GuillaumeGomez`
2 parents 2b3b95b + 2ee6196 commit fdcf1f2

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
@@ -1123,6 +1123,28 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
11231123
true
11241124
}
11251125

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

13021329
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)