Skip to content

Commit 53bb632

Browse files
Rollup merge of #107467 - WaffleLapkin:uneq, r=oli-obk
Improve enum checks Some light refactoring.
2 parents c6a104f + 340414e commit 53bb632

File tree

33 files changed

+276
-258
lines changed

33 files changed

+276
-258
lines changed

compiler/rustc_ast/src/util/comments.rs

+17-16
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_borrowck/src/diagnostics/mutability_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
344344
} else {
345345
err.span_help(source_info.span, "try removing `&mut` here");
346346
}
347-
} else if decl.mutability == Mutability::Not {
347+
} else if decl.mutability.is_not() {
348348
if matches!(
349349
decl.local_info,
350350
Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::ImplicitSelf(

compiler/rustc_borrowck/src/type_check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2028,7 +2028,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
20282028
}
20292029
};
20302030

2031-
if ty_to_mut == Mutability::Mut && ty_mut == Mutability::Not {
2031+
if ty_to_mut.is_mut() && ty_mut.is_not() {
20322032
span_mirbug!(
20332033
self,
20342034
rvalue,

compiler/rustc_const_eval/src/const_eval/machine.rs

+3-4
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_const_eval/src/interpret/memory.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
304304
.into());
305305
};
306306

307-
if alloc.mutability == Mutability::Not {
307+
if alloc.mutability.is_not() {
308308
throw_ub_format!("deallocating immutable allocation {alloc_id:?}");
309309
}
310310
if alloc_kind != kind {
@@ -631,7 +631,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
631631
}
632632

633633
let (_kind, alloc) = self.memory.alloc_map.get_mut(id).unwrap();
634-
if alloc.mutability == Mutability::Not {
634+
if alloc.mutability.is_not() {
635635
throw_ub!(WriteToReadOnly(id))
636636
}
637637
Ok((alloc, &mut self.machine))

compiler/rustc_const_eval/src/transform/validate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
754754
// FIXME(JakobDegen) The validator should check that `self.mir_phase <
755755
// DropsLowered`. However, this causes ICEs with generation of drop shims, which
756756
// seem to fail to set their `MirPhase` correctly.
757-
if *kind == RetagKind::Raw || *kind == RetagKind::TwoPhase {
757+
if matches!(kind, RetagKind::Raw | RetagKind::TwoPhase) {
758758
self.fail(location, format!("explicit `{:?}` is forbidden", kind));
759759
}
760760
}

compiler/rustc_errors/src/emitter.rs

+31-23
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_errors/src/styled_buffer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ impl StyledBuffer {
142142
pub fn set_style(&mut self, line: usize, col: usize, style: Style, overwrite: bool) {
143143
if let Some(ref mut line) = self.lines.get_mut(line) {
144144
if let Some(StyledChar { style: s, .. }) = line.get_mut(col) {
145-
if overwrite || *s == Style::NoStyle || *s == Style::Quotation {
145+
if overwrite || matches!(s, Style::NoStyle | Style::Quotation) {
146146
*s = style;
147147
}
148148
}

compiler/rustc_expand/src/mbe/macro_parser.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ impl TtParser {
503503
mp.push_match(metavar_idx, seq_depth, MatchedSeq(vec![]));
504504
}
505505

506-
if op == KleeneOp::ZeroOrMore || op == KleeneOp::ZeroOrOne {
506+
if matches!(op, KleeneOp::ZeroOrMore | KleeneOp::ZeroOrOne) {
507507
// Try zero matches of this sequence, by skipping over it.
508508
self.cur_mps.push(MatcherPos {
509509
idx: idx_first_after,

compiler/rustc_hir_analysis/src/astconv/generics.rs

+3-4
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

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

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

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

compiler/rustc_hir_pretty/src/lib.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ use rustc_ast_pretty::pp::{self, Breaks};
99
use rustc_ast_pretty::pprust::{Comments, PrintState};
1010
use rustc_hir as hir;
1111
use rustc_hir::LifetimeParamKind;
12-
use rustc_hir::{
13-
BindingAnnotation, ByRef, GenericArg, GenericParam, GenericParamKind, Mutability, Node, Term,
14-
};
12+
use rustc_hir::{BindingAnnotation, ByRef, GenericArg, GenericParam, GenericParamKind, Node, Term};
1513
use rustc_hir::{GenericBound, PatKind, RangeEnd, TraitBoundModifier};
1614
use rustc_span::source_map::SourceMap;
1715
use rustc_span::symbol::{kw, Ident, IdentPrinter, Symbol};
@@ -1746,7 +1744,7 @@ impl<'a> State<'a> {
17461744
if by_ref == ByRef::Yes {
17471745
self.word_nbsp("ref");
17481746
}
1749-
if mutbl == Mutability::Mut {
1747+
if mutbl.is_mut() {
17501748
self.word_nbsp("mut");
17511749
}
17521750
self.print_ident(ident);

compiler/rustc_hir_typeck/src/method/probe.rs

+3-4
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

+20-19
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-
}
588584

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;
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

0 commit comments

Comments
 (0)