validate usage of crate-level doc attributes
This commit is contained in:
parent
e22dab387f
commit
2ee6196078
5 changed files with 118 additions and 2 deletions
|
|
@ -126,6 +126,14 @@ passes_doc_alias_not_string_literal =
|
|||
passes_doc_alias_start_end =
|
||||
{$attr_str} cannot start or end with ' '
|
||||
|
||||
passes_doc_attr_expects_no_value =
|
||||
`doc({$attr_name})` does not accept a value
|
||||
.suggestion = use `doc({$attr_name})`
|
||||
|
||||
passes_doc_attr_expects_string =
|
||||
`doc({$attr_name})` expects a string value
|
||||
.suggestion = use `doc({$attr_name} = "...")`
|
||||
|
||||
passes_doc_attr_not_crate_level =
|
||||
`#![doc({$attr_name} = "...")]` isn't allowed as a crate-level attribute
|
||||
|
||||
|
|
|
|||
|
|
@ -1122,6 +1122,28 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
|
|||
true
|
||||
}
|
||||
|
||||
fn check_doc_attr_string_value(&self, meta: &MetaItemInner, hir_id: HirId) {
|
||||
if meta.value_str().is_none() {
|
||||
self.tcx.emit_node_span_lint(
|
||||
INVALID_DOC_ATTRIBUTES,
|
||||
hir_id,
|
||||
meta.span(),
|
||||
errors::DocAttrExpectsString { attr_name: meta.name().unwrap() },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn check_doc_attr_no_value(&self, meta: &MetaItemInner, hir_id: HirId) {
|
||||
if !meta.is_word() {
|
||||
self.tcx.emit_node_span_lint(
|
||||
INVALID_DOC_ATTRIBUTES,
|
||||
hir_id,
|
||||
meta.span(),
|
||||
errors::DocAttrExpectsNoValue { attr_name: meta.name().unwrap() },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks that `doc(test(...))` attribute contains only valid attributes and are at the right place.
|
||||
fn check_test_attr(
|
||||
&self,
|
||||
|
|
@ -1292,10 +1314,15 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
|
|||
| sym::html_logo_url
|
||||
| sym::html_playground_url
|
||||
| sym::issue_tracker_base_url
|
||||
| sym::html_root_url
|
||||
| sym::html_no_source,
|
||||
| sym::html_root_url,
|
||||
) => {
|
||||
self.check_attr_crate_level(attr_span, style, meta, hir_id);
|
||||
self.check_doc_attr_string_value(meta, hir_id);
|
||||
}
|
||||
|
||||
Some(sym::html_no_source) => {
|
||||
self.check_attr_crate_level(attr_span, style, meta, hir_id);
|
||||
self.check_doc_attr_no_value(meta, hir_id);
|
||||
}
|
||||
|
||||
Some(sym::auto_cfg) => {
|
||||
|
|
|
|||
|
|
@ -24,6 +24,18 @@ pub(crate) struct IncorrectDoNotRecommendLocation;
|
|||
#[diag(passes_incorrect_do_not_recommend_args)]
|
||||
pub(crate) struct DoNotRecommendDoesNotExpectArgs;
|
||||
|
||||
#[derive(LintDiagnostic)]
|
||||
#[diag(passes_doc_attr_expects_string)]
|
||||
pub(crate) struct DocAttrExpectsString {
|
||||
pub(crate) attr_name: Symbol,
|
||||
}
|
||||
|
||||
#[derive(LintDiagnostic)]
|
||||
#[diag(passes_doc_attr_expects_no_value)]
|
||||
pub(crate) struct DocAttrExpectsNoValue {
|
||||
pub(crate) attr_name: Symbol,
|
||||
}
|
||||
|
||||
#[derive(Diagnostic)]
|
||||
#[diag(passes_autodiff_attr)]
|
||||
pub(crate) struct AutoDiffAttr {
|
||||
|
|
|
|||
11
tests/rustdoc-ui/bad-render-options.rs
Normal file
11
tests/rustdoc-ui/bad-render-options.rs
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
// regression test for https://github.com/rust-lang/rust/issues/149187
|
||||
|
||||
#![doc(html_favicon_url)] //~ ERROR: `doc(html_favicon_url)` expects a string value [invalid_doc_attributes]
|
||||
#![doc(html_logo_url)] //~ ERROR: `doc(html_logo_url)` expects a string value [invalid_doc_attributes]
|
||||
#![doc(html_playground_url)] //~ ERROR: `doc(html_playground_url)` expects a string value [invalid_doc_attributes]
|
||||
#![doc(issue_tracker_base_url)] //~ ERROR expects a string value
|
||||
#![doc(html_favicon_url = 1)] //~ ERROR expects a string value
|
||||
#![doc(html_logo_url = 2)] //~ ERROR expects a string value
|
||||
#![doc(html_playground_url = 3)] //~ ERROR expects a string value
|
||||
#![doc(issue_tracker_base_url = 4)] //~ ERROR expects a string value
|
||||
#![doc(html_no_source = "asdf")] //~ ERROR `doc(html_no_source)` does not accept a value [invalid_doc_attributes]
|
||||
58
tests/rustdoc-ui/bad-render-options.stderr
Normal file
58
tests/rustdoc-ui/bad-render-options.stderr
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
error: `doc(html_favicon_url)` expects a string value
|
||||
--> $DIR/bad-render-options.rs:3:8
|
||||
|
|
||||
LL | #![doc(html_favicon_url)]
|
||||
| ^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: `#[deny(invalid_doc_attributes)]` on by default
|
||||
|
||||
error: `doc(html_logo_url)` expects a string value
|
||||
--> $DIR/bad-render-options.rs:4:8
|
||||
|
|
||||
LL | #![doc(html_logo_url)]
|
||||
| ^^^^^^^^^^^^^
|
||||
|
||||
error: `doc(html_playground_url)` expects a string value
|
||||
--> $DIR/bad-render-options.rs:5:8
|
||||
|
|
||||
LL | #![doc(html_playground_url)]
|
||||
| ^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: `doc(issue_tracker_base_url)` expects a string value
|
||||
--> $DIR/bad-render-options.rs:6:8
|
||||
|
|
||||
LL | #![doc(issue_tracker_base_url)]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: `doc(html_favicon_url)` expects a string value
|
||||
--> $DIR/bad-render-options.rs:7:8
|
||||
|
|
||||
LL | #![doc(html_favicon_url = 1)]
|
||||
| ^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: `doc(html_logo_url)` expects a string value
|
||||
--> $DIR/bad-render-options.rs:8:8
|
||||
|
|
||||
LL | #![doc(html_logo_url = 2)]
|
||||
| ^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: `doc(html_playground_url)` expects a string value
|
||||
--> $DIR/bad-render-options.rs:9:8
|
||||
|
|
||||
LL | #![doc(html_playground_url = 3)]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: `doc(issue_tracker_base_url)` expects a string value
|
||||
--> $DIR/bad-render-options.rs:10:8
|
||||
|
|
||||
LL | #![doc(issue_tracker_base_url = 4)]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: `doc(html_no_source)` does not accept a value
|
||||
--> $DIR/bad-render-options.rs:11:8
|
||||
|
|
||||
LL | #![doc(html_no_source = "asdf")]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: aborting due to 9 previous errors
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue