Skip to content

Commit d90bd98

Browse files
committed
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 ```
1 parent 061cc5e commit d90bd98

File tree

15 files changed

+60
-97
lines changed

15 files changed

+60
-97
lines changed

bindgen-tests/build.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ 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,
17-
// We may not have headers directory after packaging.
18-
Err(..) => return,
15+
let Ok(headers) = fs::read_dir(headers_dir) else {
16+
return;
1917
};
2018

2119
let entries =

bindgen-tests/tests/tests.rs

Lines changed: 4 additions & 3 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);

bindgen/clang.rs

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

18871887
/// Save a translation unit to the given file.
18881888
pub(crate) fn save(&mut self, file: &str) -> Result<(), CXSaveError> {
1889-
let file = if let Ok(cstring) = CString::new(file) {
1890-
cstring
1891-
} else {
1889+
let Ok(file) = CString::new(file) else {
18921890
return Err(CXSaveError_Unknown);
18931891
};
18941892
let ret = unsafe {

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: 10 additions & 17 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);
@@ -2974,20 +2971,18 @@ impl Method {
29742971
}
29752972
let function = function_item.expect_function();
29762973
let times_seen = function.codegen(ctx, result, function_item);
2977-
let times_seen = match times_seen {
2978-
Some(seen) => seen,
2979-
None => return,
2980-
};
2974+
let Some(times_seen) = times_seen else { return };
29812975
let signature_item = ctx.resolve_item(function.signature());
29822976
let mut name = match self.kind() {
29832977
MethodKind::Constructor => "new".into(),
29842978
MethodKind::Destructor => "destruct".into(),
29852979
_ => function.name().to_owned(),
29862980
};
29872981

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

29932988
let supported_abi = signature.abi(ctx, Some(&*name)).is_ok();
@@ -4488,9 +4483,8 @@ impl CodeGenerator for Function {
44884483

44894484
let signature_item = ctx.resolve_item(self.signature());
44904485
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:?}"),
4486+
let TypeKind::Function(ref signature) = *signature.kind() else {
4487+
panic!("Signature kind is not a Function: {signature:?}")
44944488
};
44954489

44964490
if is_internal {
@@ -4966,9 +4960,8 @@ impl CodeGenerator for ObjCInterface {
49664960
.expect_type()
49674961
.kind();
49684962

4969-
let parent = match parent {
4970-
TypeKind::ObjCInterface(ref parent) => parent,
4971-
_ => break,
4963+
let TypeKind::ObjCInterface(parent) = parent else {
4964+
break;
49724965
};
49734966
parent_class = parent.parent_class;
49744967

bindgen/codegen/serialize.rs

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

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

8081
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/ir/analysis/has_float.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,9 @@ impl<'ctx> MonotoneFramework for HasFloat<'ctx> {
105105
}
106106

107107
let item = self.ctx.resolve_item(id);
108-
let ty = match item.as_type() {
109-
Some(ty) => ty,
110-
None => {
111-
trace!(" not a type; ignoring");
112-
return ConstrainResult::Same;
113-
}
108+
let Some(ty) = item.as_type() else {
109+
trace!(" not a type; ignoring");
110+
return ConstrainResult::Same;
114111
};
115112

116113
match *ty.kind() {

bindgen/ir/analysis/has_type_param_in_array.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,9 @@ impl<'ctx> MonotoneFramework for HasTypeParameterInArray<'ctx> {
108108
}
109109

110110
let item = self.ctx.resolve_item(id);
111-
let ty = match item.as_type() {
112-
Some(ty) => ty,
113-
None => {
114-
trace!(" not a type; ignoring");
115-
return ConstrainResult::Same;
116-
}
111+
let Some(ty) = item.as_type() else {
112+
trace!(" not a type; ignoring");
113+
return ConstrainResult::Same;
117114
};
118115

119116
match *ty.kind() {

bindgen/ir/context.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -930,10 +930,7 @@ If you encounter an error missing from this list, please file an issue or a PR!"
930930

931931
for (id, item) in self.items() {
932932
let kind = item.kind();
933-
let ty = match kind.as_type() {
934-
Some(ty) => ty,
935-
None => continue,
936-
};
933+
let Some(ty) = kind.as_type() else { continue };
937934

938935
if let TypeKind::UnresolvedTypeRef(ref ty, loc, parent_id) =
939936
*ty.kind()
@@ -1070,9 +1067,8 @@ If you encounter an error missing from this list, please file an issue or a PR!"
10701067

10711068
// Calls to `canonical_name` are expensive, so eagerly filter out
10721069
// items that cannot be replaced.
1073-
let ty = match item.kind().as_type() {
1074-
Some(ty) => ty,
1075-
None => continue,
1070+
let Some(ty) = item.kind().as_type() else {
1071+
continue;
10761072
};
10771073

10781074
match *ty.kind() {
@@ -2524,9 +2520,8 @@ If you encounter an error missing from this list, please file an issue or a PR!"
25242520
return false;
25252521
}
25262522

2527-
let enum_ = match *ty.kind() {
2528-
TypeKind::Enum(ref e) => e,
2529-
_ => return false,
2523+
let TypeKind::Enum(ref enum_) = *ty.kind() else {
2524+
return false;
25302525
};
25312526

25322527
if ty.name().is_some() {

0 commit comments

Comments
 (0)