Skip to content

Commit 1d26e6b

Browse files
Improve code by removing similar function calls and using loops instead for collecting iterators
1 parent 2069d3e commit 1d26e6b

File tree

2 files changed

+49
-76
lines changed

2 files changed

+49
-76
lines changed

compiler/rustc_passes/src/check_attr.rs

+33-59
Original file line numberDiff line numberDiff line change
@@ -398,47 +398,42 @@ impl CheckAttrVisitor<'tcx> {
398398
target: Target,
399399
is_list: bool,
400400
) -> bool {
401+
let tcx = self.tcx;
402+
let err_fn = move |span: Span, msg: &str| {
403+
tcx.sess.span_err(
404+
span,
405+
&format!(
406+
"`#[doc(alias{})]` {}",
407+
if is_list { "(\"...\")" } else { " = \"...\"" },
408+
msg,
409+
),
410+
);
411+
false
412+
};
401413
if doc_alias.is_empty() {
402-
self.tcx
403-
.sess
404-
.struct_span_err(
405-
meta.name_value_literal_span().unwrap_or_else(|| meta.span()),
406-
&format!(
407-
"`#[doc(alias{})]` attribute cannot have empty value",
408-
if is_list { "(\"...\")" } else { " = \"...\"" },
409-
),
410-
)
411-
.emit();
412-
return false;
414+
return err_fn(
415+
meta.name_value_literal_span().unwrap_or_else(|| meta.span()),
416+
"attribute cannot have empty value",
417+
);
413418
}
414419
if let Some(c) =
415420
doc_alias.chars().find(|&c| c == '"' || c == '\'' || (c.is_whitespace() && c != ' '))
416421
{
417-
self.tcx
418-
.sess
419-
.struct_span_err(
420-
meta.name_value_literal_span().unwrap_or_else(|| meta.span()),
421-
&format!(
422-
"{:?} character isn't allowed in `#[doc(alias{})]`",
423-
c,
424-
if is_list { "(\"...\")" } else { " = \"...\"" },
425-
),
426-
)
427-
.emit();
422+
self.tcx.sess.span_err(
423+
meta.name_value_literal_span().unwrap_or_else(|| meta.span()),
424+
&format!(
425+
"{:?} character isn't allowed in `#[doc(alias{})]`",
426+
c,
427+
if is_list { "(\"...\")" } else { " = \"...\"" },
428+
),
429+
);
428430
return false;
429431
}
430432
if doc_alias.starts_with(' ') || doc_alias.ends_with(' ') {
431-
self.tcx
432-
.sess
433-
.struct_span_err(
434-
meta.name_value_literal_span().unwrap_or_else(|| meta.span()),
435-
&format!(
436-
"`#[doc(alias{})]` cannot start or end with ' '",
437-
if is_list { "(\"...\")" } else { " = \"...\"" },
438-
),
439-
)
440-
.emit();
441-
return false;
433+
return err_fn(
434+
meta.name_value_literal_span().unwrap_or_else(|| meta.span()),
435+
"cannot start or end with ' '",
436+
);
442437
}
443438
if let Some(err) = match target {
444439
Target::Impl => Some("implementation block"),
@@ -464,32 +459,11 @@ impl CheckAttrVisitor<'tcx> {
464459
}
465460
_ => None,
466461
} {
467-
self.tcx
468-
.sess
469-
.struct_span_err(
470-
meta.span(),
471-
&format!(
472-
"`#[doc(alias{})]` isn't allowed on {}",
473-
if is_list { "(\"...\")" } else { " = \"...\"" },
474-
err,
475-
),
476-
)
477-
.emit();
478-
return false;
462+
return err_fn(meta.span(), &format!("isn't allowed on {}", err));
479463
}
480464
let item_name = self.tcx.hir().name(hir_id);
481465
if &*item_name.as_str() == doc_alias {
482-
self.tcx
483-
.sess
484-
.struct_span_err(
485-
meta.span(),
486-
&format!(
487-
"`#[doc(alias{})]` is the same as the item's name",
488-
if is_list { "(\"...\")" } else { " = \"...\"" },
489-
),
490-
)
491-
.emit();
492-
return false;
466+
return err_fn(meta.span(), "is the same as the item's name");
493467
}
494468
true
495469
}
@@ -510,7 +484,7 @@ impl CheckAttrVisitor<'tcx> {
510484
.sess
511485
.struct_span_err(
512486
v.span(),
513-
"`#[doc(alias(\"a\")]` expects string literals",
487+
"`#[doc(alias(\"a\"))]` expects string literals",
514488
)
515489
.emit();
516490
errors += 1;
@@ -521,7 +495,7 @@ impl CheckAttrVisitor<'tcx> {
521495
.sess
522496
.struct_span_err(
523497
v.span(),
524-
"`#[doc(alias(\"a\")]` expects string literals",
498+
"`#[doc(alias(\"a\"))]` expects string literals",
525499
)
526500
.emit();
527501
errors += 1;
@@ -537,7 +511,7 @@ impl CheckAttrVisitor<'tcx> {
537511
.struct_span_err(
538512
meta.span(),
539513
"doc alias attribute expects a string `#[doc(alias = \"a\")]` or a list of \
540-
strings: `#[doc(alias(\"a\", \"b\")]`",
514+
strings `#[doc(alias(\"a\", \"b\"))]`",
541515
)
542516
.emit();
543517
false

src/librustdoc/clean/types.rs

+16-17
Original file line numberDiff line numberDiff line change
@@ -911,24 +911,23 @@ impl Attributes {
911911
}
912912

913913
crate fn get_doc_aliases(&self) -> FxHashSet<String> {
914-
self.other_attrs
915-
.lists(sym::doc)
916-
.filter(|a| a.has_name(sym::alias))
917-
.map(|a| {
918-
if let Some(values) = a.meta_item_list() {
919-
values
920-
.iter()
921-
.map(|l| match l.literal().unwrap().kind {
922-
ast::LitKind::Str(s, _) => s.as_str().to_string(),
923-
_ => unreachable!(),
924-
})
925-
.collect::<Vec<_>>()
926-
} else {
927-
vec![a.value_str().map(|s| s.to_string()).unwrap()]
914+
let mut aliases = FxHashSet::default();
915+
916+
for attr in self.other_attrs.lists(sym::doc).filter(|a| a.has_name(sym::alias)) {
917+
if let Some(values) = attr.meta_item_list() {
918+
for l in values {
919+
match l.literal().unwrap().kind {
920+
ast::LitKind::Str(s, _) => {
921+
aliases.insert(s.as_str().to_string());
922+
}
923+
_ => unreachable!(),
924+
}
928925
}
929-
})
930-
.flatten()
931-
.collect::<FxHashSet<_>>()
926+
} else {
927+
aliases.insert(attr.value_str().map(|s| s.to_string()).unwrap());
928+
}
929+
}
930+
aliases
932931
}
933932
}
934933

0 commit comments

Comments
 (0)