Skip to content

Commit 78adb33

Browse files
nyurikpvdrz
authored andcommitted
Multiple minor lint fixes
Added all detected pedantic lints, and allowed them all to keep track of them because some should be fixed. Also fixed a few minor simple ones.
1 parent 0917399 commit 78adb33

File tree

13 files changed

+87
-64
lines changed

13 files changed

+87
-64
lines changed

.github/workflows/bindgen.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
run: cargo fmt -- --check
3131

3232
- name: Run clippy
33-
run: cargo clippy --tests
33+
run: cargo clippy --all-targets --workspace --exclude bindgen-integration --exclude tests_expectations
3434

3535
msrv:
3636
runs-on: ubuntu-latest

Cargo.toml

+43-10
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,51 @@ syn = "2.0"
4848
tempfile = "3"
4949

5050
[workspace.lints.rust]
51-
# unused_qualifications = "warn"
51+
unused_qualifications = "warn"
5252

5353
[workspace.lints.clippy]
54-
# disallowed-names = "allow"
55-
# manual-c-str-literals = "allow"
56-
# missing-safety-doc = "allow"
57-
# op-ref = "allow"
58-
# ptr-offset-with-cast = "allow"
59-
# too-many-arguments = "allow"
60-
# transmute-int-to-bool = "allow"
61-
# unnecessary-cast = "allow"
62-
# useless-transmute = "allow"
54+
pedantic = { level = "warn", priority = -1 }
55+
56+
cast_possible_truncation = "allow"
57+
cast_possible_wrap = "allow"
58+
cast_precision_loss = "allow"
59+
cast_sign_loss = "allow"
60+
checked_conversions = "allow"
61+
default_trait_access = "allow"
62+
explicit_into_iter_loop = "allow"
63+
flat_map_option = "allow"
64+
ignored_unit_patterns = "allow"
65+
implicit_hasher = "allow"
66+
inconsistent_struct_constructor = "allow"
67+
items_after_statements = "allow"
68+
maybe_infinite_iter = "allow"
69+
missing_errors_doc = "allow"
70+
missing_panics_doc = "allow"
71+
module_name_repetitions = "allow"
72+
must_use_candidate = "allow"
73+
ptr_as_ptr = "allow"
74+
redundant_closure_for_method_calls = "allow"
75+
return_self_not_must_use = "allow"
76+
#should_panic_without_expect = "allow"
77+
similar_names = "allow"
78+
struct_excessive_bools = "allow"
79+
struct_field_names = "allow"
80+
unnecessary_wraps = "allow"
81+
unnested_or_patterns = "allow"
82+
unreadable_literal = "allow"
83+
used_underscore_binding = "allow"
84+
wildcard_imports = "allow"
85+
86+
# TODO
87+
borrow_as_ptr = "allow"
88+
match_same_arms = "allow"
89+
trivially_copy_pass_by_ref = "allow"
90+
needless_pass_by_value = "allow"
91+
unused_self = "allow"
92+
93+
# Theese seem to be ok to ignore for now
94+
enum_glob_use = "allow"
95+
too_many_lines = "allow"
6396

6497
# Config for 'cargo release'
6598
[workspace.metadata.release]

bindgen-tests/tests/parse_callbacks/item_discovery_callback/mod.rs

+7-12
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,10 @@ pub fn compare_item_caches(generated: ItemCache, expected: ItemCache) {
9494
)
9595
});
9696

97-
if found.is_none() {
98-
panic!(
99-
"Missing Expected Item: {:#?}\n in {:#?}",
100-
expected_item, generated
101-
);
102-
}
97+
assert!(
98+
found.is_some(),
99+
"Missing Expected Item: {expected_item:#?}\n in {generated:#?}"
100+
);
103101
}
104102
}
105103

