diff options
-rw-r--r-- | src/clang.rs | 64 | ||||
-rw-r--r-- | src/codegen/helpers.rs | 6 | ||||
-rw-r--r-- | src/codegen/impl_debug.rs | 37 | ||||
-rw-r--r-- | src/codegen/impl_partialeq.rs | 5 | ||||
-rw-r--r-- | src/codegen/mod.rs | 89 | ||||
-rw-r--r-- | src/codegen/struct_layout.rs | 2 | ||||
-rw-r--r-- | src/ir/analysis/derive.rs | 75 | ||||
-rw-r--r-- | src/ir/analysis/has_destructor.rs | 16 | ||||
-rw-r--r-- | src/ir/analysis/has_vtable.rs | 14 | ||||
-rw-r--r-- | src/ir/analysis/mod.rs | 4 | ||||
-rw-r--r-- | src/ir/analysis/sizedness.rs | 20 | ||||
-rw-r--r-- | src/ir/analysis/template_params.rs | 86 | ||||
-rw-r--r-- | src/ir/annotations.rs | 22 | ||||
-rw-r--r-- | src/ir/comment.rs | 6 | ||||
-rw-r--r-- | src/ir/comp.rs | 101 | ||||
-rw-r--r-- | src/ir/context.rs | 196 | ||||
-rw-r--r-- | src/ir/enum_ty.rs | 4 | ||||
-rw-r--r-- | src/ir/function.rs | 22 | ||||
-rw-r--r-- | src/ir/item.rs | 62 | ||||
-rw-r--r-- | src/ir/layout.rs | 2 | ||||
-rw-r--r-- | src/ir/module.rs | 6 | ||||
-rw-r--r-- | src/ir/objc.rs | 69 | ||||
-rw-r--r-- | src/ir/template.rs | 4 | ||||
-rw-r--r-- | src/ir/traversal.rs | 14 | ||||
-rw-r--r-- | src/ir/ty.rs | 120 | ||||
-rw-r--r-- | src/ir/var.rs | 11 | ||||
-rw-r--r-- | src/lib.rs | 64 | ||||
-rw-r--r-- | src/main.rs | 6 | ||||
-rw-r--r-- | src/options.rs | 2 | ||||
-rwxr-xr-x | tests/stylo_sanity.rs | 8 |
30 files changed, 523 insertions, 614 deletions
diff --git a/src/clang.rs b/src/clang.rs index db6467e3..d50d9030 100644 --- a/src/clang.rs +++ b/src/clang.rs @@ -4,9 +4,7 @@ #![allow(non_upper_case_globals, dead_code)] use crate::ir::context::BindgenContext; -use cexpr; use clang_sys::*; -use regex; use std::ffi::{CStr, CString}; use std::fmt; use std::hash::Hash; @@ -85,7 +83,7 @@ impl Cursor { let mut result = Vec::with_capacity(count); for i in 0..count { - let string_ptr = (*manglings).Strings.offset(i as isize); + let string_ptr = (*manglings).Strings.add(i); result.push(cxstring_to_string_leaky(*string_ptr)); } clang_disposeStringSet(manglings); @@ -223,12 +221,12 @@ impl Cursor { /// not tracking the type declaration but the location of the cursor, given /// clang doesn't expose a proper declaration for these types. pub fn is_template_like(&self) -> bool { - match self.kind() { + matches!( + self.kind(), CXCursor_ClassTemplate | - CXCursor_ClassTemplatePartialSpecialization | - CXCursor_TypeAliasTemplateDecl => true, - _ => false, - } + CXCursor_ClassTemplatePartialSpecialization | + CXCursor_TypeAliasTemplateDecl + ) } /// Is this Cursor pointing to a function-like macro definition? @@ -275,7 +273,7 @@ impl Cursor { return parent.is_in_non_fully_specialized_template(); } - return true; + true } /// Is this cursor pointing a valid referent? @@ -900,7 +898,7 @@ extern "C" fn visit_children<Visitor>( where Visitor: FnMut(Cursor) -> CXChildVisitResult, { - let func: &mut Visitor = unsafe { mem::transmute(data) }; + let func: &mut Visitor = unsafe { &mut *(data as *mut Visitor) }; let child = Cursor { x: cur }; (*func)(child) @@ -1027,7 +1025,7 @@ impl Type { let s = unsafe { cxstring_into_string(clang_getTypeSpelling(self.x)) }; // Clang 5.0 introduced changes in the spelling API so it returned the // full qualified name. Let's undo that here. - if s.split("::").all(|s| is_valid_identifier(s)) { + if s.split("::").all(is_valid_identifier) { if let Some(s) = s.split("::").last() { return s.to_owned(); } @@ -1055,7 +1053,7 @@ impl Type { ctx.target_pointer_size() as c_longlong } // Work-around https://bugs.llvm.org/show_bug.cgi?id=40813 - CXType_Auto if self.is_non_deductible_auto_type() => return -6, + CXType_Auto if self.is_non_deductible_auto_type() => -6, _ => unsafe { clang_Type_getSizeOf(self.x) }, } } @@ -1068,7 +1066,7 @@ impl Type { ctx.target_pointer_size() as c_longlong } // Work-around https://bugs.llvm.org/show_bug.cgi?id=40813 - CXType_Auto if self.is_non_deductible_auto_type() => return -6, + CXType_Auto if self.is_non_deductible_auto_type() => -6, _ => unsafe { clang_Type_getAlignOf(self.x) }, } } @@ -1286,12 +1284,12 @@ impl Type { // nasty... But can happen in <type_traits>. Unfortunately I couldn't // reduce it enough :( self.template_args().map_or(false, |args| args.len() > 0) && - match self.declaration().kind() { + !matches!( + self.declaration().kind(), CXCursor_ClassTemplatePartialSpecialization | - CXCursor_TypeAliasTemplateDecl | - CXCursor_TemplateTemplateParameter => false, - _ => true, - } + CXCursor_TypeAliasTemplateDecl | + CXCursor_TemplateTemplateParameter + ) } /// Is this type an associated template type? Eg `T::Associated` in @@ -1704,11 +1702,7 @@ impl UnsavedFile { Contents: contents.as_ptr(), Length: contents.as_bytes().len() as c_ulong, }; - UnsavedFile { - x: x, - name: name, - contents: contents, - } + UnsavedFile { x, name, contents } } } @@ -1819,7 +1813,7 @@ pub fn ast_dump(c: &Cursor, depth: isize) -> CXChildVisitResult { if let Some(refd) = c.referenced() { if refd != *c { - println!(""); + println!(); print_cursor( depth, String::from(prefix) + "referenced.", @@ -1830,7 +1824,7 @@ pub fn ast_dump(c: &Cursor, depth: isize) -> CXChildVisitResult { let canonical = c.canonical(); if canonical != *c { - println!(""); + println!(); print_cursor( depth, String::from(prefix) + "canonical.", @@ -1840,7 +1834,7 @@ pub fn ast_dump(c: &Cursor, depth: isize) -> CXChildVisitResult { if let Some(specialized) = c.specialized() { if specialized != *c { - println!(""); + println!(); print_cursor( depth, String::from(prefix) + "specialized.", @@ -1850,7 +1844,7 @@ pub fn ast_dump(c: &Cursor, depth: isize) -> CXChildVisitResult { } if let Some(parent) = c.fallible_semantic_parent() { - println!(""); + println!(); print_cursor( depth, String::from(prefix) + "semantic-parent.", @@ -1898,34 +1892,34 @@ pub fn ast_dump(c: &Cursor, depth: isize) -> CXChildVisitResult { let canonical = ty.canonical_type(); if canonical != *ty { - println!(""); + println!(); print_type(depth, String::from(prefix) + "canonical.", &canonical); } if let Some(pointee) = ty.pointee_type() { if pointee != *ty { - println!(""); + println!(); print_type(depth, String::from(prefix) + "pointee.", &pointee); } } if let Some(elem) = ty.elem_type() { if elem != *ty { - println!(""); + println!(); print_type(depth, String::from(prefix) + "elements.", &elem); } } if let Some(ret) = ty.ret_type() { if ret != *ty { - println!(""); + println!(); print_type(depth, String::from(prefix) + "return.", &ret); } } let named = ty.named(); if named != *ty && named.is_valid() { - println!(""); + println!(); print_type(depth, String::from(prefix) + "named.", &named); } } @@ -1933,13 +1927,13 @@ pub fn ast_dump(c: &Cursor, depth: isize) -> CXChildVisitResult { print_indent(depth, "("); print_cursor(depth, "", c); - println!(""); + println!(); let ty = c.cur_type(); print_type(depth, "type.", &ty); let declaration = ty.declaration(); if declaration != *c && declaration.kind() != CXCursor_NoDeclFound { - println!(""); + println!(); print_cursor(depth, "type.declaration.", &declaration); } @@ -1947,7 +1941,7 @@ pub fn ast_dump(c: &Cursor, depth: isize) -> CXChildVisitResult { let mut found_children = false; c.visit(|s| { if !found_children { - println!(""); + println!(); found_children = true; } ast_dump(&s, depth + 1) diff --git a/src/codegen/helpers.rs b/src/codegen/helpers.rs index 205995bc..2ce6894f 100644 --- a/src/codegen/helpers.rs +++ b/src/codegen/helpers.rs @@ -230,14 +230,14 @@ pub mod ast_ty { } pub fn byte_array_expr(bytes: &[u8]) -> TokenStream { - let mut bytes: Vec<_> = bytes.iter().cloned().collect(); + let mut bytes: Vec<_> = bytes.to_vec(); bytes.push(0); quote! { [ #(#bytes),* ] } } pub fn cstr_expr(mut string: String) -> TokenStream { string.push('\0'); - let b = proc_macro2::Literal::byte_string(&string.as_bytes()); + let b = proc_macro2::Literal::byte_string(string.as_bytes()); quote! { #b } @@ -271,7 +271,7 @@ pub mod ast_ty { } warn!("Unknown non-finite float number: {:?}", f); - return Err(()); + Err(()) } pub fn arguments_from_signature( diff --git a/src/codegen/impl_debug.rs b/src/codegen/impl_debug.rs index 661711e8..0e2cd33a 100644 --- a/src/codegen/impl_debug.rs +++ b/src/codegen/impl_debug.rs @@ -2,7 +2,6 @@ use crate::ir::comp::{BitfieldUnit, CompKind, Field, FieldData, FieldMethods}; use crate::ir::context::BindgenContext; use crate::ir::item::{HasTypeParamInArray, IsOpaque, Item, ItemCanonicalName}; use crate::ir::ty::{TypeKind, RUST_DERIVE_IN_ARRAY_LIMIT}; -use proc_macro2; pub fn gen_debug_impl( ctx: &BindgenContext, @@ -23,8 +22,8 @@ pub fn gen_debug_impl( } CompKind::Struct => { let processed_fields = fields.iter().filter_map(|f| match f { - &Field::DataMember(ref fd) => fd.impl_debug(ctx, ()), - &Field::Bitfields(ref bu) => bu.impl_debug(ctx, ()), + Field::DataMember(ref fd) => fd.impl_debug(ctx, ()), + Field::Bitfields(ref bu) => bu.impl_debug(ctx, ()), }); for (i, (fstring, toks)) in processed_fields.enumerate() { @@ -186,24 +185,22 @@ impl<'a> ImplDebug<'a> for Item { { // The simple case debug_print(name, quote! { #name_ident }) + } else if ctx.options().use_core { + // There is no String in core; reducing field visibility to avoid breaking + // no_std setups. + Some((format!("{}: [...]", name), vec![])) } else { - if ctx.options().use_core { - // There is no String in core; reducing field visibility to avoid breaking - // no_std setups. - Some((format!("{}: [...]", name), vec![])) - } else { - // Let's implement our own print function - Some(( - format!("{}: [{{}}]", name), - vec![quote! { - self.#name_ident - .iter() - .enumerate() - .map(|(i, v)| format!("{}{:?}", if i > 0 { ", " } else { "" }, v)) - .collect::<String>() - }], - )) - } + // Let's implement our own print function + Some(( + format!("{}: [{{}}]", name), + vec![quote! { + self.#name_ident + .iter() + .enumerate() + .map(|(i, v)| format!("{}{:?}", if i > 0 { ", " } else { "" }, v)) + .collect::<String>() + }], + )) } } TypeKind::Vector(_, len) => { diff --git a/src/codegen/impl_partialeq.rs b/src/codegen/impl_partialeq.rs index 5a1ba3fe..960306ff 100644 --- a/src/codegen/impl_partialeq.rs +++ b/src/codegen/impl_partialeq.rs @@ -2,7 +2,6 @@ use crate::ir::comp::{CompInfo, CompKind, Field, FieldMethods}; use crate::ir::context::BindgenContext; use crate::ir::item::{IsOpaque, Item}; use crate::ir::ty::{TypeKind, RUST_DERIVE_IN_ARRAY_LIMIT}; -use proc_macro2; /// Generate a manual implementation of `PartialEq` trait for the /// specified compound type. @@ -51,7 +50,7 @@ pub fn gen_partialeq_impl( } Field::Bitfields(ref bu) => { for bitfield in bu.bitfields() { - if let Some(_) = bitfield.name() { + if bitfield.name().is_some() { let getter_name = bitfield.getter_name(); let name_ident = ctx.rust_ident_raw(getter_name); tokens.push(quote! { @@ -104,7 +103,7 @@ fn gen_field( TypeKind::Opaque => quote_equals(name_ident), TypeKind::TemplateInstantiation(ref inst) => { - if inst.is_opaque(ctx, &ty_item) { + if inst.is_opaque(ctx, ty_item) { quote! { &self. #name_ident [..] == &other. #name_ident [..] } diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index 8858377f..69dde2c5 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -48,7 +48,6 @@ use proc_macro2::{self, Ident, Span}; use quote::TokenStreamExt; use crate::{Entry, HashMap, HashSet}; -use std; use std::borrow::Cow; use std::cell::Cell; use std::collections::VecDeque; @@ -58,7 +57,7 @@ use std::ops; use std::str::FromStr; // Name of type defined in constified enum module -pub static CONSTIFIED_ENUM_MODULE_REPR_NAME: &'static str = "Type"; +pub static CONSTIFIED_ENUM_MODULE_REPR_NAME: &str = "Type"; fn top_level_path( ctx: &BindgenContext, @@ -681,12 +680,11 @@ impl CodeGenerator for Var { } } VarType::Float(f) => { - match helpers::ast_ty::float_expr(ctx, f) { - Ok(expr) => result.push(quote! { + if let Ok(expr) = helpers::ast_ty::float_expr(ctx, f) { + result.push(quote! { #(#attrs)* pub const #canonical_ident : #ty = #expr ; - }), - Err(..) => return, + }); } } VarType::Char(c) => { @@ -698,7 +696,7 @@ impl CodeGenerator for Var { } } else { // If necessary, apply a `#[link_name]` attribute - let link_name = self.mangled_name().unwrap_or(self.name()); + let link_name = self.mangled_name().unwrap_or_else(|| self.name()); if !utils::names_will_be_identical_after_mangling( &canonical_name, link_name, @@ -756,7 +754,6 @@ impl CodeGenerator for Type { // converted to rust types in fields, arguments, and such. // NOTE(emilio): If you add to this list, make sure to also add // it to BindgenContext::compute_allowlisted_and_codegen_items. - return; } TypeKind::TemplateInstantiation(ref inst) => { inst.codegen(ctx, result, item) @@ -886,12 +883,9 @@ impl CodeGenerator for Type { // We prefer using `pub use` over `pub type` because of: // https://github.com/rust-lang/rust/issues/26264 - if inner_rust_type.to_string().chars().all(|c| match c { - // These are the only characters allowed in simple - // paths, eg `good::dogs::Bront`. - 'A'..='Z' | 'a'..='z' | '0'..='9' | ':' | '_' | ' ' => true, - _ => false, - }) && outer_params.is_empty() && + // These are the only characters allowed in simple + // paths, eg `good::dogs::Bront`. + if inner_rust_type.to_string().chars().all(|c| matches!(c, 'A'..='Z' | 'a'..='z' | '0'..='9' | ':' | '_' | ' ')) && outer_params.is_empty() && !is_opaque && alias_style == AliasVariation::TypeAlias && inner_item.expect_type().canonical_type(ctx).is_enum() @@ -1767,7 +1761,7 @@ impl CodeGenerator for CompInfo { let inner_item = ctx.resolve_item(base.ty); let mut inner = inner_item.to_rust_ty_or_opaque(ctx, &()); - inner.append_implicit_template_params(ctx, &inner_item); + inner.append_implicit_template_params(ctx, inner_item); let field_name = ctx.rust_ident(&base.field_name); struct_layout.saw_base(inner_item.expect_type()); @@ -2126,11 +2120,11 @@ impl CodeGenerator for CompInfo { }) .flat_map(|field| { let name = field.name().unwrap(); - field.offset().and_then(|offset| { + field.offset().map(|offset| { let field_offset = offset / 8; let field_name = ctx.rust_ident(name); - Some(quote! { + quote! { assert_eq!( unsafe { &(*(::#prefix::ptr::null::<#canonical_ident>())).#field_name as *const _ as usize @@ -2138,7 +2132,7 @@ impl CodeGenerator for CompInfo { #field_offset, concat!("Offset of field: ", stringify!(#canonical_ident), "::", stringify!(#field_name)) ); - }) + } }) }) .collect::<Vec<proc_macro2::TokenStream>>(); @@ -2348,7 +2342,7 @@ impl MethodCodegen for Method { return; } let function = function_item.expect_function(); - let times_seen = function.codegen(ctx, result, &function_item); + let times_seen = function.codegen(ctx, result, function_item); let times_seen = match times_seen { Some(seen) => seen, None => return, @@ -2414,7 +2408,7 @@ impl MethodCodegen for Method { } let mut exprs = - helpers::ast_ty::arguments_from_signature(&signature, ctx); + helpers::ast_ty::arguments_from_signature(signature, ctx); let mut stmts = vec![]; @@ -2471,8 +2465,7 @@ impl MethodCodegen for Method { #( #stmts );* }; - let mut attrs = vec![]; - attrs.push(attributes::inline()); + let mut attrs = vec![attributes::inline()]; if signature.must_use() && ctx.options().rust_features().must_use_function @@ -2513,19 +2506,13 @@ pub enum EnumVariation { impl EnumVariation { fn is_rust(&self) -> bool { - match *self { - EnumVariation::Rust { .. } => true, - _ => false, - } + matches!(*self, EnumVariation::Rust { .. }) } /// Both the `Const` and `ModuleConsts` variants will cause this to return /// true. fn is_const(&self) -> bool { - match *self { - EnumVariation::Consts | EnumVariation::ModuleConsts => true, - _ => false, - } + matches!(*self, EnumVariation::Consts | EnumVariation::ModuleConsts) } } @@ -2603,10 +2590,7 @@ impl<'a> EnumBuilder<'a> { /// Returns true if the builder is for a rustified enum. fn is_rust_enum(&self) -> bool { - match *self { - EnumBuilder::Rust { .. } => true, - _ => false, - } + matches!(*self, EnumBuilder::Rust { .. }) } /// Create a new enum given an item builder, a canonical name, a name for @@ -3027,7 +3011,7 @@ impl CodeGenerator for Enum { let mut derives: Vec<_> = derives.into(); for derive in item.annotations().derives().iter() { if !derives.contains(&derive.as_str()) { - derives.push(&derive); + derives.push(derive); } } attrs.push(attributes::derives(&derives)); @@ -3087,7 +3071,7 @@ impl CodeGenerator for Enum { let constant_mangling_prefix = if ctx.options().prepend_enum_name { if enum_ty.name().is_none() { - parent_canonical_name.as_ref().map(|n| &**n) + parent_canonical_name.as_deref() } else { Some(&*name) } @@ -3646,13 +3630,12 @@ impl TryToRustTy for Type { let void = c_void(ctx); return Ok(void.to_ptr(/* is_const = */ false)); } - let template_params = item - .used_template_params(ctx) - .into_iter() - .filter(|param| param.is_template_param(ctx, &())) - .collect::<Vec<_>>(); - if item.is_opaque(ctx, &()) && !template_params.is_empty() { + if item.is_opaque(ctx, &()) && + item.used_template_params(ctx) + .into_iter() + .any(|param| param.is_template_param(ctx, &())) + { self.try_to_opaque(ctx, item) } else if let Some(ty) = self .name() @@ -3681,10 +3664,8 @@ impl TryToRustTy for Type { inner.into_resolver().through_type_refs().resolve(ctx); let inner_ty = inner.expect_type(); - let is_objc_pointer = match inner_ty.kind() { - TypeKind::ObjCInterface(..) => true, - _ => false, - }; + let is_objc_pointer = + matches!(inner_ty.kind(), TypeKind::ObjCInterface(..)); // Regardless if we can properly represent the inner type, we // should always generate a proper pointer here, so use @@ -3817,8 +3798,8 @@ impl TryToRustTy for FunctionSig { _: &(), ) -> error::Result<proc_macro2::TokenStream> { // TODO: we might want to consider ignoring the reference return value. - let ret = utils::fnsig_return_ty(ctx, &self); - let arguments = utils::fnsig_arguments(ctx, &self); + let ret = utils::fnsig_return_ty(ctx, self); + let arguments = utils::fnsig_arguments(ctx, self); let abi = self.abi(); match abi { @@ -4060,12 +4041,12 @@ impl CodeGenerator for ObjCInterface { impl_items.push(impl_item); } - let instance_method_names: Vec<_> = - self.methods().iter().map(|m| m.rust_name()).collect(); - for class_method in self.class_methods() { - let ambiquity = - instance_method_names.contains(&class_method.rust_name()); + let ambiquity = self + .methods() + .iter() + .map(|m| m.rust_name()) + .any(|x| x == class_method.rust_name()); let prefix = if ambiquity { "class_" } else { "" }; let impl_item = objc_method_codegen( ctx, @@ -4657,7 +4638,7 @@ pub mod utils { TypeKind::Array(t, _) => { let stream = if ctx.options().array_pointers_in_arguments { - arg_ty.to_rust_ty_or_opaque(ctx, &arg_item) + arg_ty.to_rust_ty_or_opaque(ctx, arg_item) } else { t.to_rust_ty_or_opaque(ctx, &()) }; diff --git a/src/codegen/struct_layout.rs b/src/codegen/struct_layout.rs index 1c6b977b..b49fab41 100644 --- a/src/codegen/struct_layout.rs +++ b/src/codegen/struct_layout.rs @@ -428,6 +428,6 @@ impl<'a> StructLayoutTracker<'a> { // Else, just align the obvious way. self.latest_offset += self.padding_bytes(layout); - return false; + false } } diff --git a/src/ir/analysis/derive.rs b/src/ir/analysis/derive.rs index 44e6702a..682f7852 100644 --- a/src/ir/analysis/derive.rs +++ b/src/ir/analysis/derive.rs @@ -159,7 +159,7 @@ impl<'ctx> CannotDerive<'ctx> { return can_derive; } - if self.derive_trait.not_by_name(self.ctx, &item) { + if self.derive_trait.not_by_name(self.ctx, item) { trace!( " cannot derive {} for explicitly excluded type", self.derive_trait @@ -223,13 +223,13 @@ impl<'ctx> CannotDerive<'ctx> { let inner_type = self.ctx.resolve_type(inner).canonical_type(self.ctx); if let TypeKind::Function(ref sig) = *inner_type.kind() { - return self.derive_trait.can_derive_fnptr(sig); + self.derive_trait.can_derive_fnptr(sig) } else { - return self.derive_trait.can_derive_pointer(); + self.derive_trait.can_derive_pointer() } } TypeKind::Function(ref sig) => { - return self.derive_trait.can_derive_fnptr(sig) + self.derive_trait.can_derive_fnptr(sig) } // Complex cases need more information @@ -255,7 +255,7 @@ impl<'ctx> CannotDerive<'ctx> { return CanDerive::No; } - if self.derive_trait.can_derive_large_array(&self.ctx) { + if self.derive_trait.can_derive_large_array(self.ctx) { trace!(" array can derive {}", self.derive_trait); return CanDerive::Yes; } @@ -270,7 +270,7 @@ impl<'ctx> CannotDerive<'ctx> { " array is small enough to derive {}", self.derive_trait ); - return CanDerive::Yes; + CanDerive::Yes } TypeKind::Vector(t, len) => { let inner_type = @@ -285,7 +285,7 @@ impl<'ctx> CannotDerive<'ctx> { return CanDerive::No; } assert_ne!(len, 0, "vectors cannot have zero length"); - return self.derive_trait.can_derive_vector(); + self.derive_trait.can_derive_vector() } TypeKind::Comp(ref info) => { @@ -377,7 +377,7 @@ impl<'ctx> CannotDerive<'ctx> { // Bitfield units are always represented as arrays of u8, but // they're not traced as arrays, so we need to check here // instead. - if !self.derive_trait.can_derive_large_array(&self.ctx) && + if !self.derive_trait.can_derive_large_array(self.ctx) && info.has_too_large_bitfield_unit() && !item.is_opaque(self.ctx, &()) { @@ -389,7 +389,7 @@ impl<'ctx> CannotDerive<'ctx> { } let pred = self.derive_trait.consider_edge_comp(); - return self.constrain_join(item, pred); + self.constrain_join(item, pred) } TypeKind::ResolvedTypeRef(..) | @@ -397,12 +397,12 @@ impl<'ctx> CannotDerive<'ctx> { TypeKind::Alias(..) | TypeKind::BlockPointer(..) => { let pred = self.derive_trait.consider_edge_typeref(); - return self.constrain_join(item, pred); + self.constrain_join(item, pred) } TypeKind::TemplateInstantiation(..) => { let pred = self.derive_trait.consider_edge_tmpl_inst(); - return self.constrain_join(item, pred); + self.constrain_join(item, pred) } TypeKind::Opaque => unreachable!( @@ -470,10 +470,7 @@ impl DeriveTrait { fn consider_edge_comp(&self) -> EdgePredicate { match self { DeriveTrait::PartialEqOrPartialOrd => consider_edge_default, - _ => |kind| match kind { - EdgeKind::BaseMember | EdgeKind::Field => true, - _ => false, - }, + _ => |kind| matches!(kind, EdgeKind::BaseMember | EdgeKind::Field), } } @@ -498,53 +495,35 @@ impl DeriveTrait { fn can_derive_large_array(&self, ctx: &BindgenContext) -> bool { if ctx.options().rust_features().larger_arrays { - match self { - DeriveTrait::Default => false, - _ => true, - } + !matches!(self, DeriveTrait::Default) } else { - match self { - DeriveTrait::Copy => true, - _ => false, - } + matches!(self, DeriveTrait::Copy) } } fn can_derive_union(&self) -> bool { - match self { - DeriveTrait::Copy => true, - _ => false, - } + matches!(self, DeriveTrait::Copy) } fn can_derive_compound_with_destructor(&self) -> bool { - match self { - DeriveTrait::Copy => false, - _ => true, - } + !matches!(self, DeriveTrait::Copy) } fn can_derive_compound_with_vtable(&self) -> bool { - match self { - DeriveTrait::Default => false, - _ => true, - } + !matches!(self, DeriveTrait::Default) } fn can_derive_compound_forward_decl(&self) -> bool { - match self { - DeriveTrait::Copy | DeriveTrait::Debug => true, - _ => false, - } + matches!(self, DeriveTrait::Copy | DeriveTrait::Debug) } fn can_derive_incomplete_array(&self) -> bool { - match self { + !matches!( + self, DeriveTrait::Copy | - DeriveTrait::Hash | - DeriveTrait::PartialEqOrPartialOrd => false, - _ => true, - } + DeriveTrait::Hash | + DeriveTrait::PartialEqOrPartialOrd + ) } fn can_derive_fnptr(&self, f: &FunctionSig) -> CanDerive { @@ -693,10 +672,10 @@ impl<'ctx> MonotoneFramework for CannotDerive<'ctx> { Some(ty) => { let mut can_derive = self.constrain_type(item, ty); if let CanDerive::Yes = can_derive { - if !self.derive_trait.can_derive_large_array(&self.ctx) && - ty.layout(self.ctx).map_or(false, |l| { - l.align > RUST_DERIVE_IN_ARRAY_LIMIT - }) + if !self.derive_trait.can_derive_large_array(self.ctx) && + ty.layout(self.ctx) + .map(|l| l.align > RUST_DERIVE_IN_ARRAY_LIMIT) + .unwrap_or_default() { // We have to be conservative: the struct *could* have enough // padding that we emit an array that is longer than diff --git a/src/ir/analysis/has_destructor.rs b/src/ir/analysis/has_destructor.rs index 5fa22e3f..fe95b516 100644 --- a/src/ir/analysis/has_destructor.rs +++ b/src/ir/analysis/has_destructor.rs @@ -41,16 +41,14 @@ pub struct HasDestructorAnalysis<'ctx> { impl<'ctx> HasDestructorAnalysis<'ctx> { fn consider_edge(kind: EdgeKind) -> bool { - match kind { - // These are the only edges that can affect whether a type has a - // destructor or not. + matches!( + kind, EdgeKind::TypeReference | - EdgeKind::BaseMember | - EdgeKind::Field | - EdgeKind::TemplateArgument | - EdgeKind::TemplateDeclaration => true, - _ => false, - } + EdgeKind::BaseMember | + EdgeKind::Field | + EdgeKind::TemplateArgument | + EdgeKind::TemplateDeclaration + ) } fn insert<Id: Into<ItemId>>(&mut self, id: Id) -> ConstrainResult { diff --git a/src/ir/analysis/has_vtable.rs b/src/ir/analysis/has_vtable.rs index 7f5f9117..8ac47a65 100644 --- a/src/ir/analysis/has_vtable.rs +++ b/src/ir/analysis/has_vtable.rs @@ -79,14 +79,14 @@ pub struct HasVtableAnalysis<'ctx> { impl<'ctx> HasVtableAnalysis<'ctx> { fn consider_edge(kind: EdgeKind) -> bool { - match kind { - // These are the only edges that can affect whether a type has a - // vtable or not. + // These are the only edges that can affect whether a type has a + // vtable or not. + matches!( + kind, EdgeKind::TypeReference | - EdgeKind::BaseMember | - EdgeKind::TemplateDeclaration => true, - _ => false, - } + EdgeKind::BaseMember | + EdgeKind::TemplateDeclaration + ) } fn insert<Id: Into<ItemId>>( diff --git a/src/ir/analysis/mod.rs b/src/ir/analysis/mod.rs index ec4d20f9..eb9a1c0d 100644 --- a/src/ir/analysis/mod.rs +++ b/src/ir/analysis/mod.rs @@ -184,7 +184,7 @@ where let mut dependencies = HashMap::default(); for &item in ctx.allowlisted_items() { - dependencies.entry(item).or_insert(vec![]); + dependencies.entry(item).or_insert_with(Vec::new); { // We reverse our natural IR graph edges to find dependencies @@ -197,7 +197,7 @@ where { dependencies .entry(sub_item) - .or_insert(vec![]) + .or_insert_with(Vec::new) .push(item); } }, diff --git a/src/ir/analysis/sizedness.rs b/src/ir/analysis/sizedness.rs index a3ef7531..251c3747 100644 --- a/src/ir/analysis/sizedness.rs +++ b/src/ir/analysis/sizedness.rs @@ -112,17 +112,17 @@ pub struct SizednessAnalysis<'ctx> { impl<'ctx> SizednessAnalysis<'ctx> { fn consider_edge(kind: EdgeKind) -> bool { - match kind { - // These are the only edges that can affect whether a type is - // zero-sized or not. + // These are the only edges that can affect whether a type is + // zero-sized or not. + matches!( + kind, EdgeKind::TemplateArgument | - EdgeKind::TemplateParameterDefinition | - EdgeKind::TemplateDeclaration | - EdgeKind::TypeReference | - EdgeKind::BaseMember | - EdgeKind::Field => true, - _ => false, - } + EdgeKind::TemplateParameterDefinition | + EdgeKind::TemplateDeclaration | + EdgeKind::TypeReference | + EdgeKind::BaseMember | + EdgeKind::Field + ) } /// Insert an incremental result, and return whether this updated our diff --git a/src/ir/analysis/template_params.rs b/src/ir/analysis/template_params.rs index c2f18b1f..e88b774d 100644 --- a/src/ir/analysis/template_params.rs +++ b/src/ir/analysis/template_params.rs @@ -239,7 +239,7 @@ impl<'ctx> UsedTemplateParameters<'ctx> { let args = instantiation .template_arguments() - .into_iter() + .iter() .map(|a| { a.into_resolver() .through_type_refs() @@ -399,8 +399,8 @@ impl<'ctx> MonotoneFramework for UsedTemplateParameters<'ctx> { .collect(); for item in allowlisted_and_blocklisted_items { - dependencies.entry(item).or_insert(vec![]); - used.entry(item).or_insert(Some(ItemSet::new())); + dependencies.entry(item).or_insert_with(Vec::new); + used.entry(item).or_insert_with(|| Some(ItemSet::new())); { // We reverse our natural IR graph edges to find dependencies @@ -408,10 +408,11 @@ impl<'ctx> MonotoneFramework for UsedTemplateParameters<'ctx> { item.trace( ctx, &mut |sub_item: ItemId, _| { - used.entry(sub_item).or_insert(Some(ItemSet::new())); + used.entry(sub_item) + .or_insert_with(|| Some(ItemSet::new())); dependencies .entry(sub_item) - .or_insert(vec![]) + .or_insert_with(Vec::new) .push(item); }, &(), @@ -421,39 +422,42 @@ impl<'ctx> MonotoneFramework for UsedTemplateParameters<'ctx> { // Additionally, whether a template instantiation's template // arguments are used depends on whether the template declaration's // generic template parameters are used. - ctx.resolve_item(item).as_type().map(|ty| match ty.kind() { - &TypeKind::TemplateInstantiation(ref inst) => { - let decl = ctx.resolve_type(inst.template_definition()); - let args = inst.template_arguments(); - - // Although template definitions should always have - // template parameters, there is a single exception: - // opaque templates. Hence the unwrap_or. - let params = decl.self_template_params(ctx); - - for (arg, param) in args.iter().zip(params.iter()) { - let arg = arg - .into_resolver() - .through_type_aliases() - .through_type_refs() - .resolve(ctx) - .id(); - - let param = param - .into_resolver() - .through_type_aliases() - .through_type_refs() - .resolve(ctx) - .id(); - - used.entry(arg).or_insert(Some(ItemSet::new())); - used.entry(param).or_insert(Some(ItemSet::new())); - - dependencies.entry(arg).or_insert(vec![]).push(param); - } + let item_kind = + ctx.resolve_item(item).as_type().map(|ty| ty.kind()); + if let Some(&TypeKind::TemplateInstantiation(ref inst)) = item_kind + { + let decl = ctx.resolve_type(inst.template_definition()); + let args = inst.template_arguments(); + + // Although template definitions should always have + // template parameters, there is a single exception: + // opaque templates. Hence the unwrap_or. + let params = decl.self_template_params(ctx); + + for (arg, param) in args.iter().zip(params.iter()) { + let arg = arg + .into_resolver() + .through_type_aliases() + .through_type_refs() + .resolve(ctx) + .id(); + + let param = param + .into_resolver() + .through_type_aliases() + .through_type_refs() + .resolve(ctx) + .id(); + + used.entry(arg).or_insert_with(|| Some(ItemSet::new())); + used.entry(param).or_insert_with(|| Some(ItemSet::new())); + + dependencies + .entry(arg) + .or_insert_with(Vec::new) + .push(param); } - _ => {} - }); + } } if cfg!(feature = "testing_only_extra_assertions") { @@ -482,10 +486,10 @@ impl<'ctx> MonotoneFramework for UsedTemplateParameters<'ctx> { } UsedTemplateParameters { - ctx: ctx, - used: used, - dependencies: dependencies, - allowlisted_items: allowlisted_items, + ctx, + used, + dependencies, + allowlisted_items, } } diff --git a/src/ir/annotations.rs b/src/ir/annotations.rs index 4b571eaa..9bcda508 100644 --- a/src/ir/annotations.rs +++ b/src/ir/annotations.rs @@ -25,7 +25,7 @@ pub enum FieldAccessorKind { /// documentation: /// /// http://www.stack.nl/~dimitri/doxygen/manual/docblocks.html -#[derive(Clone, PartialEq, Debug)] +#[derive(Default, Clone, PartialEq, Debug)] pub struct Annotations { /// Whether this item is marked as opaque. Only applies to types. opaque: bool, @@ -77,24 +77,6 @@ fn parse_accessor(s: &str) -> FieldAccessorKind { } } -impl Default for Annotations { - fn default() -> Self { - Annotations { - opaque: false, - hide: false, - use_instead_of: None, - disallow_copy: false, - disallow_debug: false, - disallow_default: false, - must_use_type: false, - private_fields: None, - accessor_kind: None, - constify_enum_variant: false, - derives: vec![], - } - } -} - impl Annotations { /// Construct new annotations for the given cursor and its bindgen comments /// (if any). @@ -143,7 +125,7 @@ impl Annotations { /// /// That is, code for `Foo` is used to generate `Bar`. pub fn use_instead_of(&self) -> Option<&[String]> { - self.use_instead_of.as_ref().map(|s| &**s) + self.use_instead_of.as_deref() } /// The list of derives that have been specified in this annotation. diff --git a/src/ir/comment.rs b/src/ir/comment.rs index 4ebe19a2..c96e3ebb 100644 --- a/src/ir/comment.rs +++ b/src/ir/comment.rs @@ -1,7 +1,5 @@ //! Utilities for manipulating C/C++ comments. -use std::iter; - /// The type of a comment. #[derive(Debug, PartialEq, Eq)] enum Kind { @@ -15,7 +13,7 @@ enum Kind { /// Preprocesses a C/C++ comment so that it is a valid Rust comment. pub fn preprocess(comment: &str, indent: usize) -> String { - match self::kind(&comment) { + match self::kind(comment) { Some(Kind::SingleLines) => preprocess_single_lines(comment, indent), Some(Kind::MultiLine) => preprocess_multi_line(comment, indent), None => comment.to_owned(), @@ -35,7 +33,7 @@ fn kind(comment: &str) -> Option<Kind> { fn make_indent(indent: usize) -> String { const RUST_INDENTATION: usize = 4; - iter::repeat(' ').take(indent * RUST_INDENTATION).collect() + " ".repeat(indent * RUST_INDENTATION) } /// Preprocesses multiple single line comments. diff --git a/src/ir/comp.rs b/src/ir/comp.rs index 97983308..a221e520 100644 --- a/src/ir/comp.rs +++ b/src/ir/comp.rs @@ -111,11 +111,10 @@ impl Method { /// Is this a virtual method? pub fn is_virtual(&self) -> bool { - match self.kind { - MethodKind::Virtual { .. } | - MethodKind::VirtualDestructor { .. } => true, - _ => false, - } + matches!( + self.kind, + MethodKind::Virtual { .. } | MethodKind::VirtualDestructor { .. } + ) } /// Is this a static method? @@ -630,7 +629,7 @@ where bitfield_unit_count, unit_size_in_bits, unit_align, - mem::replace(&mut bitfields_in_unit, vec![]), + mem::take(&mut bitfields_in_unit), packed, ); @@ -639,15 +638,12 @@ where offset = 0; unit_align = 0; } - } else { - if offset != 0 && - (bitfield_width == 0 || - (offset & (bitfield_align * 8 - 1)) + - bitfield_width > - bitfield_size * 8) - { - offset = align_to(offset, bitfield_align * 8); - } + } else if offset != 0 && + (bitfield_width == 0 || + (offset & (bitfield_align * 8 - 1)) + bitfield_width > + bitfield_size * 8) + { + offset = align_to(offset, bitfield_align * 8); } } @@ -706,24 +702,24 @@ where /// after. #[derive(Debug)] enum CompFields { - BeforeComputingBitfieldUnits(Vec<RawField>), - AfterComputingBitfieldUnits { + Before(Vec<RawField>), + After { fields: Vec<Field>, has_bitfield_units: bool, }, - ErrorComputingBitfieldUnits, + Error, } impl Default for CompFields { fn default() -> CompFields { - CompFields::BeforeComputingBitfieldUnits(vec![]) + CompFields::Before(vec![]) } } impl CompFields { fn append_raw_field(&mut self, raw: RawField) { match *self { - CompFields::BeforeComputingBitfieldUnits(ref mut raws) => { + CompFields::Before(ref mut raws) => { raws.push(raw); } _ => { @@ -736,9 +732,7 @@ impl CompFields { fn compute_bitfield_units(&mut self, ctx: &BindgenContext, packed: bool) { let raws = match *self { - CompFields::BeforeComputingBitfieldUnits(ref mut raws) => { - mem::replace(raws, vec![]) - } + CompFields::Before(ref mut raws) => mem::take(raws), _ => { panic!("Already computed bitfield units"); } @@ -748,25 +742,23 @@ impl CompFields { match result { Ok((fields, has_bitfield_units)) => { - *self = CompFields::AfterComputingBitfieldUnits { + *self = CompFields::After { fields, has_bitfield_units, }; } Err(()) => { - *self = CompFields::ErrorComputingBitfieldUnits; + *self = CompFields::Error; } } } fn deanonymize_fields(&mut self, ctx: &BindgenContext, methods: &[Method]) { let fields = match *self { - CompFields::AfterComputingBitfieldUnits { - ref mut fields, .. - } => fields, + CompFields::After { ref mut fields, .. } => fields, // Nothing to do here. - CompFields::ErrorComputingBitfieldUnits => return, - CompFields::BeforeComputingBitfieldUnits(_) => { + CompFields::Error => return, + CompFields::Before(_) => { panic!("Not yet computed bitfield units."); } }; @@ -778,7 +770,7 @@ impl CompFields { ) -> bool { methods.iter().any(|method| { let method_name = ctx.resolve_func(method.signature()).name(); - method_name == name || ctx.rust_mangle(&method_name) == name + method_name == name || ctx.rust_mangle(method_name) == name }) } @@ -820,7 +812,7 @@ impl CompFields { for field in fields.iter_mut() { match *field { Field::DataMember(FieldData { ref mut name, .. }) => { - if let Some(_) = *name { + if name.is_some() { continue; } @@ -858,13 +850,13 @@ impl Trace for CompFields { T: Tracer, { match *self { - CompFields::ErrorComputingBitfieldUnits => {} - CompFields::BeforeComputingBitfieldUnits(ref fields) => { + CompFields::Error => {} + CompFields::Before(ref fields) => { for f in fields { tracer.visit_kind(f.ty().into(), EdgeKind::Field); } } - CompFields::AfterComputingBitfieldUnits { ref fields, .. } => { + CompFields::After { ref fields, .. } => { for f in fields { f.trace(context, tracer, &()); } @@ -900,7 +892,7 @@ pub struct FieldData { impl FieldMethods for FieldData { fn name(&self) -> Option<&str> { - self.name.as_ref().map(|n| &**n) + self.name.as_deref() } fn ty(&self) -> TypeId { @@ -908,7 +900,7 @@ impl FieldMethods for FieldData { } fn comment(&self) -> Option<&str> { - self.comment.as_ref().map(|c| &**c) + self.comment.as_deref() } fn bitfield_width(&self) -> Option<u32> { @@ -1131,11 +1123,9 @@ impl CompInfo { /// Get this type's set of fields. pub fn fields(&self) -> &[Field] { match self.fields { - CompFields::ErrorComputingBitfieldUnits => &[], - CompFields::AfterComputingBitfieldUnits { ref fields, .. } => { - fields - } - CompFields::BeforeComputingBitfieldUnits(..) => { + CompFields::Error => &[], + CompFields::After { ref fields, .. } => fields, + CompFields::Before(..) => { panic!("Should always have computed bitfield units first"); } } @@ -1143,13 +1133,9 @@ impl CompInfo { fn has_fields(&self) -> bool { match self.fields { - CompFields::ErrorComputingBitfieldUnits => false, - CompFields::AfterComputingBitfieldUnits { ref fields, .. } => { - !fields.is_empty() - } - CompFields::BeforeComputingBitfieldUnits(ref raw_fields) => { - !raw_fields.is_empty() - } + CompFields::Error => false, + CompFields::After { ref fields, .. } => !fields.is_empty(), + CompFields::Before(ref raw_fields) => !raw_fields.is_empty(), } } @@ -1159,15 +1145,15 @@ impl CompInfo { mut callback: impl FnMut(Layout), ) { match self.fields { - CompFields::ErrorComputingBitfieldUnits => return, - CompFields::AfterComputingBitfieldUnits { ref fields, .. } => { + CompFields::Error => {} + CompFields::After { ref fields, .. } => { for field in fields.iter() { if let Some(layout) = field.layout(ctx) { callback(layout); } } } - CompFields::BeforeComputingBitfieldUnits(ref raw_fields) => { + CompFields::Before(ref raw_fields) => { for field in raw_fields.iter() { let field_ty = ctx.resolve_type(field.0.ty); if let Some(layout) = field_ty.layout(ctx) { @@ -1180,12 +1166,11 @@ impl CompInfo { fn has_bitfields(&self) -> bool { match self.fields { - CompFields::ErrorComputingBitfieldUnits => false, - CompFields::AfterComputingBitfieldUnits { - has_bitfield_units, - .. + CompFields::Error => false, + CompFields::After { + has_bitfield_units, .. } => has_bitfield_units, - CompFields::BeforeComputingBitfieldUnits(_) => { + CompFields::Before(_) => { panic!("Should always have computed bitfield units first"); } } @@ -1776,7 +1761,7 @@ impl IsOpaque for CompInfo { // is a type parameter), then we can't compute bitfield units. We are // left with no choice but to make the whole struct opaque, or else we // might generate structs with incorrect sizes and alignments. - if let CompFields::ErrorComputingBitfieldUnits = self.fields { + if let CompFields::Error = self.fields { return true; } diff --git a/src/ir/context.rs b/src/ir/context.rs index 44df063a..5b23a43d 100644 --- a/src/ir/context.rs +++ b/src/ir/context.rs @@ -299,7 +299,7 @@ where /// types. #[derive(Eq, PartialEq, Hash, Debug)] enum TypeKey { - USR(String), + Usr(String), Declaration(Cursor), } @@ -640,7 +640,7 @@ If you encounter an error missing from this list, please file an issue or a PR!" /// Get the user-provided callbacks by reference, if any. pub fn parse_callbacks(&self) -> Option<&dyn ParseCallbacks> { - self.options().parse_callbacks.as_ref().map(|t| &**t) + self.options().parse_callbacks.as_deref() } /// Add another path to the set of included files. @@ -702,8 +702,10 @@ If you encounter an error missing from this list, please file an issue or a PR!" // Unnamed items can have an USR, but they can't be referenced from // other sites explicitly and the USR can match if the unnamed items are // nested, so don't bother tracking them. - if is_type && !is_template_instantiation && declaration.is_some() { - let mut declaration = declaration.unwrap(); + if !is_type || is_template_instantiation { + return; + } + if let Some(mut declaration) = declaration { if !declaration.is_valid() { if let Some(location) = location { if location.is_template_like() { @@ -732,7 +734,7 @@ If you encounter an error missing from this list, please file an issue or a PR!" let key = if is_unnamed { TypeKey::Declaration(declaration) } else if let Some(usr) = declaration.usr() { - TypeKey::USR(usr) + TypeKey::Usr(usr) } else { warn!( "Valid declaration with no USR: {:?}, {:?}", @@ -830,30 +832,89 @@ If you encounter an error missing from this list, please file an issue or a PR!" /// Mangles a name so it doesn't conflict with any keyword. pub fn rust_mangle<'a>(&self, name: &'a str) -> Cow<'a, str> { - if name.contains("@") || - name.contains("?") || - name.contains("$") || - match name { - "abstract" | "alignof" | "as" | "async" | "become" | - "box" | "break" | "const" | "continue" | "crate" | "do" | - "dyn" | "else" | "enum" | "extern" | "false" | "final" | - "fn" | "for" | "if" | "impl" | "in" | "let" | "loop" | - "macro" | "match" | "mod" | "move" | "mut" | "offsetof" | - "override" | "priv" | "proc" | "pub" | "pure" | "ref" | - "return" | "Self" | "self" | "sizeof" | "static" | - "struct" | "super" | "trait" | "true" | "type" | "typeof" | - "unsafe" | "unsized" | "use" | "virtual" | "where" | - "while" | "yield" | "str" | "bool" | "f32" | "f64" | - "usize" | "isize" | "u128" | "i128" | "u64" | "i64" | - "u32" | "i32" | "u16" | "i16" | "u8" | "i8" | "_" => true, - _ => false, - } + if name.contains('@') || + name.contains('?') || + name.contains('$') || + matches!( + name, + "abstract" | + "alignof" | + "as" | + "async" | + "become" | + "box" | + "break" | + "const" | + "continue" | + "crate" | + "do" | + "dyn" | + "else" | + "enum" | + "extern" | + "false" | + "final" | + "fn" | + "for" | + "if" | + "impl" | + "in" | + "let" | + "loop" | + "macro" | + "match" | + "mod" | + "move" | + "mut" | + "offsetof" | + "override" | + "priv" | + "proc" | + "pub" | + "pure" | + "ref" | + "return" | + "Self" | + "self" | + "sizeof" | + "static" | + "struct" | + "super" | + "trait" | + "true" | + "type" | + "typeof" | + "unsafe" | + "unsized" | + "use" | + "virtual" | + "where" | + "while" | + "yield" | + "str" | + "bool" | + "f32" | + "f64" | + "usize" | + "isize" | + "u128" | + "i128" | + "u64" | + "i64" | + "u32" | + "i32" | + "u16" | + "i16" | + "u8" | + "i8" | + "_" + ) { let mut s = name.to_owned(); s = s.replace("@", "_"); s = s.replace("?", "_"); s = s.replace("$", "_"); - s.push_str("_"); + s.push('_'); return Cow::Owned(s); } Cow::Borrowed(name) @@ -903,11 +964,10 @@ If you encounter an error missing from this list, please file an issue or a PR!" None => continue, }; - match *ty.kind() { - TypeKind::UnresolvedTypeRef(ref ty, loc, parent_id) => { - typerefs.push((id, ty.clone(), loc, parent_id)); - } - _ => {} + if let TypeKind::UnresolvedTypeRef(ref ty, loc, parent_id) = + *ty.kind() + { + typerefs.push((id, *ty, loc, parent_id)); }; } typerefs @@ -978,7 +1038,7 @@ If you encounter an error missing from this list, please file an issue or a PR!" assert!(self.collected_typerefs()); let need_bitfield_allocation = - mem::replace(&mut self.need_bitfield_allocation, vec![]); + mem::take(&mut self.need_bitfield_allocation); for id in need_bitfield_allocation { self.with_loaned_item(id, |ctx, item| { let ty = item.kind_mut().as_type_mut().unwrap(); @@ -1121,7 +1181,8 @@ If you encounter an error missing from this list, please file an issue or a PR!" .ancestors(immut_self) .find(|id| immut_self.resolve_item(*id).is_module()) }; - let new_module = new_module.unwrap_or(self.root_module.into()); + let new_module = + new_module.unwrap_or_else(|| self.root_module.into()); if new_module == old_module { // Already in the correct module. @@ -1329,12 +1390,12 @@ If you encounter an error missing from this list, please file an issue or a PR!" // any sense of template parameter usage, and you're on your own. let mut used_params = HashMap::default(); for &id in self.allowlisted_items() { - used_params.entry(id).or_insert( + used_params.entry(id).or_insert_with(|| { id.self_template_params(self) .into_iter() .map(|p| p.into()) - .collect(), - ); + .collect() + }); } self.used_template_parameters = Some(used_params); } @@ -1815,7 +1876,7 @@ If you encounter an error missing from this list, please file an issue or a PR!" .or_else(|| { decl.cursor() .usr() - .and_then(|usr| self.types.get(&TypeKey::USR(usr))) + .and_then(|usr| self.types.get(&TypeKey::Usr(usr))) }) .cloned() } @@ -1848,32 +1909,32 @@ If you encounter an error missing from this list, please file an issue or a PR!" // of it, or // * we have already parsed and resolved this type, and // there's nothing left to do. - if decl.cursor().is_template_like() && - *ty != decl.cursor().cur_type() && - location.is_some() - { - let location = location.unwrap(); - - // For specialized type aliases, there's no way to get the - // template parameters as of this writing (for a struct - // specialization we wouldn't be in this branch anyway). - // - // Explicitly return `None` if there aren't any - // unspecialized parameters (contains any `TypeRef`) so we - // resolve the canonical type if there is one and it's - // exposed. - // - // This is _tricky_, I know :( - if decl.cursor().kind() == CXCursor_TypeAliasTemplateDecl && - !location.contains_cursor(CXCursor_TypeRef) && - ty.canonical_type().is_valid_and_exposed() + if let Some(location) = location { + if decl.cursor().is_template_like() && + *ty != decl.cursor().cur_type() { - return None; - } + // For specialized type aliases, there's no way to get the + // template parameters as of this writing (for a struct + // specialization we wouldn't be in this branch anyway). + // + // Explicitly return `None` if there aren't any + // unspecialized parameters (contains any `TypeRef`) so we + // resolve the canonical type if there is one and it's + // exposed. + // + // This is _tricky_, I know :( + if decl.cursor().kind() == + CXCursor_TypeAliasTemplateDecl && + !location.contains_cursor(CXCursor_TypeRef) && + ty.canonical_type().is_valid_and_exposed() + { + return None; + } - return self - .instantiate_template(with_id, id, ty, location) - .or_else(|| Some(id)); + return self + .instantiate_template(with_id, id, ty, location) + .or(Some(id)); + } } return Some(self.build_ty_wrapper(with_id, id, parent_id, ty)); @@ -1933,7 +1994,7 @@ If you encounter an error missing from this list, please file an issue or a PR!" with_id, None, None, - parent_id.unwrap_or(self.current_module.into()), + parent_id.unwrap_or_else(|| self.current_module.into()), ItemKind::Type(ty), ); self.add_builtin_item(item); @@ -2074,10 +2135,7 @@ If you encounter an error missing from this list, please file an issue or a PR!" id: Id, ) -> bool { let id = id.into(); - match self.replacements.get(path) { - Some(replaced_by) if *replaced_by != id => true, - _ => false, - } + matches!(self.replacements.get(path), Some(replaced_by) if *replaced_by != id) } /// Is the type with the given `name` marked as opaque? @@ -2112,11 +2170,9 @@ If you encounter an error missing from this list, please file an issue or a PR!" module_name = Some(spelling) } - let tokens = cursor.tokens(); - let mut iter = tokens.iter(); let mut kind = ModuleKind::Normal; let mut found_namespace_keyword = false; - while let Some(token) = iter.next() { + for token in cursor.tokens().iter() { match token.spelling() { b"inline" => { assert!(!found_namespace_keyword); @@ -2399,7 +2455,7 @@ If you encounter an error missing from this list, please file an issue or a PR!" let codegen_items = if self.options().allowlist_recursively { AllowlistedItemsTraversal::new( self, - roots.clone(), + roots, traversal::codegen_edges, ) .collect::<ItemSet>() @@ -2700,7 +2756,7 @@ impl ItemResolver { pub fn new<Id: Into<ItemId>>(id: Id) -> ItemResolver { let id = id.into(); ItemResolver { - id: id, + id, through_type_refs: false, through_type_aliases: false, } @@ -2767,7 +2823,7 @@ impl PartialType { /// Construct a new `PartialType`. pub fn new(decl: Cursor, id: ItemId) -> PartialType { // assert!(decl == decl.canonical()); - PartialType { decl: decl, id: id } + PartialType { decl, id } } /// The cursor pointing to this partial type's declaration location. diff --git a/src/ir/enum_ty.rs b/src/ir/enum_ty.rs index 15d41368..97455c91 100644 --- a/src/ir/enum_ty.rs +++ b/src/ir/enum_ty.rs @@ -86,7 +86,7 @@ impl Enum { } else { Some(type_name) }; - let type_name = type_name.as_ref().map(String::as_str); + let type_name = type_name.as_deref(); let definition = declaration.definition().unwrap_or(declaration); definition.visit(|cursor| { @@ -286,7 +286,7 @@ impl EnumVariant { /// Get this variant's documentation. pub fn comment(&self) -> Option<&str> { - self.comment.as_ref().map(|s| &**s) + self.comment.as_deref() } /// Returns whether this variant should be enforced to be a constant by code diff --git a/src/ir/function.rs b/src/ir/function.rs index 661ee593..a3a2bbfb 100644 --- a/src/ir/function.rs +++ b/src/ir/function.rs @@ -123,7 +123,7 @@ impl Function { /// Get this function's name. pub fn mangled_name(&self) -> Option<&str> { - self.mangled_name.as_ref().map(|n| &**n) + self.mangled_name.as_deref() } /// Get this function's signature type. @@ -187,10 +187,7 @@ pub enum Abi { impl Abi { /// Returns whether this Abi is known or not. fn is_unknown(&self) -> bool { - match *self { - Abi::Unknown(..) => true, - _ => false, - } + matches!(*self, Abi::Unknown(..)) } } @@ -340,7 +337,7 @@ fn args_from_ty_and_cursor( }); let cursor = arg_cur.unwrap_or(*cursor); - let ty = arg_ty.unwrap_or(cursor.cur_type()); + let ty = arg_ty.unwrap_or_else(|| cursor.cur_type()); (name, Item::from_ty_or_ref(ty, cursor, None, ctx)) }) .collect() @@ -360,7 +357,7 @@ impl FunctionSig { argument_types, is_variadic, must_use, - abi: abi, + abi, } } @@ -411,7 +408,7 @@ impl FunctionSig { CXCursor_CXXMethod | CXCursor_ObjCInstanceMethodDecl | CXCursor_ObjCClassMethodDecl => { - args_from_ty_and_cursor(&ty, &cursor, ctx) + args_from_ty_and_cursor(ty, &cursor, ctx) } _ => { // For non-CXCursor_FunctionDecl, visiting the cursor's children @@ -434,7 +431,7 @@ impl FunctionSig { // right AST for functions tagged as stdcall and such... // // https://bugs.llvm.org/show_bug.cgi?id=45919 - args_from_ty_and_cursor(&ty, &cursor, ctx) + args_from_ty_and_cursor(ty, &cursor, ctx) } else { args } @@ -522,7 +519,7 @@ impl FunctionSig { warn!("Unknown calling convention: {:?}", call_conv); } - Ok(Self::new(ret.into(), args, ty.is_variadic(), must_use, abi)) + Ok(Self::new(ret, args, ty.is_variadic(), must_use, abi)) } /// Get this function signature's return type. @@ -567,10 +564,7 @@ impl FunctionSig { return false; } - match self.abi { - Abi::C | Abi::Unknown(..) => true, - _ => false, - } + matches!(self.abi, Abi::C | Abi::Unknown(..)) } } diff --git a/src/ir/item.rs b/src/ir/item.rs index a38c8e5f..730271ef 100644 --- a/src/ir/item.rs +++ b/src/ir/item.rs @@ -436,15 +436,15 @@ impl Item { ) -> Self { debug_assert!(id != parent_id || kind.is_module()); Item { - id: id, + id, local_id: LazyCell::new(), next_child_local_id: Cell::new(1), canonical_name: LazyCell::new(), path_for_allowlisting: LazyCell::new(), - parent_id: parent_id, - comment: comment, + parent_id, + comment, annotations: annotations.unwrap_or_default(), - kind: kind, + kind, } } @@ -612,10 +612,7 @@ impl Item { /// Is this item a module? pub fn is_module(&self) -> bool { - match self.kind { - ItemKind::Module(..) => true, - _ => false, - } + matches!(self.kind, ItemKind::Module(..)) } /// Get this item's annotations. @@ -641,7 +638,7 @@ impl Item { match self.kind { ItemKind::Type(..) => { ctx.options().blocklisted_types.matches(&name) || - ctx.is_replaced_type(&path, self.id) + ctx.is_replaced_type(path, self.id) } ItemKind::Function(..) => { ctx.options().blocklisted_functions.matches(&name) @@ -658,10 +655,7 @@ impl Item { /// Is this item a var type? pub fn is_var(&self) -> bool { - match *self.kind() { - ItemKind::Var(..) => true, - _ => false, - } + matches!(*self.kind(), ItemKind::Var(..)) } /// Take out item NameOptions @@ -722,7 +716,7 @@ impl Item { .through_type_refs() .resolve(ctx) .push_disambiguated_name(ctx, to, level + 1); - to.push_str("_"); + to.push('_'); } to.push_str(&format!("close{}", level)); } @@ -835,7 +829,7 @@ impl Item { if ctx.options().enable_cxx_namespaces { return path.last().unwrap().clone(); } - return path.join("_").to_owned(); + return path.join("_"); } let base_name = target.base_name(ctx); @@ -873,7 +867,7 @@ impl Item { // If target is anonymous we need find its first named ancestor. if target.is_anon() { - while let Some(id) = ids_iter.next() { + for id in ids_iter.by_ref() { ids.push(id); if !ctx.resolve_item(id).is_anon() { @@ -1104,7 +1098,7 @@ impl IsOpaque for Item { ); self.annotations.opaque() || self.as_type().map_or(false, |ty| ty.is_opaque(ctx, self)) || - ctx.opaque_by_name(&self.path_for_allowlisting(ctx)) + ctx.opaque_by_name(self.path_for_allowlisting(ctx)) } } @@ -1114,20 +1108,16 @@ where { fn has_vtable(&self, ctx: &BindgenContext) -> bool { let id: ItemId = (*self).into(); - id.as_type_id(ctx) - .map_or(false, |id| match ctx.lookup_has_vtable(id) { - HasVtableResult::No => false, - _ => true, - }) + id.as_type_id(ctx).map_or(false, |id| { + !matches!(ctx.lookup_has_vtable(id), HasVtableResult::No) + }) } fn has_vtable_ptr(&self, ctx: &BindgenContext) -> bool { let id: ItemId = (*self).into(); - id.as_type_id(ctx) - .map_or(false, |id| match ctx.lookup_has_vtable(id) { - HasVtableResult::SelfHasVtable => true, - _ => false, - }) + id.as_type_id(ctx).map_or(false, |id| { + matches!(ctx.lookup_has_vtable(id), HasVtableResult::SelfHasVtable) + }) } } @@ -1392,7 +1382,7 @@ impl ClangItemParser for Item { } ctx.known_semantic_parent(definition) .or(parent_id) - .unwrap_or(ctx.current_module().into()) + .unwrap_or_else(|| ctx.current_module().into()) } None => relevant_parent_id, }; @@ -1524,7 +1514,7 @@ impl ClangItemParser for Item { potential_id, None, None, - parent_id.unwrap_or(current_module.into()), + parent_id.unwrap_or_else(|| current_module.into()), ItemKind::Type(Type::new(None, None, kind, is_const)), ), None, @@ -1603,7 +1593,7 @@ impl ClangItemParser for Item { Annotations::new(&decl).or_else(|| Annotations::new(&location)); if let Some(ref annotations) = annotations { - if let Some(ref replaced) = annotations.use_instead_of() { + if let Some(replaced) = annotations.use_instead_of() { ctx.replace(replaced, id); } } @@ -1851,11 +1841,7 @@ impl ClangItemParser for Item { clang_sys::CXChildVisit_Continue }); - if let Some(def) = definition { - def - } else { - return None; - } + definition? }; assert!(is_template_with_spelling(&definition, &ty_spelling)); @@ -1939,7 +1925,7 @@ impl ItemCanonicalPath for Item { path.push(CONSTIFIED_ENUM_MODULE_REPR_NAME.into()); } - return path; + path } fn canonical_path(&self, ctx: &BindgenContext) -> Vec<String> { @@ -1972,8 +1958,8 @@ impl<'a> NameOptions<'a> { /// Construct a new `NameOptions` pub fn new(item: &'a Item, ctx: &'a BindgenContext) -> Self { NameOptions { - item: item, - ctx: ctx, + item, + ctx, within_namespaces: false, user_mangled: UserMangled::Yes, } diff --git a/src/ir/layout.rs b/src/ir/layout.rs index 28a6604d..6cf91131 100644 --- a/src/ir/layout.rs +++ b/src/ir/layout.rs @@ -64,7 +64,7 @@ impl Layout { next_align *= 2; } Layout { - size: size, + size, align: next_align / 2, packed: false, } diff --git a/src/ir/module.rs b/src/ir/module.rs index 13b7c19f..d5aca94a 100644 --- a/src/ir/module.rs +++ b/src/ir/module.rs @@ -32,15 +32,15 @@ impl Module { /// Construct a new `Module`. pub fn new(name: Option<String>, kind: ModuleKind) -> Self { Module { - name: name, - kind: kind, + name, + kind, children: ItemSet::new(), } } /// Get this module's name. pub fn name(&self) -> Option<&str> { - self.name.as_ref().map(|n| &**n) + self.name.as_deref() } /// Get a mutable reference to this module's children. diff --git a/src/ir/objc.rs b/src/ir/objc.rs index 91855c67..0845ad0f 100644 --- a/src/ir/objc.rs +++ b/src/ir/objc.rs @@ -89,12 +89,10 @@ impl ObjCInterface { pub fn rust_name(&self) -> String { if let Some(ref cat) = self.category { format!("{}_{}", self.name(), cat) + } else if self.is_protocol { + format!("P{}", self.name()) } else { - if self.is_protocol { - format!("P{}", self.name()) - } else { - format!("I{}", self.name().to_owned()) - } + format!("I{}", self.name().to_owned()) } } @@ -149,28 +147,34 @@ impl ObjCInterface { // Gather protocols this interface conforms to let needle = format!("P{}", c.spelling()); let items_map = ctx.items(); - debug!("Interface {} conforms to {}, find the item", interface.name, needle); + debug!( + "Interface {} conforms to {}, find the item", + interface.name, needle + ); - for (id, item) in items_map - { + for (id, item) in items_map { if let Some(ty) = item.as_type() { - match *ty.kind() { - TypeKind::ObjCInterface(ref protocol) => { - if protocol.is_protocol - { - debug!("Checking protocol {}, ty.name {:?}", protocol.name, ty.name()); - if Some(needle.as_ref()) == ty.name() { - debug!("Found conforming protocol {:?}", item); - interface.conforms_to.push(id); - break; - } + if let TypeKind::ObjCInterface(ref protocol) = + *ty.kind() + { + if protocol.is_protocol { + debug!( + "Checking protocol {}, ty.name {:?}", + protocol.name, + ty.name() + ); + if Some(needle.as_ref()) == ty.name() { + debug!( + "Found conforming protocol {:?}", + item + ); + interface.conforms_to.push(id); + break; } } - _ => {} } } } - } CXCursor_ObjCInstanceMethodDecl | CXCursor_ObjCClassMethodDecl => { @@ -178,8 +182,10 @@ impl ObjCInterface { let signature = FunctionSig::from_ty(&c.cur_type(), &c, ctx) .expect("Invalid function sig"); - let is_class_method = c.kind() == CXCursor_ObjCClassMethodDecl; - let method = ObjCMethod::new(&name, signature, is_class_method); + let is_class_method = + c.kind() == CXCursor_ObjCClassMethodDecl; + let method = + ObjCMethod::new(&name, signature, is_class_method); interface.add_method(method); } CXCursor_TemplateTypeParameter => { @@ -189,7 +195,7 @@ impl ObjCInterface { CXCursor_ObjCSuperClassRef => { let item = Item::from_ty_or_ref(c.cur_type(), c, None, ctx); interface.parent_class = Some(item.into()); - }, + } _ => {} } CXChildVisit_Continue @@ -218,7 +224,7 @@ impl ObjCMethod { ObjCMethod { name: name.to_owned(), - rust_name: rust_name.to_owned(), + rust_name, signature, is_class_method, } @@ -261,7 +267,7 @@ impl ObjCMethod { .collect(); // No arguments - if args.len() == 0 && split_name.len() == 1 { + if args.is_empty() && split_name.len() == 1 { let name = &split_name[0]; return quote! { #name @@ -269,13 +275,12 @@ impl ObjCMethod { } // Check right amount of arguments - if args.len() != split_name.len() - 1 { - panic!( - "Incorrect method name or arguments for objc method, {:?} vs {:?}", - args, - split_name, - ); - } + assert!( + args.len() == split_name.len() - 1, + "Incorrect method name or arguments for objc method, {:?} vs {:?}", + args, + split_name + ); // Get arguments without type signatures to pass to `msg_send!` let mut args_without_types = vec![]; diff --git a/src/ir/template.rs b/src/ir/template.rs index b519fff1..8b06748e 100644 --- a/src/ir/template.rs +++ b/src/ir/template.rs @@ -134,10 +134,10 @@ pub trait TemplateParameters: Sized { where Self: ItemAncestors, { - let ancestors: Vec<_> = self.ancestors(ctx).collect(); + let mut ancestors: Vec<_> = self.ancestors(ctx).collect(); + ancestors.reverse(); ancestors .into_iter() - .rev() .flat_map(|id| id.self_template_params(ctx).into_iter()) .collect() } diff --git a/src/ir/traversal.rs b/src/ir/traversal.rs index 430dd027..088e744a 100644 --- a/src/ir/traversal.rs +++ b/src/ir/traversal.rs @@ -24,9 +24,9 @@ impl Edge { } } -impl Into<ItemId> for Edge { - fn into(self) -> ItemId { - self.to +impl From<Edge> for ItemId { + fn from(val: Edge) -> Self { + val.to } } @@ -424,10 +424,10 @@ where } ItemTraversal { - ctx: ctx, - seen: seen, - queue: queue, - predicate: predicate, + ctx, + seen, + queue, + predicate, currently_traversing: None, } } diff --git a/src/ir/ty.rs b/src/ir/ty.rs index e049ed65..9cfbd7a9 100644 --- a/src/ir/ty.rs +++ b/src/ir/ty.rs @@ -87,23 +87,17 @@ impl Type { /// Get this type's name. pub fn name(&self) -> Option<&str> { - self.name.as_ref().map(|name| &**name) + self.name.as_deref() } /// Whether this is a block pointer type. pub fn is_block_pointer(&self) -> bool { - match self.kind { - TypeKind::BlockPointer(..) => true, - _ => false, - } + matches!(self.kind, TypeKind::BlockPointer(..)) } /// Is this a compound type? pub fn is_comp(&self) -> bool { - match self.kind { - TypeKind::Comp(..) => true, - _ => false, - } + matches!(self.kind, TypeKind::Comp(..)) } /// Is this a union? @@ -116,58 +110,43 @@ impl Type { /// Is this type of kind `TypeKind::TypeParam`? pub fn is_type_param(&self) -> bool { - match self.kind { - TypeKind::TypeParam => true, - _ => false, - } + matches!(self.kind, TypeKind::TypeParam) } /// Is this a template instantiation type? pub fn is_template_instantiation(&self) -> bool { - match self.kind { - TypeKind::TemplateInstantiation(..) => true, - _ => false, - } + matches!(self.kind, TypeKind::TemplateInstantiation(..)) } /// Is this a template alias type? pub fn is_template_alias(&self) -> bool { - match self.kind { - TypeKind::TemplateAlias(..) => true, - _ => false, - } + matches!(self.kind, TypeKind::TemplateAlias(..)) } /// Is this a function type? pub fn is_function(&self) -> bool { - match self.kind { - TypeKind::Function(..) => true, - _ => false, - } + matches!(self.kind, TypeKind::Function(..)) } /// Is this an enum type? pub fn is_enum(&self) -> bool { - match self.kind { - TypeKind::Enum(..) => true, - _ => false, - } + matches!(self.kind, TypeKind::Enum(..)) } /// Is this either a builtin or named type? pub fn is_builtin_or_type_param(&self) -> bool { - match self.kind { + matches!( + self.kind, TypeKind::Void | - TypeKind::NullPtr | - TypeKind::Function(..) | - TypeKind::Array(..) | - TypeKind::Reference(..) | - TypeKind::Pointer(..) | - TypeKind::Int(..) | - TypeKind::Float(..) | - TypeKind::TypeParam => true, - _ => false, - } + TypeKind::NullPtr | + TypeKind::Function(..) | + TypeKind::Array(..) | + TypeKind::Reference(..) | + TypeKind::Pointer(..) | + TypeKind::Int(..) | + TypeKind::Float(..) | + TypeKind::TypeParam + ) } /// Creates a new named type, with name `name`. @@ -178,26 +157,17 @@ impl Type { /// Is this a floating point type? pub fn is_float(&self) -> bool { - match self.kind { - TypeKind::Float(..) => true, - _ => false, - } + matches!(self.kind, TypeKind::Float(..)) } /// Is this a boolean type? pub fn is_bool(&self) -> bool { - match self.kind { - TypeKind::Int(IntKind::Bool) => true, - _ => false, - } + matches!(self.kind, TypeKind::Int(IntKind::Bool)) } /// Is this an integer type? pub fn is_integer(&self) -> bool { - match self.kind { - TypeKind::Int(..) => true, - _ => false, - } + matches!(self.kind, TypeKind::Int(..)) } /// Cast this type to an integer kind, or `None` if it is not an integer @@ -216,19 +186,15 @@ impl Type { /// Is this a reference to another type? pub fn is_type_ref(&self) -> bool { - match self.kind { - TypeKind::ResolvedTypeRef(_) | - TypeKind::UnresolvedTypeRef(_, _, _) => true, - _ => false, - } + matches!( + self.kind, + TypeKind::ResolvedTypeRef(_) | TypeKind::UnresolvedTypeRef(_, _, _) + ) } /// Is this an unresolved reference? pub fn is_unresolved_ref(&self) -> bool { - match self.kind { - TypeKind::UnresolvedTypeRef(_, _, _) => true, - _ => false, - } + matches!(self.kind, TypeKind::UnresolvedTypeRef(_, _, _)) } /// Is this a incomplete array type? @@ -278,14 +244,14 @@ impl Type { match self.kind { TypeKind::TypeParam => { let name = self.name().expect("Unnamed named type?"); - !clang::is_valid_identifier(&name) + !clang::is_valid_identifier(name) } _ => false, } } /// Takes `name`, and returns a suitable identifier representation for it. - fn sanitize_name<'a>(name: &'a str) -> Cow<'a, str> { + fn sanitize_name(name: &str) -> Cow<str> { if clang::is_valid_identifier(name) { return Cow::Borrowed(name); } @@ -300,12 +266,8 @@ impl Type { ctx: &BindgenContext, ) -> Option<Cow<'a, str>> { let name_info = match *self.kind() { - TypeKind::Pointer(inner) => { - Some((inner.into(), Cow::Borrowed("ptr"))) - } - TypeKind::Reference(inner) => { - Some((inner.into(), Cow::Borrowed("ref"))) - } + TypeKind::Pointer(inner) => Some((inner, Cow::Borrowed("ptr"))), + TypeKind::Reference(inner) => Some((inner, Cow::Borrowed("ref"))), TypeKind::Array(inner, length) => { Some((inner, format!("array{}", length).into())) } @@ -375,16 +337,16 @@ impl Type { /// There are some types we don't want to stop at when finding an opaque /// item, so we can arrive to the proper item that needs to be generated. pub fn should_be_traced_unconditionally(&self) -> bool { - match self.kind { + matches!( + self.kind, TypeKind::Comp(..) | - TypeKind::Function(..) | - TypeKind::Pointer(..) | - TypeKind::Array(..) | - TypeKind::Reference(..) | - TypeKind::TemplateInstantiation(..) | - TypeKind::ResolvedTypeRef(..) => true, - _ => false, - } + TypeKind::Function(..) | + TypeKind::Pointer(..) | + TypeKind::Array(..) | + TypeKind::Reference(..) | + TypeKind::TemplateInstantiation(..) | + TypeKind::ResolvedTypeRef(..) + ) } } @@ -791,7 +753,7 @@ impl Type { (ty.template_args().is_some() && ty_kind != CXType_Typedef) { // This is a template instantiation. - match TemplateInstantiation::from_ty(&ty, ctx) { + match TemplateInstantiation::from_ty(ty, ctx) { Some(inst) => TypeKind::TemplateInstantiation(inst), None => TypeKind::Opaque, } diff --git a/src/ir/var.rs b/src/ir/var.rs index 49c4f304..cd179370 100644 --- a/src/ir/var.rs +++ b/src/ir/var.rs @@ -88,7 +88,7 @@ impl Var { /// Get this variable's mangled name. pub fn mangled_name(&self) -> Option<&str> { - self.mangled_name.as_ref().map(|n| &**n) + self.mangled_name.as_deref() } } @@ -282,7 +282,7 @@ impl ClangSubItemParser for Var { .parse_callbacks() .and_then(|c| c.int_macro(&name, value)) .unwrap_or_else(|| { - default_macro_constant_type(&ctx, value) + default_macro_constant_type(ctx, value) }); (TypeKind::Int(kind), VarType::Int(value)) @@ -398,11 +398,8 @@ fn parse_macro( let parser = expr::IdentifierParser::new(ctx.parsed_macros()); - match parser.macro_definition(&cexpr_tokens) { - Ok((_, (id, val))) => { - return Some((id.into(), val)); - } - _ => {} + if let Ok((_, (id, val))) = parser.macro_definition(&cexpr_tokens) { + return Some((id.into(), val)); } // Try without the last token, to workaround a libclang bug in versions @@ -89,7 +89,7 @@ type HashSet<K> = ::rustc_hash::FxHashSet<K>; pub(crate) use std::collections::hash_map::Entry; /// Default prefix for the anon fields. -pub const DEFAULT_ANON_FIELDS_PREFIX: &'static str = "__bindgen_anon_"; +pub const DEFAULT_ANON_FIELDS_PREFIX: &str = "__bindgen_anon_"; fn file_is_cpp(name_file: &str) -> bool { name_file.ends_with(".hpp") || @@ -2148,7 +2148,7 @@ pub struct Bindings { module: proc_macro2::TokenStream, } -pub(crate) const HOST_TARGET: &'static str = +pub(crate) const HOST_TARGET: &str = include_str!(concat!(env!("OUT_DIR"), "/host-target.txt")); // Some architecture triplets are different between rust and libclang, see #1211 @@ -2156,7 +2156,8 @@ pub(crate) const HOST_TARGET: &'static str = fn rust_to_clang_target(rust_target: &str) -> String { if rust_target.starts_with("aarch64-apple-") { let mut clang_target = "arm64-apple-".to_owned(); - clang_target.push_str(&rust_target["aarch64-apple-".len()..]); + clang_target + .push_str(rust_target.strip_prefix("aarch64-apple-").unwrap()); return clang_target; } rust_target.to_owned() @@ -2278,10 +2279,7 @@ impl Bindings { // Whether we are working with C or C++ inputs. let is_cpp = args_are_cpp(&options.clang_args) || - options - .input_header - .as_ref() - .map_or(false, |i| file_is_cpp(&i)); + options.input_header.as_deref().map_or(false, file_is_cpp); let search_paths = if is_cpp { clang.cpp_search_paths @@ -2369,15 +2367,6 @@ impl Bindings { }) } - /// Convert these bindings into source text (with raw lines prepended). - pub fn to_string(&self) -> String { - let mut bytes = vec![]; - self.write(Box::new(&mut bytes) as Box<dyn Write>) - .expect("writing to a vec cannot fail"); - String::from_utf8(bytes) - .expect("we should only write bindings that are valid utf-8") - } - /// Write these bindings as source text to a file. pub fn write_to_file<P: AsRef<Path>>(&self, path: P) -> io::Result<()> { let file = OpenOptions::new() @@ -2427,7 +2416,7 @@ impl Bindings { } /// Gets the rustfmt path to rustfmt the generated bindings. - fn rustfmt_path<'a>(&'a self) -> io::Result<Cow<'a, PathBuf>> { + fn rustfmt_path(&self) -> io::Result<Cow<PathBuf>> { debug_assert!(self.options.rustfmt_bindings); if let Some(ref p) = self.options.rustfmt_path { return Ok(Cow::Borrowed(p)); @@ -2518,6 +2507,18 @@ impl Bindings { } } +impl std::fmt::Display for Bindings { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut bytes = vec![]; + self.write(Box::new(&mut bytes) as Box<dyn Write>) + .expect("writing to a vec cannot fail"); + f.write_str( + std::str::from_utf8(&bytes) + .expect("we should only write bindings that are valid utf-8"), + ) + } +} + /// Determines whether the given cursor is in any of the files matched by the /// options. fn filter_builtins(ctx: &BindgenContext, cursor: &clang::Cursor) -> bool { @@ -2566,7 +2567,7 @@ fn parse(context: &mut BindgenContext) -> Result<(), ()> { if context.options().emit_ast { fn dump_if_not_builtin(cur: &clang::Cursor) -> CXChildVisitResult { if !cur.is_builtin() { - clang::ast_dump(&cur, 0) + clang::ast_dump(cur, 0) } else { CXChildVisit_Continue } @@ -2603,26 +2604,19 @@ pub fn clang_version() -> ClangVersion { let raw_v: String = clang::extract_clang_version(); let split_v: Option<Vec<&str>> = raw_v .split_whitespace() - .filter(|t| t.chars().next().map_or(false, |v| v.is_ascii_digit())) - .next() + .find(|t| t.chars().next().map_or(false, |v| v.is_ascii_digit())) .map(|v| v.split('.').collect()); - match split_v { - Some(v) => { - if v.len() >= 2 { - let maybe_major = v[0].parse::<u32>(); - let maybe_minor = v[1].parse::<u32>(); - match (maybe_major, maybe_minor) { - (Ok(major), Ok(minor)) => { - return ClangVersion { - parsed: Some((major, minor)), - full: raw_v.clone(), - } - } - _ => {} - } + if let Some(v) = split_v { + if v.len() >= 2 { + let maybe_major = v[0].parse::<u32>(); + let maybe_minor = v[1].parse::<u32>(); + if let (Ok(major), Ok(minor)) = (maybe_major, maybe_minor) { + return ClangVersion { + parsed: Some((major, minor)), + full: raw_v.clone(), + }; } } - None => {} }; ClangVersion { parsed: None, diff --git a/src/main.rs b/src/main.rs index 1768ed8d..f3398db8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,7 +37,7 @@ fn clang_version_check() { ); if expected_version.is_some() { - assert_eq!(version.parsed, version.parsed); + // assert_eq!(version.parsed, version.parsed); } } @@ -45,9 +45,7 @@ pub fn main() { #[cfg(feature = "logging")] env_logger::init(); - let bind_args: Vec<_> = env::args().collect(); - - match builder_from_flags(bind_args.into_iter()) { + match builder_from_flags(env::args()) { Ok((builder, output, verbose)) => { clang_version_check(); let builder_result = panic::catch_unwind(|| { diff --git a/src/options.rs b/src/options.rs index 0524871a..9aac5dae 100644 --- a/src/options.rs +++ b/src/options.rs @@ -717,7 +717,7 @@ where if let Some(what_to_generate) = matches.value_of("generate") { let mut config = CodegenConfig::empty(); - for what in what_to_generate.split(",") { + for what in what_to_generate.split(',') { match what { "functions" => config.insert(CodegenConfig::FUNCTIONS), "types" => config.insert(CodegenConfig::TYPES), diff --git a/tests/stylo_sanity.rs b/tests/stylo_sanity.rs index 2ea77628..39777cd1 100755 --- a/tests/stylo_sanity.rs +++ b/tests/stylo_sanity.rs @@ -545,14 +545,14 @@ fn sanity_check_can_generate_stylo_bindings() { let now = Instant::now(); - println!(""); - println!(""); + println!(); + println!(); println!( "Generated Stylo bindings in: {:?}", now.duration_since(then) ); - println!(""); - println!(""); + println!(); + println!(); // panic!("Uncomment this line to get timing logs"); } |