Skip to content

Commit

Permalink
Fix manual_let_else lint
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
nyurik committed Dec 1, 2024
1 parent 061cc5e commit d90bd98
Show file tree
Hide file tree
Showing 15 changed files with 60 additions and 97 deletions.
6 changes: 2 additions & 4 deletions bindgen-tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
7 changes: 4 additions & 3 deletions bindgen-tests/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 1 addition & 3 deletions bindgen/clang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 1 addition & 6 deletions bindgen/codegen/impl_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
27 changes: 10 additions & 17 deletions bindgen/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -2974,20 +2971,18 @@ 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(),
MethodKind::Destructor => "destruct".into(),
_ => 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();
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;

Expand Down
7 changes: 4 additions & 3 deletions bindgen/codegen/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
5 changes: 2 additions & 3 deletions bindgen/codegen/struct_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 3 additions & 6 deletions bindgen/ir/analysis/has_float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
9 changes: 3 additions & 6 deletions bindgen/ir/analysis/has_type_param_in_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
15 changes: 5 additions & 10 deletions bindgen/ir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down
15 changes: 6 additions & 9 deletions bindgen/ir/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down
15 changes: 6 additions & 9 deletions bindgen/ir/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
15 changes: 6 additions & 9 deletions bindgen/ir/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 5 additions & 6 deletions bindgen/ir/var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?");
Expand Down Expand Up @@ -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());
Expand Down
5 changes: 2 additions & 3 deletions bindgen/regex_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,8 @@ impl RegexSet {
S: AsRef<str>,
{
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 {
Expand Down

0 comments on commit d90bd98

Please sign in to comment.