1
Fork 0

Improve code by removing similar function calls and using loops instead for collecting iterators

This commit is contained in:
Guillaume Gomez 2021-03-08 17:30:22 +01:00
parent 2069d3e13b
commit 1d26e6b632
2 changed files with 49 additions and 76 deletions

View file

@ -398,47 +398,42 @@ impl CheckAttrVisitor<'tcx> {
target: Target, target: Target,
is_list: bool, is_list: bool,
) -> bool { ) -> bool {
let tcx = self.tcx;
let err_fn = move |span: Span, msg: &str| {
tcx.sess.span_err(
span,
&format!(
"`#[doc(alias{})]` {}",
if is_list { "(\"...\")" } else { " = \"...\"" },
msg,
),
);
false
};
if doc_alias.is_empty() { if doc_alias.is_empty() {
self.tcx return err_fn(
.sess meta.name_value_literal_span().unwrap_or_else(|| meta.span()),
.struct_span_err( "attribute cannot have empty value",
meta.name_value_literal_span().unwrap_or_else(|| meta.span()), );
&format!(
"`#[doc(alias{})]` attribute cannot have empty value",
if is_list { "(\"...\")" } else { " = \"...\"" },
),
)
.emit();
return false;
} }
if let Some(c) = if let Some(c) =
doc_alias.chars().find(|&c| c == '"' || c == '\'' || (c.is_whitespace() && c != ' ')) doc_alias.chars().find(|&c| c == '"' || c == '\'' || (c.is_whitespace() && c != ' '))
{ {
self.tcx self.tcx.sess.span_err(
.sess meta.name_value_literal_span().unwrap_or_else(|| meta.span()),
.struct_span_err( &format!(
meta.name_value_literal_span().unwrap_or_else(|| meta.span()), "{:?} character isn't allowed in `#[doc(alias{})]`",
&format!( c,
"{:?} character isn't allowed in `#[doc(alias{})]`", if is_list { "(\"...\")" } else { " = \"...\"" },
c, ),
if is_list { "(\"...\")" } else { " = \"...\"" }, );
),
)
.emit();
return false; return false;
} }
if doc_alias.starts_with(' ') || doc_alias.ends_with(' ') { if doc_alias.starts_with(' ') || doc_alias.ends_with(' ') {
self.tcx return err_fn(
.sess meta.name_value_literal_span().unwrap_or_else(|| meta.span()),
.struct_span_err( "cannot start or end with ' '",
meta.name_value_literal_span().unwrap_or_else(|| meta.span()), );
&format!(
"`#[doc(alias{})]` cannot start or end with ' '",
if is_list { "(\"...\")" } else { " = \"...\"" },
),
)
.emit();
return false;
} }
if let Some(err) = match target { if let Some(err) = match target {
Target::Impl => Some("implementation block"), Target::Impl => Some("implementation block"),
@ -464,32 +459,11 @@ impl CheckAttrVisitor<'tcx> {
} }
_ => None, _ => None,
} { } {
self.tcx return err_fn(meta.span(), &format!("isn't allowed on {}", err));
.sess
.struct_span_err(
meta.span(),
&format!(
"`#[doc(alias{})]` isn't allowed on {}",
if is_list { "(\"...\")" } else { " = \"...\"" },
err,
),
)
.emit();
return false;
} }
let item_name = self.tcx.hir().name(hir_id); let item_name = self.tcx.hir().name(hir_id);
if &*item_name.as_str() == doc_alias { if &*item_name.as_str() == doc_alias {
self.tcx return err_fn(meta.span(), "is the same as the item's name");
.sess
.struct_span_err(
meta.span(),
&format!(
"`#[doc(alias{})]` is the same as the item's name",
if is_list { "(\"...\")" } else { " = \"...\"" },
),
)
.emit();
return false;
} }
true true
} }
@ -510,7 +484,7 @@ impl CheckAttrVisitor<'tcx> {
.sess .sess
.struct_span_err( .struct_span_err(
v.span(), v.span(),
"`#[doc(alias(\"a\")]` expects string literals", "`#[doc(alias(\"a\"))]` expects string literals",
) )
.emit(); .emit();
errors += 1; errors += 1;
@ -521,7 +495,7 @@ impl CheckAttrVisitor<'tcx> {
.sess .sess
.struct_span_err( .struct_span_err(
v.span(), v.span(),
"`#[doc(alias(\"a\")]` expects string literals", "`#[doc(alias(\"a\"))]` expects string literals",
) )
.emit(); .emit();
errors += 1; errors += 1;
@ -537,7 +511,7 @@ impl CheckAttrVisitor<'tcx> {
.struct_span_err( .struct_span_err(
meta.span(), meta.span(),
"doc alias attribute expects a string `#[doc(alias = \"a\")]` or a list of \ "doc alias attribute expects a string `#[doc(alias = \"a\")]` or a list of \
strings: `#[doc(alias(\"a\", \"b\")]`", strings `#[doc(alias(\"a\", \"b\"))]`",
) )
.emit(); .emit();
false false

View file

@ -911,24 +911,23 @@ impl Attributes {
} }
crate fn get_doc_aliases(&self) -> FxHashSet<String> { crate fn get_doc_aliases(&self) -> FxHashSet<String> {
self.other_attrs let mut aliases = FxHashSet::default();
.lists(sym::doc)
.filter(|a| a.has_name(sym::alias)) for attr in self.other_attrs.lists(sym::doc).filter(|a| a.has_name(sym::alias)) {
.map(|a| { if let Some(values) = attr.meta_item_list() {
if let Some(values) = a.meta_item_list() { for l in values {
values match l.literal().unwrap().kind {
.iter() ast::LitKind::Str(s, _) => {
.map(|l| match l.literal().unwrap().kind { aliases.insert(s.as_str().to_string());
ast::LitKind::Str(s, _) => s.as_str().to_string(), }
_ => unreachable!(), _ => unreachable!(),
}) }
.collect::<Vec<_>>()
} else {
vec![a.value_str().map(|s| s.to_string()).unwrap()]
} }
}) } else {
.flatten() aliases.insert(attr.value_str().map(|s| s.to_string()).unwrap());
.collect::<FxHashSet<_>>() }
}
aliases
} }
} }