Forbid some characters to be used as doc alias

This commit is contained in:
Guillaume Gomez 2020-09-16 16:41:45 +02:00
parent 5fae56971d
commit 84cb71c6fa

View File

@ -252,23 +252,42 @@ impl CheckAttrVisitor<'tcx> {
}
}
fn doc_alias_str_error(&self, meta: &NestedMetaItem) {
self.tcx
.sess
.struct_span_err(
meta.span(),
"doc alias attribute expects a string: #[doc(alias = \"0\")]",
)
.emit();
}
fn check_doc_alias(&self, attr: &Attribute, hir_id: HirId, target: Target) -> bool {
if let Some(mi) = attr.meta() {
if let Some(list) = mi.meta_item_list() {
for meta in list {
if meta.has_name(sym::alias) {
if !meta.is_value_str()
|| meta
.value_str()
.map(|s| s.to_string())
.unwrap_or_else(String::new)
.is_empty()
if !meta.is_value_str() {
self.doc_alias_str_error(meta);
return false;
}
let doc_alias =
meta.value_str().map(|s| s.to_string()).unwrap_or_else(String::new);
if doc_alias.is_empty() {
self.doc_alias_str_error(meta);
return false;
}
if let Some(c) =
doc_alias.chars().find(|&c| c == '"' || c == '\'' || c.is_whitespace())
{
self.tcx
.sess
.struct_span_err(
meta.span(),
"doc alias attribute expects a string: #[doc(alias = \"0\")]",
&format!(
"{:?} character isn't allowed in `#[doc(alias = \"...\")]`",
c,
),
)
.emit();
return false;
@ -304,6 +323,7 @@ impl CheckAttrVisitor<'tcx> {
&format!("`#[doc(alias = \"...\")]` isn't allowed on {}", err),
)
.emit();
return false;
}
}
}