@@ -235,12 +233,9 @@ pub fn compare_alias_info(
235233
let expected_aliased = expected.get(expected_alias_for).unwrap();
236234

237235
// We must have the aliased type in the cache
238-
let generated_aliased =
239-
if let Some(generated_aliased) = generated.get(generated_alias_for) {
240-
generated_aliased
241-
} else {
242-
return false;
243-
};
236+
let Some(generated_aliased) = generated.get(generated_alias_for) else {
237+
return false;
238+
};
244239

245240
compare_item_info(expected_aliased, generated_aliased, expected, generated)
246241
}

bindgen-tests/tests/quickchecking/src/bin.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ fn parse_tests_count(v: &str) -> Result<u64, String> {
4141
// Parse CLI argument input for fuzzed headers output path.
4242
fn parse_path(v: &str) -> Result<PathBuf, String> {
4343
let path = PathBuf::from(v);
44-
match path.is_dir() {
45-
true => Ok(path),
46-
false => Err(String::from("Provided directory path does not exist.")),
44+
if path.is_dir() {
45+
Ok(path)
46+
} else {
47+
Err(String::from("Provided directory path does not exist."))
4748
}
4849
}
4950

bindgen-tests/tests/tests.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ fn should_overwrite_expected() -> bool {
2626
if var == "1" {
2727
return true;
2828
}
29-
if var != "0" && var != "" {
30-
panic!("Invalid value of BINDGEN_OVERWRITE_EXPECTED");
31-
}
29+
assert!(
30+
var == "0" || var == "",
31+
"Invalid value of BINDGEN_OVERWRITE_EXPECTED"
32+
);
3233
}
3334
false
3435
}

bindgen/clang.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1761,7 +1761,7 @@ impl File {
17611761

17621762
fn cxstring_to_string_leaky(s: CXString) -> String {
17631763
if s.data.is_null() {
1764-
return "".to_owned();
1764+
return String::new();
17651765
}
17661766
let c_str = unsafe { CStr::from_ptr(clang_getCString(s) as *const _) };
17671767
c_str.to_string_lossy().into_owned()

bindgen/codegen/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4565,7 +4565,7 @@ impl CodeGenerator for Function {
45654565
FunctionKind::Function => {
45664566
ctx.options().dynamic_library_name.is_some()
45674567
}
4568-
_ => false,
4568+
FunctionKind::Method(_) => false,
45694569
};
45704570

45714571
// Similar to static member variables in a class template, we can't
@@ -5878,7 +5878,7 @@ pub(crate) mod utils {
58785878

58795879
// Check that the mangled name contains the canonical name after the
58805880
// prefix
5881-
if &mangled_name[1..canonical_name.len() + 1] != canonical_name {
5881+
if &mangled_name[1..=canonical_name.len()] != canonical_name {
58825882
return false;
58835883
}
58845884

bindgen/ir/context.rs

+11-15
Original file line numberDiff line numberDiff line change
@@ -2271,21 +2271,17 @@ If you encounter an error missing from this list, please file an issue or a PR!"
22712271
);
22722272
}
22732273
break;
2274-
} else {
2275-
// This is _likely_, but not certainly, a macro that's
2276-
// been placed just before the namespace keyword.
2277-
// Unfortunately, clang tokens don't let us easily see
2278-
// through the ifdef tokens, so we don't know what this
2279-
// token should really be. Instead of panicking though,
2280-
// we warn the user that we assumed the token was blank,
2281-
// and then move on.
2282-
//
2283-
// See also https://github.com/rust-lang/rust-bindgen/issues/1676.
2284-
warn!(
2285-
"Ignored unknown namespace prefix '{}' at {token:?} in {cursor:?}",
2286-
String::from_utf8_lossy(name),
2287-
);
22882274
}
2275+
// This is _likely_, but not certainly, a macro that's
2276+
// been placed just before the namespace keyword.
2277+
// Unfortunately, clang tokens don't let us easily see
2278+
// through the ifdef tokens, so we don't know what this
2279+
// token should really be. Instead of panicking though,
2280+
// we warn the user that we assumed the token was blank,
2281+
// and then move on.
2282+
//
2283+
// See also https://github.com/rust-lang/rust-bindgen/issues/1676.
2284+
warn!("Ignored unknown namespace prefix '{}' at {token:?} in {cursor:?}", String::from_utf8_lossy(name));
22892285
}
22902286
}
22912287
}
@@ -2613,7 +2609,7 @@ If you encounter an error missing from this list, please file an issue or a PR!"
26132609

