Skip to content

Commit dcf4d1f

Browse files
Rollup merge of #76888 - matthiaskrgr:clippy_single_match_2, r=Dylan-DPC
use if let instead of single match arm expressions use if let instead of single match arm expressions to compact code and reduce nesting (clippy::single_match)
2 parents 60b9901 + c690c82 commit dcf4d1f

File tree

8 files changed

+28
-58
lines changed

8 files changed

+28
-58
lines changed

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -488,18 +488,16 @@ impl<'tcx> Visitor<'tcx> for HirTraitObjectVisitor {
488488
}
489489

490490
fn visit_ty(&mut self, t: &'tcx hir::Ty<'tcx>) {
491-
match t.kind {
492-
TyKind::TraitObject(
493-
poly_trait_refs,
494-
Lifetime { name: LifetimeName::ImplicitObjectLifetimeDefault, .. },
495-
) => {
496-
for ptr in poly_trait_refs {
497-
if Some(self.1) == ptr.trait_ref.trait_def_id() {
498-
self.0.push(ptr.span);
499-
}
491+
if let TyKind::TraitObject(
492+
poly_trait_refs,
493+
Lifetime { name: LifetimeName::ImplicitObjectLifetimeDefault, .. },
494+
) = t.kind
495+
{
496+
for ptr in poly_trait_refs {
497+
if Some(self.1) == ptr.trait_ref.trait_def_id() {
498+
self.0.push(ptr.span);
500499
}
501500
}
502-
_ => {}
503501
}
504502
walk_ty(self, t);
505503
}

compiler/rustc_middle/src/ty/error.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -834,14 +834,11 @@ fn foo(&self) -> Self::T { String::new() }
834834
kind: hir::ItemKind::Impl { items, .. }, ..
835835
})) => {
836836
for item in &items[..] {
837-
match item.kind {
838-
hir::AssocItemKind::Type => {
839-
if self.type_of(self.hir().local_def_id(item.id.hir_id)) == found {
840-
db.span_label(item.span, "expected this associated type");
841-
return true;
842-
}
837+
if let hir::AssocItemKind::Type = item.kind {
838+
if self.type_of(self.hir().local_def_id(item.id.hir_id)) == found {
839+
db.span_label(item.span, "expected this associated type");
840+
return true;
843841
}
844-
_ => {}
845842
}
846843
}
847844
}

compiler/rustc_middle/src/ty/print/pretty.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -2125,17 +2125,10 @@ fn for_each_def(tcx: TyCtxt<'_>, mut collect_fn: impl for<'b> FnMut(&'b Ident, N
21252125
// Iterate all local crate items no matter where they are defined.
21262126
let hir = tcx.hir();
21272127
for item in hir.krate().items.values() {
2128-
if item.ident.name.as_str().is_empty() {
2128+
if item.ident.name.as_str().is_empty() || matches!(item.kind, ItemKind::Use(_, _)) {
21292129
continue;
21302130
}
21312131

2132-
match item.kind {
2133-
ItemKind::Use(_, _) => {
2134-
continue;
2135-
}
2136-
_ => {}
2137-
}
2138-
21392132
if let Some(local_def_id) = hir.definitions().opt_hir_id_to_local_def_id(item.hir_id) {
21402133
let def_id = local_def_id.to_def_id();
21412134
let ns = tcx.def_kind(def_id).ns().unwrap_or(Namespace::TypeNS);

compiler/rustc_mir/src/transform/promote_consts.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,8 @@ impl<'tcx> Visitor<'tcx> for Collector<'_, 'tcx> {
242242
}
243243
TerminatorKind::InlineAsm { ref operands, .. } => {
244244
for (index, op) in operands.iter().enumerate() {
245-
match op {
246-
InlineAsmOperand::Const { .. } => {
247-
self.candidates.push(Candidate::InlineAsm { bb: location.block, index })
248-
}
249-
_ => {}
245+
if let InlineAsmOperand::Const { .. } = op {
246+
self.candidates.push(Candidate::InlineAsm { bb: location.block, index })
250247
}
251248
}
252249
}
@@ -612,12 +609,9 @@ impl<'tcx> Validator<'_, 'tcx> {
612609
let operand_ty = operand.ty(self.body, self.tcx);
613610
let cast_in = CastTy::from_ty(operand_ty).expect("bad input type for cast");
614611
let cast_out = CastTy::from_ty(cast_ty).expect("bad output type for cast");
615-
match (cast_in, cast_out) {
616-
(CastTy::Ptr(_) | CastTy::FnPtr, CastTy::Int(_)) => {
617-
// ptr-to-int casts are not possible in consts and thus not promotable
618-
return Err(Unpromotable);
619-
}
620-
_ => {}
612+
if let (CastTy::Ptr(_) | CastTy::FnPtr, CastTy::Int(_)) = (cast_in, cast_out) {
613+
// ptr-to-int casts are not possible in consts and thus not promotable
614+
return Err(Unpromotable);
621615
}
622616
}
623617

compiler/rustc_parse/src/lexer/tokentrees.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,9 @@ impl<'a> TokenTreesReader<'a> {
149149
}
150150
}
151151

152-
match (open_brace, delim) {
153-
//only add braces
154-
(DelimToken::Brace, DelimToken::Brace) => {
155-
self.matching_block_spans.push((open_brace_span, close_brace_span));
156-
}
157-
_ => {}
152+
//only add braces
153+
if let (DelimToken::Brace, DelimToken::Brace) = (open_brace, delim) {
154+
self.matching_block_spans.push((open_brace_span, close_brace_span));
158155
}
159156

160157
if self.open_braces.is_empty() {

compiler/rustc_parse_format/src/lib.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -525,12 +525,9 @@ impl<'a> Parser<'a> {
525525

526526
// fill character
527527
if let Some(&(_, c)) = self.cur.peek() {
528-
match self.cur.clone().nth(1) {
529-
Some((_, '>' | '<' | '^')) => {
530-
spec.fill = Some(c);
531-
self.cur.next();
532-
}
533-
_ => {}
528+
if let Some((_, '>' | '<' | '^')) = self.cur.clone().nth(1) {
529+
spec.fill = Some(c);
530+
self.cur.next();
534531
}
535532
}
536533
// Alignment

compiler/rustc_resolve/src/lib.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -534,11 +534,8 @@ impl<'a> ModuleData<'a> {
534534
if ns != TypeNS {
535535
return;
536536
}
537-
match binding.res() {
538-
Res::Def(DefKind::Trait | DefKind::TraitAlias, _) => {
539-
collected_traits.push((name, binding))
540-
}
541-
_ => (),
537+
if let Res::Def(DefKind::Trait | DefKind::TraitAlias, _) = binding.res() {
538+
collected_traits.push((name, binding))
542539
}
543540
});
544541
*traits = Some(collected_traits.into_boxed_slice());

compiler/rustc_symbol_mangling/src/v0.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,8 @@ impl SymbolMangler<'tcx> {
152152
let _ = write!(self.out, "{}", ident.len());
153153

154154
// Write a separating `_` if necessary (leading digit or `_`).
155-
match ident.chars().next() {
156-
Some('_' | '0'..='9') => {
157-
self.push("_");
158-
}
159-
_ => {}
155+
if let Some('_' | '0'..='9') = ident.chars().next() {
156+
self.push("_");
160157
}
161158

162159
self.push(ident);

0 commit comments

Comments
 (0)