Skip to content

Commit

Permalink
Fixes all the compiler warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
xanathar committed May 29, 2024
1 parent cd9346d commit a0e36cc
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 69 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[workspace]
resolver = "2"
members = [
"csbindgen",
"csbindgen-tests",
Expand Down
25 changes: 13 additions & 12 deletions csbindgen-tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::missing_safety_doc)]

use std::{
collections::HashSet,
ffi::{c_char, c_long, c_ulong, CString},
Expand Down Expand Up @@ -310,7 +312,7 @@ pub extern "C" fn ignore_nop() -> (i32, i32) {
}

#[no_mangle]
pub extern "C" fn nop() -> () {
pub extern "C" fn nop() {
println!("hello nop!");
}

Expand Down Expand Up @@ -374,19 +376,17 @@ pub struct InternalCounterContext {
}

#[no_mangle]
pub extern "C" fn my_bool(
pub unsafe extern "C" fn my_bool(
x: bool,
y: bool,
z: bool,
xr: *mut bool,
yr: *mut bool,
zr: *mut bool,
) -> bool {
unsafe {
*xr = x;
*yr = y;
*zr = z;
}
*xr = x;
*yr = y;
*zr = z;

true
}
Expand All @@ -399,12 +399,12 @@ pub extern "C" fn alloc_c_string() -> *mut c_char {

#[no_mangle]
pub unsafe extern "C" fn free_c_string(str: *mut c_char) {
unsafe { CString::from_raw(str) };
unsafe { let _ = CString::from_raw(str); };
}

#[no_mangle]
pub extern "C" fn alloc_u8_string() -> *mut ByteBuffer {
let str = format!("foo bar baz");
let str = "foo bar baz".to_string();
let buf = ByteBuffer::from_vec(str.into_bytes());
Box::into_raw(Box::new(buf))
}
Expand Down Expand Up @@ -451,8 +451,8 @@ pub extern "C" fn create_context() -> *mut Context {
}

#[no_mangle]
pub extern "C" fn delete_context(context: *mut Context) {
unsafe { Box::from_raw(context) };
pub unsafe extern "C" fn delete_context(context: *mut Context) {
unsafe { let _ = Box::from_raw(context); };
}

#[no_mangle]
Expand Down Expand Up @@ -530,6 +530,7 @@ pub struct ByteBuffer {
}

impl ByteBuffer {
#[allow(clippy::len_without_is_empty)]
pub fn len(&self) -> usize {
self.length
.try_into()
Expand Down Expand Up @@ -646,7 +647,7 @@ pub struct InternalHiddenContext {
}

pub struct TreatAsEmptyStruct {
internal: std::sync::Arc<InternalHiddenContext>
_internal: std::sync::Arc<InternalHiddenContext>
}

#[no_mangle]
Expand Down
2 changes: 1 addition & 1 deletion csbindgen/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ impl Builder {
/// configure C# generate const, default is false
/// equivalent to csharp_generate_const_filter(|_| csharp_generate_const)
#[deprecated(note = "User csharp_generate_const_filter instead")]
pub fn csharp_generate_const(mut self, csharp_generate_const: bool) -> Builder {
pub fn csharp_generate_const(self, csharp_generate_const: bool) -> Builder {
self.csharp_generate_const_filter(if csharp_generate_const { |_| true } else { |_| false })
}

Expand Down
16 changes: 7 additions & 9 deletions csbindgen/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ pub fn emit_csharp(
&"return".to_string(),
) {
method_list_string.push_str(
format!(" [UnmanagedFunctionPointer(CallingConvention.Cdecl)]\n")
.as_str(),
" [UnmanagedFunctionPointer(CallingConvention.Cdecl)]\n"
);
method_list_string
.push_str(format!(" public {delegate_method};\n\n").as_str());
Expand All @@ -145,8 +144,7 @@ pub fn emit_csharp(
&p.name,
) {
method_list_string.push_str(
format!(" [UnmanagedFunctionPointer(CallingConvention.Cdecl)]\n")
.as_str(),
" [UnmanagedFunctionPointer(CallingConvention.Cdecl)]\n"
);
method_list_string
.push_str(format!(" public {delegate_method};\n\n").as_str());
Expand Down Expand Up @@ -308,19 +306,19 @@ pub fn emit_csharp(
);

// special case for string, char, ByteStr
if item.value.starts_with("\"") {
if item.value.starts_with('\"') {
type_name = "string".to_string();
} else if item.value.starts_with("\'") {
} else if item.value.starts_with('\'') {
type_name = "char".to_string();
}

if item.value.starts_with("[") {
if item.value.starts_with('[') {
const_string.push_str(
format!(
" {} static ReadOnlySpan<byte> {} => new byte[] {};\n",
accessibility,
escape_csharp_name(item.const_name.as_str()),
item.value.replace("[", "{ ").replace("]", " }")
item.value.replace('[', "{ ").replace(']', " }")
)
.as_str(),
);
Expand All @@ -344,7 +342,7 @@ pub fn emit_csharp(
}
}

let file_header = if options.csharp_file_header.len() > 0 {
let file_header = if !options.csharp_file_header.is_empty() {
options.csharp_file_header.to_string() + "\n"
} else {
"".to_string()
Expand Down
47 changes: 21 additions & 26 deletions csbindgen/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,15 @@ enum FnItem {
/// build a Vec of all Items, unless the Item is a Item::Mod, then append the Item contents of the vect
/// Do this recursively.
/// This is not memory-efficient, would work better with an iterator, but does not seem performance critical.
fn depth_first_module_walk<'a>(ast: &'a Vec<Item>) -> Vec<&'a syn::Item> {
fn depth_first_module_walk(ast: &Vec<Item>) -> Vec<&syn::Item> {
let mut unwrapped_items: Vec<&syn::Item> = vec![];
for item in ast {
match item {
Item::Mod(m) => match &m.content {
Some((_, items)) => {
unwrapped_items.extend(depth_first_module_walk(items));
}
_ => {}
},
_ => {
unwrapped_items.push(item);
if let Item::Mod(m) = item {
if let Some((_, items)) = &m.content {
unwrapped_items.extend(depth_first_module_walk(items));
}
} else {
unwrapped_items.push(item);
}
}

Expand Down Expand Up @@ -119,7 +115,7 @@ fn parse_method(item: FnItem, options: &BindgenOptions) -> Option<ExternMethod>
let mut export_naming = NoMangle;
if !is_foreign_item {
let found = attrs.iter()
.map(|attr| {
.filter_map(|attr| {
let name = &attr.path.segments.last().unwrap().ident;
if name == "no_mangle" {
return Some(NoMangle)
Expand All @@ -130,7 +126,6 @@ fn parse_method(item: FnItem, options: &BindgenOptions) -> Option<ExternMethod>
}
None
})
.flatten()
.next();

if let Some(x) = found {
Expand Down Expand Up @@ -209,7 +204,7 @@ pub fn collect_struct(ast: &syn::File, result: &mut Vec<RustStruct>) {
}
}

if let Some(_) = repr {
if repr.is_some() {
if let syn::Fields::Named(f) = &t.fields {
let struct_name = t.ident.to_string();
let fields = collect_fields(f);
Expand Down Expand Up @@ -274,7 +269,7 @@ fn collect_fields_unnamed(fields: &syn::FieldsUnnamed) -> Vec<FieldMember> {
let name = format!("Item{i}");
let t = parse_type(&field.ty);
result.push(FieldMember {
name: name,
name,
rust_type: t,
});
}
Expand Down Expand Up @@ -314,13 +309,13 @@ pub fn collect_const(ast: &syn::File, result: &mut Vec<RustConst>,filter:fn(cons
syn::Lit::Bool(b) => {
format!("{}", b.value)
}
_ => format!(""),
_ => String::new(),
};

result.push(RustConst {
const_name: const_name,
const_name,
rust_type: t,
value: value,
value,
});
}
}
Expand Down Expand Up @@ -512,7 +507,7 @@ fn parse_type(t: &syn::Type) -> RustType {
};
}
syn::Type::Tuple(t) => {
if t.elems.len() == 0 {
if t.elems.is_empty() {
return RustType {
type_name: "()".to_string(),
type_kind: TypeKind::Normal,
Expand All @@ -535,7 +530,7 @@ fn parse_type(t: &syn::Type) -> RustType {

let ret = match &t.output {
syn::ReturnType::Default => None,
syn::ReturnType::Type(_, t) => Some(Box::new(parse_type(&t))),
syn::ReturnType::Type(_, t) => Some(Box::new(parse_type(t))),
};

return RustType {
Expand All @@ -544,7 +539,7 @@ fn parse_type(t: &syn::Type) -> RustType {
};
}
syn::Type::Reference(t) => {
let result = parse_type(&*t.elem);
let result = parse_type(&t.elem);
let is_mut = t.mutability.is_some();

match result {
Expand All @@ -561,7 +556,7 @@ fn parse_type(t: &syn::Type) -> RustType {
} else {
PointerType::ConstPointer
},
Box::new(parse_type(&*t.elem)),
Box::new(parse_type(&t.elem)),
),
};
}
Expand All @@ -574,7 +569,7 @@ fn parse_type(t: &syn::Type) -> RustType {
} else {
PointerType::ConstPointerPointer
},
Box::new(parse_type(&*t.elem)),
Box::new(parse_type(&t.elem)),
),
};
}
Expand All @@ -587,7 +582,7 @@ fn parse_type(t: &syn::Type) -> RustType {
} else {
PointerType::ConstMutPointerPointer
},
Box::new(parse_type(&*t.elem)),
Box::new(parse_type(&t.elem)),
),
};
}
Expand All @@ -599,7 +594,7 @@ fn parse_type(t: &syn::Type) -> RustType {
type_name: result.type_name,
type_kind: TypeKind::Pointer(
PointerType::ConstPointer,
Box::new(parse_type(&*t.elem)),
Box::new(parse_type(&t.elem)),
),
};
}
Expand Down Expand Up @@ -640,8 +635,8 @@ fn parse_type_path(t: &syn::TypePath) -> RustType {
}
}

return RustType {
RustType {
type_name: last_segment.ident.to_string(),
type_kind: TypeKind::Normal,
};
}
}
Loading

0 comments on commit a0e36cc

Please sign in to comment.