Skip to content

Commit a80ec3b

Browse files
committed
Auto merge of #70326 - matthiaskrgr:cl1ppy_single_match, r=Centril
Use if let instead of match when only matching a single variant (clippy::single_match) Makes code more compact and reduces nesting.
2 parents 8926bb4 + 9bba047 commit a80ec3b

File tree

48 files changed

+591
-751
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+591
-751
lines changed

src/librustc_ast_lowering/item.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -1332,17 +1332,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
13321332
self.resolver.definitions().as_local_node_id(def_id)
13331333
{
13341334
for param in &generics.params {
1335-
match param.kind {
1336-
GenericParamKind::Type { .. } => {
1337-
if node_id == param.id {
1338-
add_bounds
1339-
.entry(param.id)
1340-
.or_default()
1341-
.push(bound.clone());
1342-
continue 'next_bound;
1343-
}
1335+
if let GenericParamKind::Type { .. } = param.kind {
1336+
if node_id == param.id {
1337+
add_bounds
1338+
.entry(param.id)
1339+
.or_default()
1340+
.push(bound.clone());
1341+
continue 'next_bound;
13441342
}
1345-
_ => {}
13461343
}
13471344
}
13481345
}

src/librustc_ast_passes/feature_gate.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -516,27 +516,25 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
516516
}
517517

518518
fn visit_generic_param(&mut self, param: &'a GenericParam) {
519-
match param.kind {
520-
GenericParamKind::Const { .. } => gate_feature_post!(
519+
if let GenericParamKind::Const { .. } = param.kind {
520+
gate_feature_post!(
521521
&self,
522522
const_generics,
523523
param.ident.span,
524524
"const generics are unstable"
525-
),
526-
_ => {}
525+
)
527526
}
528527
visit::walk_generic_param(self, param)
529528
}
530529

531530
fn visit_assoc_ty_constraint(&mut self, constraint: &'a AssocTyConstraint) {
532-
match constraint.kind {
533-
AssocTyConstraintKind::Bound { .. } => gate_feature_post!(
531+
if let AssocTyConstraintKind::Bound { .. } = constraint.kind {
532+
gate_feature_post!(
534533
&self,
535534
associated_type_bounds,
536535
constraint.span,
537536
"associated type bounds are unstable"
538-
),
539-
_ => {}
537+
)
540538
}
541539
visit::walk_assoc_ty_constraint(self, constraint)
542540
}

src/librustc_ast_pretty/pprust.rs

+12-19
Original file line numberDiff line numberDiff line change
@@ -637,9 +637,8 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
637637
match tt {
638638
TokenTree::Token(ref token) => {
639639
self.word(token_to_string_ext(&token, convert_dollar_crate));
640-
match token.kind {
641-
token::DocComment(..) => self.hardbreak(),
642-
_ => {}
640+
if let token::DocComment(..) = token.kind {
641+
self.hardbreak()
643642
}
644643
}
645644
TokenTree::Delimited(dspan, delim, tts) => {
@@ -1390,13 +1389,10 @@ impl<'a> State<'a> {
13901389
self.print_visibility(&v.vis);
13911390
let generics = ast::Generics::default();
13921391
self.print_struct(&v.data, &generics, v.ident, v.span, false);
1393-
match v.disr_expr {
1394-
Some(ref d) => {
1395-
self.s.space();
1396-
self.word_space("=");
1397-
self.print_expr(&d.value)
1398-
}
1399-
_ => {}
1392+
if let Some(ref d) = v.disr_expr {
1393+
self.s.space();
1394+
self.word_space("=");
1395+
self.print_expr(&d.value)
14001396
}
14011397
}
14021398

@@ -2082,12 +2078,10 @@ impl<'a> State<'a> {
20822078
}
20832079
ast::ExprKind::Yield(ref e) => {
20842080
self.s.word("yield");
2085-
match *e {
2086-
Some(ref expr) => {
2087-
self.s.space();
2088-
self.print_expr_maybe_paren(expr, parser::PREC_JUMP);
2089-
}
2090-
_ => (),
2081+
2082+
if let Some(ref expr) = *e {
2083+
self.s.space();
2084+
self.print_expr_maybe_paren(expr, parser::PREC_JUMP);
20912085
}
20922086
}
20932087
ast::ExprKind::Try(ref e) => {
@@ -2139,9 +2133,8 @@ impl<'a> State<'a> {
21392133
self.s.word("::");
21402134
let item_segment = path.segments.last().unwrap();
21412135
self.print_ident(item_segment.ident);
2142-
match item_segment.args {
2143-
Some(ref args) => self.print_generic_args(args, colons_before_params),
2144-
None => {}
2136+
if let Some(ref args) = item_segment.args {
2137+
self.print_generic_args(args, colons_before_params)
21452138
}
21462139
}
21472140

src/librustc_builtin_macros/format_foreign.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,8 @@ pub mod printf {
2727
}
2828

2929
pub fn set_position(&mut self, start: usize, end: usize) {
30-
match self {
31-
Substitution::Format(ref mut fmt) => {
32-
fmt.position = InnerSpan::new(start, end);
33-
}
34-
_ => {}
30+
if let Substitution::Format(ref mut fmt) = self {
31+
fmt.position = InnerSpan::new(start, end);
3532
}
3633
}
3734

@@ -311,9 +308,8 @@ pub mod printf {
311308

312309
let at = {
313310
let start = s.find('%')?;
314-
match s[start + 1..].chars().next()? {
315-
'%' => return Some((Substitution::Escape, &s[start + 2..])),
316-
_ => { /* fall-through */ }
311+
if let '%' = s[start + 1..].chars().next()? {
312+
return Some((Substitution::Escape, &s[start + 2..]));
317313
}
318314

319315
Cur::new_at(&s[..], start)

src/librustc_codegen_llvm/debuginfo/metadata.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -1869,16 +1869,14 @@ fn prepare_enum_metadata(
18691869

18701870
let layout = cx.layout_of(enum_type);
18711871

1872-
match (&layout.abi, &layout.variants) {
1873-
(
1874-
&layout::Abi::Scalar(_),
1875-
&layout::Variants::Multiple {
1876-
discr_kind: layout::DiscriminantKind::Tag,
1877-
ref discr,
1878-
..
1879-
},
1880-
) => return FinalMetadata(discriminant_type_metadata(discr.value)),
1881-
_ => {}
1872+
if let (
1873+
&layout::Abi::Scalar(_),
1874+
&layout::Variants::Multiple {
1875+
discr_kind: layout::DiscriminantKind::Tag, ref discr, ..
1876+
},
1877+
) = (&layout.abi, &layout.variants)
1878+
{
1879+
return FinalMetadata(discriminant_type_metadata(discr.value));
18821880
}
18831881

18841882
if use_enum_fallback(cx) {

src/librustc_driver/pretty.rs

+12-18
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,8 @@ impl<'hir> PrinterSupport for IdentifiedAnnotation<'hir> {
177177

178178
impl<'hir> pprust::PpAnn for IdentifiedAnnotation<'hir> {
179179
fn pre(&self, s: &mut pprust::State<'_>, node: pprust::AnnNode<'_>) {
180-
match node {
181-
pprust::AnnNode::Expr(_) => s.popen(),
182-
_ => {}
180+
if let pprust::AnnNode::Expr(_) = node {
181+
s.popen();
183182
}
184183
}
185184
fn post(&self, s: &mut pprust::State<'_>, node: pprust::AnnNode<'_>) {
@@ -232,9 +231,8 @@ impl<'hir> pprust_hir::PpAnn for IdentifiedAnnotation<'hir> {
232231
}
233232
}
234233
fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
235-
match node {
236-
pprust_hir::AnnNode::Expr(_) => s.popen(),
237-
_ => {}
234+
if let pprust_hir::AnnNode::Expr(_) = node {
235+
s.popen();
238236
}
239237
}
240238
fn post(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
@@ -339,21 +337,17 @@ impl<'a, 'tcx> pprust_hir::PpAnn for TypedAnnotation<'a, 'tcx> {
339337
self.tables.set(old_tables);
340338
}
341339
fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
342-
match node {
343-
pprust_hir::AnnNode::Expr(_) => s.popen(),
344-
_ => {}
340+
if let pprust_hir::AnnNode::Expr(_) = node {
341+
s.popen();
345342
}
346343
}
347344
fn post(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
348-
match node {
349-
pprust_hir::AnnNode::Expr(expr) => {
350-
s.s.space();
351-
s.s.word("as");
352-
s.s.space();
353-
s.s.word(self.tables.get().expr_ty(expr).to_string());
354-
s.pclose();
355-
}
356-
_ => {}
345+
if let pprust_hir::AnnNode::Expr(expr) = node {
346+
s.s.space();
347+
s.s.word("as");
348+
s.s.space();
349+
s.s.word(self.tables.get().expr_ty(expr).to_string());
350+
s.pclose();
357351
}
358352
}
359353
}

src/librustc_errors/emitter.rs

+16-22
Original file line numberDiff line numberDiff line change
@@ -1719,40 +1719,36 @@ impl EmitterWriter {
17191719
if !self.short_message {
17201720
for child in children {
17211721
let span = child.render_span.as_ref().unwrap_or(&child.span);
1722-
match self.emit_message_default(
1722+
if let Err(err) = self.emit_message_default(
17231723
&span,
17241724
&child.styled_message(),
17251725
&None,
17261726
&child.level,
17271727
max_line_num_len,
17281728
true,
17291729
) {
1730-
Err(e) => panic!("failed to emit error: {}", e),
1731-
_ => (),
1730+
panic!("failed to emit error: {}", err);
17321731
}
17331732
}
17341733
for sugg in suggestions {
17351734
if sugg.style == SuggestionStyle::CompletelyHidden {
17361735
// do not display this suggestion, it is meant only for tools
17371736
} else if sugg.style == SuggestionStyle::HideCodeAlways {
1738-
match self.emit_message_default(
1737+
if let Err(e) = self.emit_message_default(
17391738
&MultiSpan::new(),
17401739
&[(sugg.msg.to_owned(), Style::HeaderMsg)],
17411740
&None,
17421741
&Level::Help,
17431742
max_line_num_len,
17441743
true,
17451744
) {
1746-
Err(e) => panic!("failed to emit error: {}", e),
1747-
_ => (),
1745+
panic!("failed to emit error: {}", e);
17481746
}
1749-
} else {
1750-
match self.emit_suggestion_default(sugg, &Level::Help, max_line_num_len)
1751-
{
1752-
Err(e) => panic!("failed to emit error: {}", e),
1753-
_ => (),
1754-
}
1755-
}
1747+
} else if let Err(e) =
1748+
self.emit_suggestion_default(sugg, &Level::Help, max_line_num_len)
1749+
{
1750+
panic!("failed to emit error: {}", e);
1751+
};
17561752
}
17571753
}
17581754
}
@@ -1762,10 +1758,11 @@ impl EmitterWriter {
17621758
let mut dst = self.dst.writable();
17631759
match writeln!(dst) {
17641760
Err(e) => panic!("failed to emit error: {}", e),
1765-
_ => match dst.flush() {
1766-
Err(e) => panic!("failed to emit error: {}", e),
1767-
_ => (),
1768-
},
1761+
_ => {
1762+
if let Err(e) = dst.flush() {
1763+
panic!("failed to emit error: {}", e)
1764+
}
1765+
}
17691766
}
17701767
}
17711768
}
@@ -2149,11 +2146,8 @@ impl<'a> Write for WritableDst<'a> {
21492146

21502147
impl<'a> Drop for WritableDst<'a> {
21512148
fn drop(&mut self) {
2152-
match *self {
2153-
WritableDst::Buffered(ref mut dst, ref mut buf) => {
2154-
drop(dst.print(buf));
2155-
}
2156-
_ => {}
2149+
if let WritableDst::Buffered(ref mut dst, ref mut buf) = self {
2150+
drop(dst.print(buf));
21572151
}
21582152
}
21592153
}

src/librustc_expand/mbe/macro_rules.rs

+12-13
Original file line numberDiff line numberDiff line change
@@ -354,20 +354,19 @@ fn generic_extension<'cx>(
354354
mbe::TokenTree::Delimited(_, ref delim) => &delim.tts[..],
355355
_ => continue,
356356
};
357-
match parse_tt(&mut Cow::Borrowed(&parser_from_cx(sess, arg.clone())), lhs_tt) {
358-
Success(_) => {
359-
if comma_span.is_dummy() {
360-
err.note("you might be missing a comma");
361-
} else {
362-
err.span_suggestion_short(
363-
comma_span,
364-
"missing comma here",
365-
", ".to_string(),
366-
Applicability::MachineApplicable,
367-
);
368-
}
357+
if let Success(_) =
358+
parse_tt(&mut Cow::Borrowed(&parser_from_cx(sess, arg.clone())), lhs_tt)
359+
{
360+
if comma_span.is_dummy() {
361+
err.note("you might be missing a comma");
362+
} else {
363+
err.span_suggestion_short(
364+
comma_span,
365+
"missing comma here",
366+
", ".to_string(),
367+
Applicability::MachineApplicable,
368+
);
369369
}
370-
_ => {}
371370
}
372371
}
373372
}

src/librustc_hir_pretty/lib.rs

+9-16
Original file line numberDiff line numberDiff line change
@@ -2036,13 +2036,10 @@ impl<'a> State<'a> {
20362036
}
20372037
GenericParamKind::Type { ref default, .. } => {
20382038
self.print_bounds(":", param.bounds);
2039-
match default {
2040-
Some(default) => {
2041-
self.s.space();
2042-
self.word_space("=");
2043-
self.print_type(&default)
2044-
}
2045-
_ => {}
2039+
if let Some(default) = default {
2040+
self.s.space();
2041+
self.word_space("=");
2042+
self.print_type(&default)
20462043
}
20472044
}
20482045
GenericParamKind::Const { ref ty } => {
@@ -2145,9 +2142,8 @@ impl<'a> State<'a> {
21452142
}
21462143
self.end();
21472144

2148-
match decl.output {
2149-
hir::FnRetTy::Return(ref output) => self.maybe_print_comment(output.span.lo()),
2150-
_ => {}
2145+
if let hir::FnRetTy::Return(ref output) = decl.output {
2146+
self.maybe_print_comment(output.span.lo())
21512147
}
21522148
}
21532149

@@ -2222,12 +2218,9 @@ impl<'a> State<'a> {
22222218
}
22232219

22242220
pub fn print_extern_opt_abi(&mut self, opt_abi: Option<Abi>) {
2225-
match opt_abi {
2226-
Some(abi) => {
2227-
self.word_nbsp("extern");
2228-
self.word_nbsp(abi.to_string())
2229-
}
2230-
None => {}
2221+
if let Some(abi) = opt_abi {
2222+
self.word_nbsp("extern");
2223+
self.word_nbsp(abi.to_string())
22312224
}
22322225
}
22332226

0 commit comments

Comments
 (0)