Skip to content

Commit 0c3ae5c

Browse files
nyurikpvdrz
authored andcommitted
Fix manual_let_else and single_match_else lint
Used this to find issues, apply suggestions, and manually fixed a clippy bug ```bash cargo clippy --all-targets --workspace --exclude bindgen-integration --exclude tests_expectations -- -W clippy::manual_let_else -W clippy::single_match_else ```
1 parent 8f21aa6 commit 0c3ae5c

18 files changed

+177
-242
lines changed

bindgen-tests/build.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ pub fn main() {
1212
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
1313
let headers_dir = manifest_dir.join("tests").join("headers");
1414

15-
let headers = match fs::read_dir(headers_dir) {
16-
Ok(dir) => dir,
15+
let Ok(headers) = fs::read_dir(headers_dir) else {
1716
// We may not have headers directory after packaging.
18-
Err(..) => return,
17+
return;
1918
};
2019

2120
let entries =

bindgen-tests/tests/tests.rs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ fn error_diff_mismatch(
5555

5656
if let Some(var) = env::var_os("BINDGEN_TESTS_DIFFTOOL") {
5757
//usecase: var = "meld" -> You can hand check differences
58-
let name = match filename.components().last() {
59-
Some(std::path::Component::Normal(name)) => name,
60-
_ => panic!("Why is the header variable so weird?"),
58+
let Some(std::path::Component::Normal(name)) =
59+
filename.components().last()
60+
else {
61+
panic!("Why is the header variable so weird?")
6162
};
6263
let actual_result_path =
6364
PathBuf::from(env::var("OUT_DIR").unwrap()).join(name);
@@ -581,29 +582,28 @@ fn test_macro_fallback_non_system_dir() {
581582

582583
let actual = format_code(actual).unwrap();
583584

584-
let (expected_filename, expected) = match clang_version().parsed {
585-
Some((9, _)) => {
586-
let expected_filename = concat!(
587-
env!("CARGO_MANIFEST_DIR"),
588-
"/tests/expectations/tests/libclang-9/macro_fallback_non_system_dir.rs",
589-
);
590-
let expected = include_str!(concat!(
591-
env!("CARGO_MANIFEST_DIR"),
592-
"/tests/expectations/tests/libclang-9/macro_fallback_non_system_dir.rs",
593-
));
594-
(expected_filename, expected)
595-
}
596-
_ => {
597-
let expected_filename = concat!(
598-
env!("CARGO_MANIFEST_DIR"),
599-
"/tests/expectations/tests/test_macro_fallback_non_system_dir.rs",
600-
);
601-
let expected = include_str!(concat!(
602-
env!("CARGO_MANIFEST_DIR"),
603-
"/tests/expectations/tests/test_macro_fallback_non_system_dir.rs",
604-
));
605-
(expected_filename, expected)
606-
}
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)
607607
};
608608
let expected = format_code(expected).unwrap();
609609
if expected != actual {

bindgen/clang.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1882,9 +1882,7 @@ impl TranslationUnit {
18821882

18831883
/// Save a translation unit to the given file.
18841884
pub(crate) fn save(&mut self, file: &str) -> Result<(), CXSaveError> {
1885-
let file = if let Ok(cstring) = CString::new(file) {
1886-
cstring
1887-
} else {
1885+
let Ok(file) = CString::new(file) else {
18881886
return Err(CXSaveError_Unknown);
18891887
};
18901888
let ret = unsafe {

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/impl_debug.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,7 @@ impl<'a> ImplDebug<'a> for Item {
126126
return None;
127127
}
128128

129-
let ty = match self.as_type() {
130-
Some(ty) => ty,
131-
None => {
132-
return None;
133-
}
134-
};
129+
let ty = self.as_type()?;
135130

136131
fn debug_print(
137132
name: &str,

bindgen/codegen/mod.rs

Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,10 +1198,7 @@ impl CodeGenerator for Vtable<'_> {
11981198
let function_item = ctx.resolve_item(m.signature());
11991199
let function = function_item.expect_function();
12001200
let signature_item = ctx.resolve_item(function.signature());
1201-
let signature = match signature_item.expect_type().kind() {
1202-
TypeKind::Function(ref sig) => sig,
1203-
_ => panic!("Function signature type mismatch"),
1204-
};
1201+
let TypeKind::Function(ref signature) = signature_item.expect_type().kind() else { panic!("Function signature type mismatch") };
12051202

12061203
// FIXME: Is there a canonical name without the class prepended?
12071204
let function_name = function_item.canonical_name(ctx);
@@ -1943,16 +1940,14 @@ impl<'a> FieldCodegen<'a> for Bitfield {
19431940
let bitfield_ty_layout = bitfield_ty
19441941
.layout(ctx)
19451942
.expect("Bitfield without layout? Gah!");
1946-
let bitfield_int_ty = match helpers::integer_type(bitfield_ty_layout) {
1947-
Some(int_ty) => {
1943+
let bitfield_int_ty =
1944+
if let Some(int_ty) = helpers::integer_type(bitfield_ty_layout) {
19481945
*bitfield_representable_as_int = true;
19491946
int_ty
1950-
}
1951-
None => {
1947+
} else {
19521948
*bitfield_representable_as_int = false;
19531949
return;
1954-
}
1955-
};
1950+
};
19561951

19571952
let bitfield_ty =
19581953
bitfield_ty.to_rust_ty_or_opaque(ctx, bitfield_ty_item);
@@ -2974,20 +2969,18 @@ impl Method {
29742969
}
29752970
let function = function_item.expect_function();
29762971
let times_seen = function.codegen(ctx, result, function_item);
2977-
let times_seen = match times_seen {
2978-
Some(seen) => seen,
2979-
None => return,
2980-
};
2972+
let Some(times_seen) = times_seen else { return };
29812973
let signature_item = ctx.resolve_item(function.signature());
29822974
let mut name = match self.kind() {
29832975
MethodKind::Constructor => "new".into(),
29842976
MethodKind::Destructor => "destruct".into(),
29852977
_ => function.name().to_owned(),
29862978
};
29872979

2988-
let signature = match *signature_item.expect_type().kind() {
2989-
TypeKind::Function(ref sig) => sig,
2990-
_ => panic!("How in the world?"),
2980+
let TypeKind::Function(ref signature) =
2981+
*signature_item.expect_type().kind()
2982+
else {
2983+
panic!("How in the world?")
29912984
};
29922985

29932986
let supported_abi = signature.abi(ctx, Some(&*name)).is_ok();
@@ -3564,18 +3557,17 @@ impl CodeGenerator for Enum {
35643557
// * the representation couldn't be determined from the C source
35653558
// * it was explicitly requested as a bindgen option
35663559

3567-
let kind = match repr {
3568-
Some(repr) => match *repr.canonical_type(ctx).kind() {
3560+
let kind = if let Some(repr) = repr {
3561+
match *repr.canonical_type(ctx).kind() {
35693562
TypeKind::Int(int_kind) => int_kind,
35703563
_ => panic!("Unexpected type as enum repr"),
3571-
},
3572-
None => {
3573-
warn!(
3574-
"Guessing type of enum! Forward declarations of enums \
3575-
shouldn't be legal!"
3576-
);
3577-
IntKind::Int
35783564
}
3565+
} else {
3566+
warn!(
3567+
"Guessing type of enum! Forward declarations of enums \
3568+
shouldn't be legal!"
3569+
);
3570+
IntKind::Int
35793571
};
35803572

35813573
let signed = kind.is_signed();
@@ -4488,9 +4480,8 @@ impl CodeGenerator for Function {
44884480

44894481
let signature_item = ctx.resolve_item(self.signature());
44904482
let signature = signature_item.kind().expect_type().canonical_type(ctx);
4491-
let signature = match *signature.kind() {
4492-
TypeKind::Function(ref sig) => sig,
4493-
_ => panic!("Signature kind is not a Function: {signature:?}"),
4483+
let TypeKind::Function(ref signature) = *signature.kind() else {
4484+
panic!("Signature kind is not a Function: {signature:?}")
44944485
};
44954486

44964487
if is_internal {
@@ -4966,9 +4957,8 @@ impl CodeGenerator for ObjCInterface {
49664957
.expect_type()
49674958
.kind();
49684959

4969-
let parent = match parent {
4970-
TypeKind::ObjCInterface(ref parent) => parent,
4971-
_ => break,
4960+
let TypeKind::ObjCInterface(parent) = parent else {
4961+
break;
49724962
};
49734963
parent_class = parent.parent_class;
49744964

@@ -5683,12 +5673,11 @@ pub(crate) mod utils {
56835673
.map(|(name, ty)| {
56845674
let arg_ty = fnsig_argument_type(ctx, ty);
56855675

5686-
let arg_name = match *name {
5687-
Some(ref name) => ctx.rust_mangle(name).into_owned(),
5688-
None => {
5689-
unnamed_arguments += 1;
5690-
format!("arg{unnamed_arguments}")
5691-
}
5676+
let arg_name = if let Some(ref name) = *name {
5677+
ctx.rust_mangle(name).into_owned()
5678+
} else {
5679+
unnamed_arguments += 1;
5680+
format!("arg{unnamed_arguments}")
56925681
};
56935682

56945683
assert!(!arg_name.is_empty());
@@ -5727,12 +5716,11 @@ pub(crate) mod utils {
57275716
.argument_types()
57285717
.iter()
57295718
.map(|&(ref name, _ty)| {
5730-
let arg_name = match *name {
5731-
Some(ref name) => ctx.rust_mangle(name).into_owned(),
5732-
None => {
5733-
unnamed_arguments += 1;
5734-
format!("arg{unnamed_arguments}")
5735-
}
5719+
let arg_name = if let Some(ref name) = *name {
5720+
ctx.rust_mangle(name).into_owned()
5721+
} else {
5722+
unnamed_arguments += 1;
5723+
format!("arg{unnamed_arguments}")
57365724
};
57375725

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

bindgen/codegen/serialize.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,10 @@ impl<'a> CSerialize<'a> for Function {
7171
});
7272
}
7373

74-
let signature = match ctx.resolve_type(self.signature()).kind() {
75-
TypeKind::Function(signature) => signature,
76-
_ => unreachable!(),
74+
let TypeKind::Function(signature) =
75+
ctx.resolve_type(self.signature()).kind()
76+
else {
77+
unreachable!()
7778
};
7879

7980
assert!(!signature.is_variadic());

bindgen/codegen/struct_layout.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,9 +421,8 @@ impl<'a> StructLayoutTracker<'a> {
421421
return false;
422422
}
423423

424-
let layout = match self.latest_field_layout {
425-
Some(l) => l,
426-
None => return false,
424+
let Some(layout) = self.latest_field_layout else {
425+
return false;
427426
};
428427

429428
// If it was, we may or may not need to align, depending on what the

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

0 commit comments

Comments
 (0)