26142610
/// Call if an opaque array is generated
26152611
pub(crate) fn generated_opaque_array(&self) {
2616-
self.generated_opaque_array.set(true)
2612+
self.generated_opaque_array.set(true);
26172613
}
26182614

26192615
/// Whether we need to generate the opaque array type

bindgen/ir/function.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use std::str::FromStr;
1717

1818
const RUST_DERIVE_FUNPTR_LIMIT: usize = 12;
1919

20-
/// What kind of a function are we looking at?
20+
/// What kind of function are we looking at?
2121
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
2222
pub(crate) enum FunctionKind {
2323
/// A plain, free function.

bindgen/ir/item.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -1856,16 +1856,11 @@ impl Item {
18561856
let parent = ctx.root_module().into();
18571857

18581858
if let Some(id) = ctx.get_type_param(&definition) {
1859-
if let Some(with_id) = with_id {
1860-
return Some(ctx.build_ty_wrapper(
1861-
with_id,
1862-
id,
1863-
Some(parent),
1864-
&ty,
1865-
));
1859+
return Some(if let Some(with_id) = with_id {
1860+
ctx.build_ty_wrapper(with_id, id, Some(parent), &ty)
18661861
} else {
1867-
return Some(id);
1868-
}
1862+
id
1863+
});
18691864
}
18701865

18711866
// See tests/headers/const_tparam.hpp and

bindgen/ir/ty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -504,15 +504,15 @@ fn is_invalid_type_param_invalid_remaining() {
504504
}
505505

506506
#[test]
507-
#[should_panic]
507+
#[should_panic(expected = "Unnamed named type")]
508508
fn is_invalid_type_param_unnamed() {
509509
let ty = Type::new(None, None, TypeKind::TypeParam, false);
510510
assert!(ty.is_invalid_type_param());
511511
}
512512

513513
#[test]
514514
fn is_invalid_type_param_empty_name() {
515-
let ty = Type::new(Some("".into()), None, TypeKind::TypeParam, false);
515+
let ty = Type::new(Some(String::new()), None, TypeKind::TypeParam, false);
516516
assert!(ty.is_invalid_type_param());
517517
}
518518

bindgen/lib.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,12 @@ pub const DEFAULT_ANON_FIELDS_PREFIX: &str = "__bindgen_anon_";
8686
const DEFAULT_NON_EXTERN_FNS_SUFFIX: &str = "__extern";
8787

8888
fn file_is_cpp(name_file: &str) -> bool {
89-
name_file.ends_with(".hpp") ||
90-
name_file.ends_with(".hxx") ||
91-
name_file.ends_with(".hh") ||
92-
name_file.ends_with(".h++")
89+
Path::new(name_file).extension().map_or(false, |ext| {
90+
ext.eq_ignore_ascii_case("hpp") ||
91+
ext.eq_ignore_ascii_case("hxx") ||
92+
ext.eq_ignore_ascii_case("hh") ||
93+
ext.eq_ignore_ascii_case("h++")
94+
})
9395
}
9496

9597
fn args_are_cpp(clang_args: &[Box<str>]) -> bool {

bindgen/time.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl<'a> Timer<'a> {
2929

3030
/// Returns the time elapsed since the timer's creation
3131
pub fn elapsed(&self) -> Duration {
32-
Instant::now() - self.start
32+
self.start.elapsed()
3333
}
3434

3535
fn print_elapsed(&mut self) {

0 commit comments

Comments
 (0)