Skip to content

Commit 79267ce

Browse files
committed
Include single_match_else
1 parent d90bd98 commit 79267ce

File tree

7 files changed

+117
-146
lines changed

7 files changed

+117
-146
lines changed

bindgen-tests/tests/tests.rs

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -582,29 +582,28 @@ fn test_macro_fallback_non_system_dir() {
582582

583583
let actual = format_code(actual).unwrap();
584584

585-
let (expected_filename, expected) = match clang_version().parsed {
586-
Some((9, _)) => {
587-
let expected_filename = concat!(
588-
env!("CARGO_MANIFEST_DIR"),
589-
"/tests/expectations/tests/libclang-9/macro_fallback_non_system_dir.rs",
590-
);
591-
let expected = include_str!(concat!(
592-
env!("CARGO_MANIFEST_DIR"),
593-
"/tests/expectations/tests/libclang-9/macro_fallback_non_system_dir.rs",
594-
));
595-
(expected_filename, expected)
596-
}
597-
_ => {
598-
let expected_filename = concat!(
599-
env!("CARGO_MANIFEST_DIR"),
600-
"/tests/expectations/tests/test_macro_fallback_non_system_dir.rs",
601-
);
602-
let expected = include_str!(concat!(
603-
env!("CARGO_MANIFEST_DIR"),
604-
"/tests/expectations/tests/test_macro_fallback_non_system_dir.rs",
605-
));
606-
(expected_filename, expected)
607-
}
585+
let (expected_filename, expected) = if let Some((9, _)) =
586+
clang_version().parsed
587+
{
588+
let expected_filename = concat!(
589+
env!("CARGO_MANIFEST_DIR"),
590+
"/tests/expectations/tests/libclang-9/macro_fallback_non_system_dir.rs",
591+
);
592+
let expected = include_str!(concat!(
593+
env!("CARGO_MANIFEST_DIR"),
594+
"/tests/expectations/tests/libclang-9/macro_fallback_non_system_dir.rs",
595+
));
596+
(expected_filename, expected)
597+
} else {
598+
let expected_filename = concat!(
599+
env!("CARGO_MANIFEST_DIR"),
600+
"/tests/expectations/tests/test_macro_fallback_non_system_dir.rs",
601+
);
602+
let expected = include_str!(concat!(
603+
env!("CARGO_MANIFEST_DIR"),
604+
"/tests/expectations/tests/test_macro_fallback_non_system_dir.rs",
605+
));
606+
(expected_filename, expected)
608607
};
609608
let expected = format_code(expected).unwrap();
610609
if expected != actual {

bindgen/codegen/helpers.rs

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,10 @@ pub(crate) fn blob(layout: Layout) -> syn::Type {
8484
// some things that legitimately are more than 8-byte aligned.
8585
//
8686
// Eventually we should be able to `unwrap` here, but...
87-
let ty = match opaque.known_rust_type_for_array() {
88-
Some(ty) => ty,
89-
None => {
90-
warn!("Found unknown alignment on code generation!");
91-
syn::parse_quote! { u8 }
92-
}
93-
};
87+
let ty = opaque.known_rust_type_for_array().unwrap_or_else(|| {
88+
warn!("Found unknown alignment on code generation!");
89+
syn::parse_quote! { u8 }
90+
});
9491

9592
let data_len = opaque.array_size().unwrap_or(layout.size);
9693

@@ -245,24 +242,21 @@ pub(crate) mod ast_ty {
245242
(FloatKind::Float, false) => raw_type(ctx, "c_float"),
246243
(FloatKind::Double, false) => raw_type(ctx, "c_double"),
247244
(FloatKind::LongDouble, _) => {
248-
match layout {
249-
Some(layout) => {
250-
match layout.size {
251-
4 => syn::parse_quote! { f32 },
252-
8 => syn::parse_quote! { f64 },
253-
// TODO(emilio): If rust ever gains f128 we should
254-
// use it here and below.
255-
_ => super::integer_type(layout)
256-
.unwrap_or(syn::parse_quote! { f64 }),
257-
}
258-
}
259-
None => {
260-
debug_assert!(
261-
false,
262-
"How didn't we know the layout for a primitive type?"
263-
);
264-
syn::parse_quote! { f64 }
245+
if let Some(layout) = layout {
246+
match layout.size {
247+
4 => syn::parse_quote! { f32 },
248+
8 => syn::parse_quote! { f64 },
249+
// TODO(emilio): If rust ever gains f128 we should
250+
// use it here and below.
251+
_ => super::integer_type(layout)
252+
.unwrap_or(syn::parse_quote! { f64 }),
265253
}
254+
} else {
255+
debug_assert!(
256+
false,
257+
"How didn't we know the layout for a primitive type?"
258+
);
259+
syn::parse_quote! { f64 }
266260
}
267261
}
268262
(FloatKind::Float128, _) => {
@@ -365,17 +359,14 @@ pub(crate) mod ast_ty {
365359
signature
366360
.argument_types()
367361
.iter()
368-
.map(|&(ref name, _ty)| match *name {
369-
Some(ref name) => {
370-
let name = ctx.rust_ident(name);
371-
quote! { #name }
372-
}
373-
None => {
362+
.map(|&(ref name, _ty)| {
363+
let name = if let Some(ref name) = *name {
364+
ctx.rust_ident(name)
365+
} else {
374366
unnamed_arguments += 1;
375-
let name =
376-
ctx.rust_ident(format!("arg{unnamed_arguments}"));
377-
quote! { #name }
378-
}
367+
ctx.rust_ident(format!("arg{unnamed_arguments}"))
368+
};
369+
quote! { #name }
379370
})
380371
.collect()
381372
}

bindgen/codegen/mod.rs

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1940,16 +1940,14 @@ impl<'a> FieldCodegen<'a> for Bitfield {
19401940
let bitfield_ty_layout = bitfield_ty
19411941
.layout(ctx)
19421942
.expect("Bitfield without layout? Gah!");
1943-
let bitfield_int_ty = match helpers::integer_type(bitfield_ty_layout) {
1944-
Some(int_ty) => {
1943+
let bitfield_int_ty =
1944+
if let Some(int_ty) = helpers::integer_type(bitfield_ty_layout) {
19451945
*bitfield_representable_as_int = true;
19461946
int_ty
1947-
}
1948-
None => {
1947+
} else {
19491948
*bitfield_representable_as_int = false;
19501949
return;
1951-
}
1952-
};
1950+
};
19531951

19541952
let bitfield_ty =
19551953
bitfield_ty.to_rust_ty_or_opaque(ctx, bitfield_ty_item);
@@ -3559,18 +3557,17 @@ impl CodeGenerator for Enum {
35593557
// * the representation couldn't be determined from the C source
35603558
// * it was explicitly requested as a bindgen option
35613559

3562-
let kind = match repr {
3563-
Some(repr) => match *repr.canonical_type(ctx).kind() {
3560+
let kind = if let Some(repr) = repr {
3561+
match *repr.canonical_type(ctx).kind() {
35643562
TypeKind::Int(int_kind) => int_kind,
35653563
_ => panic!("Unexpected type as enum repr"),
3566-
},
3567-
None => {
3568-
warn!(
3569-
"Guessing type of enum! Forward declarations of enums \
3570-
shouldn't be legal!"
3571-
);
3572-
IntKind::Int
35733564
}
3565+
} else {
3566+
warn!(
3567+
"Guessing type of enum! Forward declarations of enums \
3568+
shouldn't be legal!"
3569+
);
3570+
IntKind::Int
35743571
};
35753572

35763573
let signed = kind.is_signed();
@@ -5680,12 +5677,11 @@ pub(crate) mod utils {
56805677
.map(|(name, ty)| {
56815678
let arg_ty = fnsig_argument_type(ctx, ty);
56825679

5683-
let arg_name = match *name {
5684-
Some(ref name) => ctx.rust_mangle(name).into_owned(),
5685-
None => {
5686-
unnamed_arguments += 1;
5687-
format!("arg{unnamed_arguments}")
5688-
}
5680+
let arg_name = if let Some(ref name) = *name {
5681+
ctx.rust_mangle(name).into_owned()
5682+
} else {
5683+
unnamed_arguments += 1;
5684+
format!("arg{unnamed_arguments}")
56895685
};
56905686

56915687
assert!(!arg_name.is_empty());
@@ -5724,12 +5720,11 @@ pub(crate) mod utils {
57245720
.argument_types()
57255721
.iter()
57265722
.map(|&(ref name, _ty)| {
5727-
let arg_name = match *name {
5728-
Some(ref name) => ctx.rust_mangle(name).into_owned(),
5729-
None => {
5730-
unnamed_arguments += 1;
5731-
format!("arg{unnamed_arguments}")
5732-
}
5723+
let arg_name = if let Some(ref name) = *name {
5724+
ctx.rust_mangle(name).into_owned()
5725+
} else {
5726+
unnamed_arguments += 1;
5727+
format!("arg{unnamed_arguments}")
57335728
};
57345729

57355730
assert!(!arg_name.is_empty());

bindgen/features.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -368,22 +368,21 @@ impl FromStr for RustTarget {
368368
));
369369
}
370370

371-
let (minor, patch) = match tail.split_once('.') {
372-
Some((minor_str, patch_str)) => {
373-
let Ok(minor) = minor_str.parse::<u64>() else {
374-
return Err(invalid_input(input, "the minor version number must be an unsigned 64-bit integer"));
375-
};
376-
let Ok(patch) = patch_str.parse::<u64>() else {
377-
return Err(invalid_input(input, "the patch version number must be an unsigned 64-bit integer"));
378-
};
379-
(minor, patch)
380-
}
381-
None => {
382-
let Ok(minor) = tail.parse::<u64>() else {
383-
return Err(invalid_input(input, "the minor version number must be an unsigned 64-bit integer"));
384-
};
385-
(minor, 0)
386-
}
371+
let (minor, patch) = if let Some((minor_str, patch_str)) =
372+
tail.split_once('.')
373+
{
374+
let Ok(minor) = minor_str.parse::<u64>() else {
375+
return Err(invalid_input(input, "the minor version number must be an unsigned 64-bit integer"));
376+
};
377+
let Ok(patch) = patch_str.parse::<u64>() else {
378+
return Err(invalid_input(input, "the patch version number must be an unsigned 64-bit integer"));
379+
};
380+
(minor, patch)
381+
} else {
382+
let Ok(minor) = tail.parse::<u64>() else {
383+
return Err(invalid_input(input, "the minor version number must be an unsigned 64-bit integer"));
384+
};
385+
(minor, 0)
387386
};
388387

389388
Self::stable(minor, patch).map_err(|err| invalid_input(input, err))

bindgen/ir/analysis/derive.rs

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -540,31 +540,25 @@ impl DeriveTrait {
540540
}
541541

542542
fn can_derive_vector(&self) -> CanDerive {
543-
match self {
544-
DeriveTrait::PartialEqOrPartialOrd => {
545-
// FIXME: vectors always can derive PartialEq, but they should
546-
// not derive PartialOrd:
547-
// https://github.com/rust-lang-nursery/packed_simd/issues/48
548-
trace!(" vectors cannot derive PartialOrd");
549-
CanDerive::No
550-
}
551-
_ => {
552-
trace!(" vector can derive {self}");
553-
CanDerive::Yes
554-
}
543+
if *self == DeriveTrait::PartialEqOrPartialOrd {
544+
// FIXME: vectors always can derive PartialEq, but they should
545+
// not derive PartialOrd:
546+
// https://github.com/rust-lang-nursery/packed_simd/issues/48
547+
trace!(" vectors cannot derive PartialOrd");
548+
CanDerive::No
549+
} else {
550+
trace!(" vector can derive {self}");
551+
CanDerive::Yes
555552
}
556553
}
557554

558555
fn can_derive_pointer(&self) -> CanDerive {
559-
match self {
560-
DeriveTrait::Default => {
561-
trace!(" pointer cannot derive Default");
562-
CanDerive::No
563-
}
564-
_ => {
565-
trace!(" pointer can derive {self}");
566-
CanDerive::Yes
567-
}
556+
if *self == DeriveTrait::Default {
557+
trace!(" pointer cannot derive Default");
558+
CanDerive::No
559+
} else {
560+
trace!(" pointer can derive {self}");
561+
CanDerive::Yes
568562
}
569563
}
570564

bindgen/ir/analysis/has_type_param_in_array.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -139,17 +139,14 @@ impl<'ctx> MonotoneFramework for HasTypeParameterInArray<'ctx> {
139139
TypeKind::Array(t, _) => {
140140
let inner_ty =
141141
self.ctx.resolve_type(t).canonical_type(self.ctx);
142-
match *inner_ty.kind() {
143-
TypeKind::TypeParam => {
144-
trace!(" Array with Named type has type parameter");
145-
self.insert(id)
146-
}
147-
_ => {
148-
trace!(
149-
" Array without Named type does have type parameter"
150-
);
151-
ConstrainResult::Same
152-
}
142+
if let TypeKind::TypeParam = *inner_ty.kind() {
143+
trace!(" Array with Named type has type parameter");
144+
self.insert(id)
145+
} else {
146+
trace!(
147+
" Array without Named type does have type parameter"
148+
);
149+
ConstrainResult::Same
153150
}
154151
}
155152

bindgen/ir/ty.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -865,20 +865,16 @@ impl Type {
865865
Some(location),
866866
ctx,
867867
);
868-
match complex {
869-
Ok(complex) => TypeKind::Comp(complex),
870-
Err(_) => {
871-
warn!(
872-
"Could not create complex type \
873-
from class template or base \
874-
specifier, using opaque blob"
875-
);
876-
let opaque =
877-
Opaque::from_clang_ty(ty, ctx);
878-
return Ok(ParseResult::New(
879-
opaque, None,
880-
));
881-
}
868+
if let Ok(complex) = complex {
869+
TypeKind::Comp(complex)
870+
} else {
871+
warn!(
872+
"Could not create complex type \
873+
from class template or base \
874+
specifier, using opaque blob"
875+
);
876+
let opaque = Opaque::from_clang_ty(ty, ctx);
877+
return Ok(ParseResult::New(opaque, None));
882878
}
883879
}
884880
CXCursor_TypeAliasTemplateDecl => {

0 commit comments

Comments
 (0)