Skip to content

Commit fd649a3

Browse files
committed
Replace enum ==s with matches where it makes sense
1 parent f55b002 commit fd649a3

File tree

16 files changed

+256
-229
lines changed

16 files changed

+256
-229
lines changed

compiler/rustc_ast/src/util/comments.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,23 +58,24 @@ pub fn beautify_doc_string(data: Symbol, kind: CommentKind) -> Symbol {
5858
// In case we have doc comments like `/**` or `/*!`, we want to remove stars if they are
5959
// present. However, we first need to strip the empty lines so they don't get in the middle
6060
// when we try to compute the "horizontal trim".
61-
let lines = if kind == CommentKind::Block {
62-
// Whatever happens, we skip the first line.
63-
let mut i = lines
64-
.get(0)
65-
.map(|l| if l.trim_start().starts_with('*') { 0 } else { 1 })
66-
.unwrap_or(0);
67-
let mut j = lines.len();
68-
69-
while i < j && lines[i].trim().is_empty() {
70-
i += 1;
71-
}
72-
while j > i && lines[j - 1].trim().is_empty() {
73-
j -= 1;
61+
let lines = match kind {
62+
CommentKind::Block => {
63+
// Whatever happens, we skip the first line.
64+
let mut i = lines
65+
.get(0)
66+
.map(|l| if l.trim_start().starts_with('*') { 0 } else { 1 })
67+
.unwrap_or(0);
68+
let mut j = lines.len();
69+
70+
while i < j && lines[i].trim().is_empty() {
71+
i += 1;
72+
}
73+
while j > i && lines[j - 1].trim().is_empty() {
74+
j -= 1;
75+
}
76+
&lines[i..j]
7477
}
75-
&lines[i..j]
76-
} else {
77-
lines
78+
CommentKind::Line => lines,
7879
};
7980

8081
for line in lines {

compiler/rustc_const_eval/src/const_eval/machine.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -622,10 +622,9 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
622622
let alloc = alloc.inner();
623623
if is_write {
624624
// Write access. These are never allowed, but we give a targeted error message.
625-
if alloc.mutability == Mutability::Not {
626-
Err(err_ub!(WriteToReadOnly(alloc_id)).into())
627-
} else {
628-
Err(ConstEvalErrKind::ModifiedGlobal.into())
625+
match alloc.mutability {
626+
Mutability::Not => Err(err_ub!(WriteToReadOnly(alloc_id)).into()),
627+
Mutability::Mut => Err(ConstEvalErrKind::ModifiedGlobal.into()),
629628
}
630629
} else {
631630
// Read access. These are usually allowed, with some exceptions.

compiler/rustc_errors/src/emitter.rs

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2113,30 +2113,38 @@ impl EmitterWriter {
21132113
}
21142114
}
21152115
for sugg in suggestions {
2116-
if sugg.style == SuggestionStyle::CompletelyHidden {
2117-
// do not display this suggestion, it is meant only for tools
2118-
} else if sugg.style == SuggestionStyle::HideCodeAlways {
2119-
if let Err(e) = self.emit_message_default(
2120-
&MultiSpan::new(),
2121-
&[(sugg.msg.to_owned(), Style::HeaderMsg)],
2122-
args,
2123-
&None,
2124-
&Level::Help,
2125-
max_line_num_len,
2126-
true,
2127-
None,
2128-
) {
2129-
panic!("failed to emit error: {}", e);
2116+
match sugg.style {
2117+
SuggestionStyle::CompletelyHidden => {
2118+
// do not display this suggestion, it is meant only for tools
21302119
}
2131-
} else if let Err(e) = self.emit_suggestion_default(
2132-
span,
2133-
sugg,
2134-
args,
2135-
&Level::Help,
2136-
max_line_num_len,
2137-
) {
2138-
panic!("failed to emit error: {}", e);
2139-
};
2120+
SuggestionStyle::HideCodeAlways => {
2121+
if let Err(e) = self.emit_message_default(
2122+
&MultiSpan::new(),
2123+
&[(sugg.msg.to_owned(), Style::HeaderMsg)],
2124+
args,
2125+
&None,
2126+
&Level::Help,
2127+
max_line_num_len,
2128+
true,
2129+
None,
2130+
) {
2131+
panic!("failed to emit error: {}", e);
2132+
}
2133+
}
2134+
SuggestionStyle::HideCodeInline
2135+
| SuggestionStyle::ShowCode
2136+
| SuggestionStyle::ShowAlways => {
2137+
if let Err(e) = self.emit_suggestion_default(
2138+
span,
2139+
sugg,
2140+
args,
2141+
&Level::Help,
2142+
max_line_num_len,
2143+
) {
2144+
panic!("failed to emit error: {}", e);
2145+
}
2146+
}
2147+
}
21402148
}
21412149
}
21422150
}

compiler/rustc_hir_analysis/src/astconv/generics.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,10 +385,9 @@ pub fn check_generic_arg_count_for_call(
385385
) -> GenericArgCountResult {
386386
let empty_args = hir::GenericArgs::none();
387387
let gen_args = seg.args.unwrap_or(&empty_args);
388-
let gen_pos = if is_method_call == IsMethodCall::Yes {
389-
GenericArgPosition::MethodCall
390-
} else {
391-
GenericArgPosition::Value
388+
let gen_pos = match is_method_call {
389+
IsMethodCall::Yes => GenericArgPosition::MethodCall,
390+
IsMethodCall::No => GenericArgPosition::Value,
392391
};
393392
let has_self = generics.parent.is_none() && generics.has_self;
394393

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -605,59 +605,66 @@ fn check_item_type(tcx: TyCtxt<'_>, id: hir::ItemId) {
605605
};
606606
check_abi(tcx, it.hir_id(), it.span, abi);
607607

608-
if abi == Abi::RustIntrinsic {
609-
for item in items {
610-
let item = tcx.hir().foreign_item(item.id);
611-
intrinsic::check_intrinsic_type(tcx, item);
612-
}
613-
} else if abi == Abi::PlatformIntrinsic {
614-
for item in items {
615-
let item = tcx.hir().foreign_item(item.id);
616-
intrinsic::check_platform_intrinsic_type(tcx, item);
608+
match abi {
609+
Abi::RustIntrinsic => {
610+
for item in items {
611+
let item = tcx.hir().foreign_item(item.id);
612+
intrinsic::check_intrinsic_type(tcx, item);
613+
}
617614
}
618-
} else {
619-
for item in items {
620-
let def_id = item.id.owner_id.def_id;
621-
let generics = tcx.generics_of(def_id);
622-
let own_counts = generics.own_counts();
623-
if generics.params.len() - own_counts.lifetimes != 0 {
624-
let (kinds, kinds_pl, egs) = match (own_counts.types, own_counts.consts) {
625-
(_, 0) => ("type", "types", Some("u32")),
626-
// We don't specify an example value, because we can't generate
627-
// a valid value for any type.
628-
(0, _) => ("const", "consts", None),
629-
_ => ("type or const", "types or consts", None),
630-
};
631-
struct_span_err!(
632-
tcx.sess,
633-
item.span,
634-
E0044,
635-
"foreign items may not have {kinds} parameters",
636-
)
637-
.span_label(item.span, &format!("can't have {kinds} parameters"))
638-
.help(
639-
// FIXME: once we start storing spans for type arguments, turn this
640-
// into a suggestion.
641-
&format!(
642-
"replace the {} parameters with concrete {}{}",
643-
kinds,
644-
kinds_pl,
645-
egs.map(|egs| format!(" like `{}`", egs)).unwrap_or_default(),
646-
),
647-
)
648-
.emit();
615+
616+
Abi::PlatformIntrinsic => {
617+
for item in items {
618+
let item = tcx.hir().foreign_item(item.id);
619+
intrinsic::check_platform_intrinsic_type(tcx, item);
649620
}
621+
}
650622

651-
let item = tcx.hir().foreign_item(item.id);
652-
match &item.kind {
653-
hir::ForeignItemKind::Fn(fn_decl, _, _) => {
654-
require_c_abi_if_c_variadic(tcx, fn_decl, abi, item.span);
623+
_ => {
624+
for item in items {
625+
let def_id = item.id.owner_id.def_id;
626+
let generics = tcx.generics_of(def_id);
627+
let own_counts = generics.own_counts();
628+
if generics.params.len() - own_counts.lifetimes != 0 {
629+
let (kinds, kinds_pl, egs) = match (own_counts.types, own_counts.consts)
630+
{
631+
(_, 0) => ("type", "types", Some("u32")),
632+
// We don't specify an example value, because we can't generate
633+
// a valid value for any type.
634+
(0, _) => ("const", "consts", None),
635+
_ => ("type or const", "types or consts", None),
636+
};
637+
struct_span_err!(
638+
tcx.sess,
639+
item.span,
640+
E0044,
641+
"foreign items may not have {kinds} parameters",
642+
)
643+
.span_label(item.span, &format!("can't have {kinds} parameters"))
644+
.help(
645+
// FIXME: once we start storing spans for type arguments, turn this
646+
// into a suggestion.
647+
&format!(
648+
"replace the {} parameters with concrete {}{}",
649+
kinds,
650+
kinds_pl,
651+
egs.map(|egs| format!(" like `{}`", egs)).unwrap_or_default(),
652+
),
653+
)
654+
.emit();
655655
}
656-
hir::ForeignItemKind::Static(..) => {
657-
check_static_inhabited(tcx, def_id);
658-
check_static_linkage(tcx, def_id);
656+
657+
let item = tcx.hir().foreign_item(item.id);
658+
match &item.kind {
659+
hir::ForeignItemKind::Fn(fn_decl, _, _) => {
660+
require_c_abi_if_c_variadic(tcx, fn_decl, abi, item.span);
661+
}
662+
hir::ForeignItemKind::Static(..) => {
663+
check_static_inhabited(tcx, def_id);
664+
check_static_linkage(tcx, def_id);
665+
}
666+
_ => {}
659667
}
660-
_ => {}
661668
}
662669
}
663670
}

compiler/rustc_hir_typeck/src/method/probe.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,13 +1354,12 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
13541354
return Some(Err(MethodError::Ambiguity(sources)));
13551355
}
13561356

1357-
applicable_candidates.pop().map(|(probe, status)| {
1358-
if status == ProbeResult::Match {
1357+
applicable_candidates.pop().map(|(probe, status)| match status {
1358+
ProbeResult::Match => {
13591359
Ok(probe
13601360
.to_unadjusted_pick(self_ty, unstable_candidates.cloned().unwrap_or_default()))
1361-
} else {
1362-
Err(MethodError::BadReturnType)
13631361
}
1362+
ProbeResult::NoMatch | ProbeResult::BadReturnType => Err(MethodError::BadReturnType),
13641363
})
13651364
}
13661365
}

compiler/rustc_lint/src/builtin.rs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -580,27 +580,28 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
580580
}
581581

582582
fn check_impl_item(&mut self, cx: &LateContext<'_>, impl_item: &hir::ImplItem<'_>) {
583-
// If the method is an impl for a trait, don't doc.
584583
let context = method_context(cx, impl_item.owner_id.def_id);
585-
if context == MethodLateContext::TraitImpl {
586-
return;
587-
}
588-
589-
// If the method is an impl for an item with docs_hidden, don't doc.
590-
if context == MethodLateContext::PlainImpl {
591-
let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id());
592-
let impl_ty = cx.tcx.type_of(parent);
593-
let outerdef = match impl_ty.kind() {
594-
ty::Adt(def, _) => Some(def.did()),
595-
ty::Foreign(def_id) => Some(*def_id),
596-
_ => None,
597-
};
598-
let is_hidden = match outerdef {
599-
Some(id) => cx.tcx.is_doc_hidden(id),
600-
None => false,
601-
};
602-
if is_hidden {
603-
return;
584+
585+
match context {
586+
// If the method is an impl for a trait, don't doc.
587+
MethodLateContext::TraitImpl => return,
588+
MethodLateContext::TraitAutoImpl => {}
589+
// If the method is an impl for an item with docs_hidden, don't doc.
590+
MethodLateContext::PlainImpl => {
591+
let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id());
592+
let impl_ty = cx.tcx.type_of(parent);
593+
let outerdef = match impl_ty.kind() {
594+
ty::Adt(def, _) => Some(def.did()),
595+
ty::Foreign(def_id) => Some(*def_id),
596+
_ => None,
597+
};
598+
let is_hidden = match outerdef {
599+
Some(id) => cx.tcx.is_doc_hidden(id),
600+
None => false,
601+
};
602+
if is_hidden {
603+
return;
604+
}
604605
}
605606
}
606607

compiler/rustc_metadata/src/dependency_format.rs

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -113,37 +113,37 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList {
113113
CrateType::Staticlib => Linkage::Static,
114114
};
115115

116-
if preferred_linkage == Linkage::NotLinked {
116+
match preferred_linkage {
117117
// If the crate is not linked, there are no link-time dependencies.
118-
return Vec::new();
119-
}
120-
121-
if preferred_linkage == Linkage::Static {
122-
// Attempt static linkage first. For dylibs and executables, we may be
123-
// able to retry below with dynamic linkage.
124-
if let Some(v) = attempt_static(tcx) {
125-
return v;
126-
}
118+
Linkage::NotLinked => return Vec::new(),
119+
Linkage::Static => {
120+
// Attempt static linkage first. For dylibs and executables, we may be
121+
// able to retry below with dynamic linkage.
122+
if let Some(v) = attempt_static(tcx) {
123+
return v;
124+
}
127125

128-
// Staticlibs and static executables must have all static dependencies.
129-
// If any are not found, generate some nice pretty errors.
130-
if ty == CrateType::Staticlib
131-
|| (ty == CrateType::Executable
132-
&& sess.crt_static(Some(ty))
133-
&& !sess.target.crt_static_allows_dylibs)
134-
{
135-
for &cnum in tcx.crates(()).iter() {
136-
if tcx.dep_kind(cnum).macros_only() {
137-
continue;
126+
// Staticlibs and static executables must have all static dependencies.
127+
// If any are not found, generate some nice pretty errors.
128+
if ty == CrateType::Staticlib
129+
|| (ty == CrateType::Executable
130+
&& sess.crt_static(Some(ty))
131+
&& !sess.target.crt_static_allows_dylibs)
132+
{
133+
for &cnum in tcx.crates(()).iter() {
134+
if tcx.dep_kind(cnum).macros_only() {
135+
continue;
136+
}
137+
let src = tcx.used_crate_source(cnum);
138+
if src.rlib.is_some() {
139+
continue;
140+
}
141+
sess.emit_err(RlibRequired { crate_name: tcx.crate_name(cnum) });
138142
}
139-
let src = tcx.used_crate_source(cnum);
140-
if src.rlib.is_some() {
141-
continue;
142-
}
143-
sess.emit_err(RlibRequired { crate_name: tcx.crate_name(cnum) });
143+
return Vec::new();
144144
}
145-
return Vec::new();
146145
}
146+
Linkage::Dynamic | Linkage::IncludedFromDylib => {}
147147
}
148148

149149
let mut formats = FxHashMap::default();
@@ -283,12 +283,9 @@ fn attempt_static(tcx: TyCtxt<'_>) -> Option<DependencyList> {
283283
let mut ret = tcx
284284
.crates(())
285285
.iter()
286-
.map(|&cnum| {
287-
if tcx.dep_kind(cnum) == CrateDepKind::Explicit {
288-
Linkage::Static
289-
} else {
290-
Linkage::NotLinked
291-
}
286+
.map(|&cnum| match tcx.dep_kind(cnum) {
287+
CrateDepKind::Explicit => Linkage::Static,
288+
CrateDepKind::MacrosOnly | CrateDepKind::Implicit => Linkage::NotLinked,
292289
})
293290
.collect::<Vec<_>>();
294291

0 commit comments

Comments
 (0)