Skip to content

Commit 5f6a6bc

Browse files
Improve code readability
1 parent 93fbd25 commit 5f6a6bc

File tree

1 file changed

+45
-21
lines changed

1 file changed

+45
-21
lines changed

compiler/rustc_passes/src/check_attr.rs

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,21 @@ impl IntoDiagArg for ProcMacroKind {
9999
}
100100
}
101101

102+
#[derive(Clone, Copy)]
103+
enum DocFakeItemKind {
104+
Attribute,
105+
Keyword,
106+
}
107+
108+
impl DocFakeItemKind {
109+
fn name(self) -> &'static str {
110+
match self {
111+
Self::Attribute => "attribute",
112+
Self::Keyword => "keyword",
113+
}
114+
}
115+
}
116+
102117
struct CheckAttrVisitor<'tcx> {
103118
tcx: TyCtxt<'tcx>,
104119

@@ -855,7 +870,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
855870
&self,
856871
meta: &MetaItemInner,
857872
hir_id: HirId,
858-
is_keyword: bool,
873+
attr_kind: DocFakeItemKind,
859874
) {
860875
fn is_doc_keyword(s: Symbol) -> bool {
861876
// FIXME: Once rustdoc can handle URL conflicts on case insensitive file systems, we
@@ -868,13 +883,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
868883
rustc_feature::BUILTIN_ATTRIBUTE_MAP.contains_key(&s)
869884
}
870885

871-
fn get_attr_name(is_keyword: bool) -> &'static str {
872-
if is_keyword { "keyword" } else { "attribute" }
873-
}
874-
875886
let value = match meta.value_str() {
876887
Some(value) if value != sym::empty => value,
877-
_ => return self.doc_attr_str_error(meta, get_attr_name(is_keyword)),
888+
_ => return self.doc_attr_str_error(meta, attr_kind.name()),
878889
};
879890

880891
let item_kind = match self.tcx.hir_node(hir_id) {
@@ -886,31 +897,36 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
886897
if !module.item_ids.is_empty() {
887898
self.dcx().emit_err(errors::DocKeywordAttributeEmptyMod {
888899
span: meta.span(),
889-
attr_name: get_attr_name(is_keyword),
900+
attr_name: attr_kind.name(),
890901
});
891902
return;
892903
}
893904
}
894905
_ => {
895906
self.dcx().emit_err(errors::DocKeywordAttributeNotMod {
896907
span: meta.span(),
897-
attr_name: get_attr_name(is_keyword),
908+
attr_name: attr_kind.name(),
898909
});
899910
return;
900911
}
901912
}
902-
if is_keyword {
903-
if !is_doc_keyword(value) {
904-
self.dcx().emit_err(errors::DocKeywordNotKeyword {
905-
span: meta.name_value_literal_span().unwrap_or_else(|| meta.span()),
906-
keyword: value,
907-
});
913+
match attr_kind {
914+
DocFakeItemKind::Keyword => {
915+
if !is_doc_keyword(value) {
916+
self.dcx().emit_err(errors::DocKeywordNotKeyword {
917+
span: meta.name_value_literal_span().unwrap_or_else(|| meta.span()),
918+
keyword: value,
919+
});
920+
}
921+
}
922+
DocFakeItemKind::Attribute => {
923+
if !is_builtin_attr(value) {
924+
self.dcx().emit_err(errors::DocAttributeNotAttribute {
925+
span: meta.name_value_literal_span().unwrap_or_else(|| meta.span()),
926+
attribute: value,
927+
});
928+
}
908929
}
909-
} else if !is_builtin_attr(value) {
910-
self.dcx().emit_err(errors::DocAttributeNotAttribute {
911-
span: meta.name_value_literal_span().unwrap_or_else(|| meta.span()),
912-
attribute: value,
913-
});
914930
}
915931
}
916932

@@ -1170,13 +1186,21 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
11701186

11711187
Some(sym::keyword) => {
11721188
if self.check_attr_not_crate_level(meta, hir_id, "keyword") {
1173-
self.check_doc_keyword_and_attribute(meta, hir_id, true);
1189+
self.check_doc_keyword_and_attribute(
1190+
meta,
1191+
hir_id,
1192+
DocFakeItemKind::Keyword,
1193+
);
11741194
}
11751195
}
11761196

11771197
Some(sym::attribute) => {
11781198
if self.check_attr_not_crate_level(meta, hir_id, "attribute") {
1179-
self.check_doc_keyword_and_attribute(meta, hir_id, false);
1199+
self.check_doc_keyword_and_attribute(
1200+
meta,
1201+
hir_id,
1202+
DocFakeItemKind::Attribute,
1203+
);
11801204
}
11811205
}
11821206

0 commit comments

Comments
 (0)