Skip to content

Commit

Permalink
Auto merge of rust-lang#70326 - matthiaskrgr:cl1ppy_single_match, r=C…
Browse files Browse the repository at this point in the history
…entril

Use if let instead of match when only matching a single variant (clippy::single_match)

Makes code more compact and reduces nesting.
  • Loading branch information
bors committed Mar 30, 2020
2 parents 8926bb4 + 9bba047 commit a80ec3b
Show file tree
Hide file tree
Showing 48 changed files with 591 additions and 751 deletions.
17 changes: 7 additions & 10 deletions src/librustc_ast_lowering/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1332,17 +1332,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
self.resolver.definitions().as_local_node_id(def_id)
{
for param in &generics.params {
match param.kind {
GenericParamKind::Type { .. } => {
if node_id == param.id {
add_bounds
.entry(param.id)
.or_default()
.push(bound.clone());
continue 'next_bound;
}
if let GenericParamKind::Type { .. } = param.kind {
if node_id == param.id {
add_bounds
.entry(param.id)
.or_default()
.push(bound.clone());
continue 'next_bound;
}
_ => {}
}
}
}
Expand Down
14 changes: 6 additions & 8 deletions src/librustc_ast_passes/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,27 +516,25 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
}

fn visit_generic_param(&mut self, param: &'a GenericParam) {
match param.kind {
GenericParamKind::Const { .. } => gate_feature_post!(
if let GenericParamKind::Const { .. } = param.kind {
gate_feature_post!(
&self,
const_generics,
param.ident.span,
"const generics are unstable"
),
_ => {}
)
}
visit::walk_generic_param(self, param)
}

fn visit_assoc_ty_constraint(&mut self, constraint: &'a AssocTyConstraint) {
match constraint.kind {
AssocTyConstraintKind::Bound { .. } => gate_feature_post!(
if let AssocTyConstraintKind::Bound { .. } = constraint.kind {
gate_feature_post!(
&self,
associated_type_bounds,
constraint.span,
"associated type bounds are unstable"
),
_ => {}
)
}
visit::walk_assoc_ty_constraint(self, constraint)
}
Expand Down
31 changes: 12 additions & 19 deletions src/librustc_ast_pretty/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,9 +637,8 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
match tt {
TokenTree::Token(ref token) => {
self.word(token_to_string_ext(&token, convert_dollar_crate));
match token.kind {
token::DocComment(..) => self.hardbreak(),
_ => {}
if let token::DocComment(..) = token.kind {
self.hardbreak()
}
}
TokenTree::Delimited(dspan, delim, tts) => {
Expand Down Expand Up @@ -1390,13 +1389,10 @@ impl<'a> State<'a> {
self.print_visibility(&v.vis);
let generics = ast::Generics::default();
self.print_struct(&v.data, &generics, v.ident, v.span, false);
match v.disr_expr {
Some(ref d) => {
self.s.space();
self.word_space("=");
self.print_expr(&d.value)
}
_ => {}
if let Some(ref d) = v.disr_expr {
self.s.space();
self.word_space("=");
self.print_expr(&d.value)
}
}

Expand Down Expand Up @@ -2082,12 +2078,10 @@ impl<'a> State<'a> {
}
ast::ExprKind::Yield(ref e) => {
self.s.word("yield");
match *e {
Some(ref expr) => {
self.s.space();
self.print_expr_maybe_paren(expr, parser::PREC_JUMP);
}
_ => (),

if let Some(ref expr) = *e {
self.s.space();
self.print_expr_maybe_paren(expr, parser::PREC_JUMP);
}
}
ast::ExprKind::Try(ref e) => {
Expand Down Expand Up @@ -2139,9 +2133,8 @@ impl<'a> State<'a> {
self.s.word("::");
let item_segment = path.segments.last().unwrap();
self.print_ident(item_segment.ident);
match item_segment.args {
Some(ref args) => self.print_generic_args(args, colons_before_params),
None => {}
if let Some(ref args) = item_segment.args {
self.print_generic_args(args, colons_before_params)
}
}

Expand Down
12 changes: 4 additions & 8 deletions src/librustc_builtin_macros/format_foreign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,8 @@ pub mod printf {
}

pub fn set_position(&mut self, start: usize, end: usize) {
match self {
Substitution::Format(ref mut fmt) => {
fmt.position = InnerSpan::new(start, end);
}
_ => {}
if let Substitution::Format(ref mut fmt) = self {
fmt.position = InnerSpan::new(start, end);
}
}

Expand Down Expand Up @@ -311,9 +308,8 @@ pub mod printf {

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

Cur::new_at(&s[..], start)
Expand Down
18 changes: 8 additions & 10 deletions src/librustc_codegen_llvm/debuginfo/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1869,16 +1869,14 @@ fn prepare_enum_metadata(

let layout = cx.layout_of(enum_type);

match (&layout.abi, &layout.variants) {
(
&layout::Abi::Scalar(_),
&layout::Variants::Multiple {
discr_kind: layout::DiscriminantKind::Tag,
ref discr,
..
},
) => return FinalMetadata(discriminant_type_metadata(discr.value)),
_ => {}
if let (
&layout::Abi::Scalar(_),
&layout::Variants::Multiple {
discr_kind: layout::DiscriminantKind::Tag, ref discr, ..
},
) = (&layout.abi, &layout.variants)
{
return FinalMetadata(discriminant_type_metadata(discr.value));
}

if use_enum_fallback(cx) {
Expand Down
30 changes: 12 additions & 18 deletions src/librustc_driver/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,8 @@ impl<'hir> PrinterSupport for IdentifiedAnnotation<'hir> {

impl<'hir> pprust::PpAnn for IdentifiedAnnotation<'hir> {
fn pre(&self, s: &mut pprust::State<'_>, node: pprust::AnnNode<'_>) {
match node {
pprust::AnnNode::Expr(_) => s.popen(),
_ => {}
if let pprust::AnnNode::Expr(_) = node {
s.popen();
}
}
fn post(&self, s: &mut pprust::State<'_>, node: pprust::AnnNode<'_>) {
Expand Down Expand Up @@ -232,9 +231,8 @@ impl<'hir> pprust_hir::PpAnn for IdentifiedAnnotation<'hir> {
}
}
fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
match node {
pprust_hir::AnnNode::Expr(_) => s.popen(),
_ => {}
if let pprust_hir::AnnNode::Expr(_) = node {
s.popen();
}
}
fn post(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
Expand Down Expand Up @@ -339,21 +337,17 @@ impl<'a, 'tcx> pprust_hir::PpAnn for TypedAnnotation<'a, 'tcx> {
self.tables.set(old_tables);
}
fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
match node {
pprust_hir::AnnNode::Expr(_) => s.popen(),
_ => {}
if let pprust_hir::AnnNode::Expr(_) = node {
s.popen();
}
}
fn post(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
match node {
pprust_hir::AnnNode::Expr(expr) => {
s.s.space();
s.s.word("as");
s.s.space();
s.s.word(self.tables.get().expr_ty(expr).to_string());
s.pclose();
}
_ => {}
if let pprust_hir::AnnNode::Expr(expr) = node {
s.s.space();
s.s.word("as");
s.s.space();
s.s.word(self.tables.get().expr_ty(expr).to_string());
s.pclose();
}
}
}
Expand Down
38 changes: 16 additions & 22 deletions src/librustc_errors/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1719,40 +1719,36 @@ impl EmitterWriter {
if !self.short_message {
for child in children {
let span = child.render_span.as_ref().unwrap_or(&child.span);
match self.emit_message_default(
if let Err(err) = self.emit_message_default(
&span,
&child.styled_message(),
&None,
&child.level,
max_line_num_len,
true,
) {
Err(e) => panic!("failed to emit error: {}", e),
_ => (),
panic!("failed to emit error: {}", err);
}
}
for sugg in suggestions {
if sugg.style == SuggestionStyle::CompletelyHidden {
// do not display this suggestion, it is meant only for tools
} else if sugg.style == SuggestionStyle::HideCodeAlways {
match self.emit_message_default(
if let Err(e) = self.emit_message_default(
&MultiSpan::new(),
&[(sugg.msg.to_owned(), Style::HeaderMsg)],
&None,
&Level::Help,
max_line_num_len,
true,
) {
Err(e) => panic!("failed to emit error: {}", e),
_ => (),
panic!("failed to emit error: {}", e);
}
} else {
match self.emit_suggestion_default(sugg, &Level::Help, max_line_num_len)
{
Err(e) => panic!("failed to emit error: {}", e),
_ => (),
}
}
} else if let Err(e) =
self.emit_suggestion_default(sugg, &Level::Help, max_line_num_len)
{
panic!("failed to emit error: {}", e);
};
}
}
}
Expand All @@ -1762,10 +1758,11 @@ impl EmitterWriter {
let mut dst = self.dst.writable();
match writeln!(dst) {
Err(e) => panic!("failed to emit error: {}", e),
_ => match dst.flush() {
Err(e) => panic!("failed to emit error: {}", e),
_ => (),
},
_ => {
if let Err(e) = dst.flush() {
panic!("failed to emit error: {}", e)
}
}
}
}
}
Expand Down Expand Up @@ -2149,11 +2146,8 @@ impl<'a> Write for WritableDst<'a> {

impl<'a> Drop for WritableDst<'a> {
fn drop(&mut self) {
match *self {
WritableDst::Buffered(ref mut dst, ref mut buf) => {
drop(dst.print(buf));
}
_ => {}
if let WritableDst::Buffered(ref mut dst, ref mut buf) = self {
drop(dst.print(buf));
}
}
}
Expand Down
25 changes: 12 additions & 13 deletions src/librustc_expand/mbe/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,20 +354,19 @@ fn generic_extension<'cx>(
mbe::TokenTree::Delimited(_, ref delim) => &delim.tts[..],
_ => continue,
};
match parse_tt(&mut Cow::Borrowed(&parser_from_cx(sess, arg.clone())), lhs_tt) {
Success(_) => {
if comma_span.is_dummy() {
err.note("you might be missing a comma");
} else {
err.span_suggestion_short(
comma_span,
"missing comma here",
", ".to_string(),
Applicability::MachineApplicable,
);
}
if let Success(_) =
parse_tt(&mut Cow::Borrowed(&parser_from_cx(sess, arg.clone())), lhs_tt)
{
if comma_span.is_dummy() {
err.note("you might be missing a comma");
} else {
err.span_suggestion_short(
comma_span,
"missing comma here",
", ".to_string(),
Applicability::MachineApplicable,
);
}
_ => {}
}
}
}
Expand Down
25 changes: 9 additions & 16 deletions src/librustc_hir_pretty/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2036,13 +2036,10 @@ impl<'a> State<'a> {
}
GenericParamKind::Type { ref default, .. } => {
self.print_bounds(":", param.bounds);
match default {
Some(default) => {
self.s.space();
self.word_space("=");
self.print_type(&default)
}
_ => {}
if let Some(default) = default {
self.s.space();
self.word_space("=");
self.print_type(&default)
}
}
GenericParamKind::Const { ref ty } => {
Expand Down Expand Up @@ -2145,9 +2142,8 @@ impl<'a> State<'a> {
}
self.end();

match decl.output {
hir::FnRetTy::Return(ref output) => self.maybe_print_comment(output.span.lo()),
_ => {}
if let hir::FnRetTy::Return(ref output) = decl.output {
self.maybe_print_comment(output.span.lo())
}
}

Expand Down Expand Up @@ -2222,12 +2218,9 @@ impl<'a> State<'a> {
}

pub fn print_extern_opt_abi(&mut self, opt_abi: Option<Abi>) {
match opt_abi {
Some(abi) => {
self.word_nbsp("extern");
self.word_nbsp(abi.to_string())
}
None => {}
if let Some(abi) = opt_abi {
self.word_nbsp("extern");
self.word_nbsp(abi.to_string())
}
}

Expand Down
Loading

0 comments on commit a80ec3b

Please sign in to comment.