From d90bd98ca730198b4a256f9b1291e71d1ce755d0 Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Sun, 1 Dec 2024 01:21:09 -0500 Subject: [PATCH] Fix manual_let_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 ``` --- bindgen-tests/build.rs | 6 ++--- bindgen-tests/tests/tests.rs | 7 ++--- bindgen/clang.rs | 4 +-- bindgen/codegen/impl_debug.rs | 7 +---- bindgen/codegen/mod.rs | 27 +++++++------------ bindgen/codegen/serialize.rs | 7 ++--- bindgen/codegen/struct_layout.rs | 5 ++-- bindgen/ir/analysis/has_float.rs | 9 +++---- .../ir/analysis/has_type_param_in_array.rs | 9 +++---- bindgen/ir/context.rs | 15 ++++------- bindgen/ir/item.rs | 15 +++++------ bindgen/ir/template.rs | 15 +++++------ bindgen/ir/ty.rs | 15 +++++------ bindgen/ir/var.rs | 11 ++++---- bindgen/regex_set.rs | 5 ++-- 15 files changed, 60 insertions(+), 97 deletions(-) diff --git a/bindgen-tests/build.rs b/bindgen-tests/build.rs index 713dbb3c57..69ffbba51d 100644 --- a/bindgen-tests/build.rs +++ b/bindgen-tests/build.rs @@ -12,10 +12,8 @@ pub fn main() { let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); let headers_dir = manifest_dir.join("tests").join("headers"); - let headers = match fs::read_dir(headers_dir) { - Ok(dir) => dir, - // We may not have headers directory after packaging. - Err(..) => return, + let Ok(headers) = fs::read_dir(headers_dir) else { + return; }; let entries = diff --git a/bindgen-tests/tests/tests.rs b/bindgen-tests/tests/tests.rs index bbe7eb2e24..09f22f335d 100644 --- a/bindgen-tests/tests/tests.rs +++ b/bindgen-tests/tests/tests.rs @@ -55,9 +55,10 @@ fn error_diff_mismatch( if let Some(var) = env::var_os("BINDGEN_TESTS_DIFFTOOL") { //usecase: var = "meld" -> You can hand check differences - let name = match filename.components().last() { - Some(std::path::Component::Normal(name)) => name, - _ => panic!("Why is the header variable so weird?"), + let Some(std::path::Component::Normal(name)) = + filename.components().last() + else { + panic!("Why is the header variable so weird?") }; let actual_result_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join(name); diff --git a/bindgen/clang.rs b/bindgen/clang.rs index bca4a80978..868d59446e 100644 --- a/bindgen/clang.rs +++ b/bindgen/clang.rs @@ -1886,9 +1886,7 @@ impl TranslationUnit { /// Save a translation unit to the given file. pub(crate) fn save(&mut self, file: &str) -> Result<(), CXSaveError> { - let file = if let Ok(cstring) = CString::new(file) { - cstring - } else { + let Ok(file) = CString::new(file) else { return Err(CXSaveError_Unknown); }; let ret = unsafe { diff --git a/bindgen/codegen/impl_debug.rs b/bindgen/codegen/impl_debug.rs index b0e73b6137..c4daddf260 100644 --- a/bindgen/codegen/impl_debug.rs +++ b/bindgen/codegen/impl_debug.rs @@ -126,12 +126,7 @@ impl<'a> ImplDebug<'a> for Item { return None; } - let ty = match self.as_type() { - Some(ty) => ty, - None => { - return None; - } - }; + let ty = self.as_type()?; fn debug_print( name: &str, diff --git a/bindgen/codegen/mod.rs b/bindgen/codegen/mod.rs index c58237c17b..cf08fafcff 100644 --- a/bindgen/codegen/mod.rs +++ b/bindgen/codegen/mod.rs @@ -1198,10 +1198,7 @@ impl CodeGenerator for Vtable<'_> { let function_item = ctx.resolve_item(m.signature()); let function = function_item.expect_function(); let signature_item = ctx.resolve_item(function.signature()); - let signature = match signature_item.expect_type().kind() { - TypeKind::Function(ref sig) => sig, - _ => panic!("Function signature type mismatch"), - }; + let TypeKind::Function(ref signature) = signature_item.expect_type().kind() else { panic!("Function signature type mismatch") }; // FIXME: Is there a canonical name without the class prepended? let function_name = function_item.canonical_name(ctx); @@ -2974,10 +2971,7 @@ impl Method { } let function = function_item.expect_function(); let times_seen = function.codegen(ctx, result, function_item); - let times_seen = match times_seen { - Some(seen) => seen, - None => return, - }; + let Some(times_seen) = times_seen else { return }; let signature_item = ctx.resolve_item(function.signature()); let mut name = match self.kind() { MethodKind::Constructor => "new".into(), @@ -2985,9 +2979,10 @@ impl Method { _ => function.name().to_owned(), }; - let signature = match *signature_item.expect_type().kind() { - TypeKind::Function(ref sig) => sig, - _ => panic!("How in the world?"), + let TypeKind::Function(ref signature) = + *signature_item.expect_type().kind() + else { + panic!("How in the world?") }; let supported_abi = signature.abi(ctx, Some(&*name)).is_ok(); @@ -4488,9 +4483,8 @@ impl CodeGenerator for Function { let signature_item = ctx.resolve_item(self.signature()); let signature = signature_item.kind().expect_type().canonical_type(ctx); - let signature = match *signature.kind() { - TypeKind::Function(ref sig) => sig, - _ => panic!("Signature kind is not a Function: {signature:?}"), + let TypeKind::Function(ref signature) = *signature.kind() else { + panic!("Signature kind is not a Function: {signature:?}") }; if is_internal { @@ -4966,9 +4960,8 @@ impl CodeGenerator for ObjCInterface { .expect_type() .kind(); - let parent = match parent { - TypeKind::ObjCInterface(ref parent) => parent, - _ => break, + let TypeKind::ObjCInterface(parent) = parent else { + break; }; parent_class = parent.parent_class; diff --git a/bindgen/codegen/serialize.rs b/bindgen/codegen/serialize.rs index 864d86b452..409e9584a7 100644 --- a/bindgen/codegen/serialize.rs +++ b/bindgen/codegen/serialize.rs @@ -72,9 +72,10 @@ impl<'a> CSerialize<'a> for Function { }); } - let signature = match ctx.resolve_type(self.signature()).kind() { - TypeKind::Function(signature) => signature, - _ => unreachable!(), + let TypeKind::Function(signature) = + ctx.resolve_type(self.signature()).kind() + else { + unreachable!() }; assert!(!signature.is_variadic()); diff --git a/bindgen/codegen/struct_layout.rs b/bindgen/codegen/struct_layout.rs index 40edefd540..0645d8a84a 100644 --- a/bindgen/codegen/struct_layout.rs +++ b/bindgen/codegen/struct_layout.rs @@ -421,9 +421,8 @@ impl<'a> StructLayoutTracker<'a> { return false; } - let layout = match self.latest_field_layout { - Some(l) => l, - None => return false, + let Some(layout) = self.latest_field_layout else { + return false; }; // If it was, we may or may not need to align, depending on what the diff --git a/bindgen/ir/analysis/has_float.rs b/bindgen/ir/analysis/has_float.rs index 630458e527..da4b413372 100644 --- a/bindgen/ir/analysis/has_float.rs +++ b/bindgen/ir/analysis/has_float.rs @@ -105,12 +105,9 @@ impl<'ctx> MonotoneFramework for HasFloat<'ctx> { } let item = self.ctx.resolve_item(id); - let ty = match item.as_type() { - Some(ty) => ty, - None => { - trace!(" not a type; ignoring"); - return ConstrainResult::Same; - } + let Some(ty) = item.as_type() else { + trace!(" not a type; ignoring"); + return ConstrainResult::Same; }; match *ty.kind() { diff --git a/bindgen/ir/analysis/has_type_param_in_array.rs b/bindgen/ir/analysis/has_type_param_in_array.rs index 61a8d631d1..fb07ea5bbe 100644 --- a/bindgen/ir/analysis/has_type_param_in_array.rs +++ b/bindgen/ir/analysis/has_type_param_in_array.rs @@ -108,12 +108,9 @@ impl<'ctx> MonotoneFramework for HasTypeParameterInArray<'ctx> { } let item = self.ctx.resolve_item(id); - let ty = match item.as_type() { - Some(ty) => ty, - None => { - trace!(" not a type; ignoring"); - return ConstrainResult::Same; - } + let Some(ty) = item.as_type() else { + trace!(" not a type; ignoring"); + return ConstrainResult::Same; }; match *ty.kind() { diff --git a/bindgen/ir/context.rs b/bindgen/ir/context.rs index d8bcaba1d4..22302bb1bd 100644 --- a/bindgen/ir/context.rs +++ b/bindgen/ir/context.rs @@ -930,10 +930,7 @@ If you encounter an error missing from this list, please file an issue or a PR!" for (id, item) in self.items() { let kind = item.kind(); - let ty = match kind.as_type() { - Some(ty) => ty, - None => continue, - }; + let Some(ty) = kind.as_type() else { continue }; if let TypeKind::UnresolvedTypeRef(ref ty, loc, parent_id) = *ty.kind() @@ -1070,9 +1067,8 @@ If you encounter an error missing from this list, please file an issue or a PR!" // Calls to `canonical_name` are expensive, so eagerly filter out // items that cannot be replaced. - let ty = match item.kind().as_type() { - Some(ty) => ty, - None => continue, + let Some(ty) = item.kind().as_type() else { + continue; }; match *ty.kind() { @@ -2524,9 +2520,8 @@ If you encounter an error missing from this list, please file an issue or a PR!" return false; } - let enum_ = match *ty.kind() { - TypeKind::Enum(ref e) => e, - _ => return false, + let TypeKind::Enum(ref enum_) = *ty.kind() else { + return false; }; if ty.name().is_some() { diff --git a/bindgen/ir/item.rs b/bindgen/ir/item.rs index ee0fdc525b..dbe6e2713d 100644 --- a/bindgen/ir/item.rs +++ b/bindgen/ir/item.rs @@ -582,9 +582,8 @@ impl Item { let mut parent = self.parent_id; loop { - let parent_item = match ctx.resolve_item_fallible(parent) { - Some(item) => item, - None => return false, + let Some(parent_item) = ctx.resolve_item_fallible(parent) else { + return false; }; if parent_item.id() == ctx.root_module() { @@ -978,9 +977,8 @@ impl Item { // Do not jump through aliases, except for aliases that point to a type // with the same name, since we dont generate coe for them. let item = self.id.into_resolver().through_type_refs().resolve(ctx); - let type_ = match *item.kind() { - ItemKind::Type(ref type_) => type_, - _ => return false, + let ItemKind::Type(ref type_) = *item.kind() else { + return false; }; match *type_.kind() { @@ -1077,9 +1075,8 @@ impl Item { /// Returns a prefix for the canonical name when C naming is enabled. fn c_naming_prefix(&self) -> Option<&str> { - let ty = match self.kind { - ItemKind::Type(ref ty) => ty, - _ => return None, + let ItemKind::Type(ref ty) = self.kind else { + return None; }; Some(match ty.kind() { diff --git a/bindgen/ir/template.rs b/bindgen/ir/template.rs index 59bd4bfde4..2783b414d8 100644 --- a/bindgen/ir/template.rs +++ b/bindgen/ir/template.rs @@ -266,17 +266,14 @@ impl TemplateInstantiation { }) }; - let definition = match definition { - Some(def) => def, - None => { - if !ty.declaration().is_builtin() { - warn!( - "Could not find template definition for template \ + let Some(definition) = definition else { + if !ty.declaration().is_builtin() { + warn!( + "Could not find template definition for template \ instantiation" - ); - } - return None; + ); } + return None; }; let template_definition = diff --git a/bindgen/ir/ty.rs b/bindgen/ir/ty.rs index fc3bfa6088..62b078e1e7 100644 --- a/bindgen/ir/ty.rs +++ b/bindgen/ir/ty.rs @@ -926,16 +926,13 @@ impl Type { CXChildVisit_Continue }); - let inner_type = match inner { - Ok(inner) => inner, - Err(..) => { - warn!( - "Failed to parse template alias \ + let Ok(inner_type) = inner else { + warn!( + "Failed to parse template alias \ {:?}", - location - ); - return Err(ParseError::Continue); - } + location + ); + return Err(ParseError::Continue); }; TypeKind::TemplateAlias(inner_type, args) diff --git a/bindgen/ir/var.rs b/bindgen/ir/var.rs index 2aa92f84bd..b63e9ea46d 100644 --- a/bindgen/ir/var.rs +++ b/bindgen/ir/var.rs @@ -198,9 +198,8 @@ impl ClangSubItemParser for Var { let value = parse_macro(ctx, &cursor); - let (id, value) = match value { - Some(v) => v, - None => return Err(ParseError::Continue), + let Some((id, value)) = value else { + return Err(ParseError::Continue); }; assert!(!id.is_empty(), "Empty macro name?"); @@ -338,9 +337,9 @@ impl ClangSubItemParser for Var { // to look at the canonical type of the pointee too, and check // is char, u8, or i8 I guess). let value = if is_integer { - let kind = match *canonical_ty.unwrap().kind() { - TypeKind::Int(kind) => kind, - _ => unreachable!(), + let TypeKind::Int(kind) = *canonical_ty.unwrap().kind() + else { + unreachable!() }; let mut val = cursor.evaluate().and_then(|v| v.as_int()); diff --git a/bindgen/regex_set.rs b/bindgen/regex_set.rs index be0041dcfa..dc6bded77a 100644 --- a/bindgen/regex_set.rs +++ b/bindgen/regex_set.rs @@ -115,9 +115,8 @@ impl RegexSet { S: AsRef, { let s = string.as_ref(); - let set = match self.set { - Some(ref set) => set, - None => return false, + let Some(ref set) = self.set else { + return false; }; if !self.record_matches {