Fix clippy and track in CI

`flake check` will run clippy.
This commit is contained in:
John Ericson 2025-12-15 19:48:30 -05:00
parent 654addc555
commit 351a206776
13 changed files with 82 additions and 50 deletions

View file

@ -8,3 +8,12 @@ members = [
"nix-bindings-util", "nix-bindings-util",
] ]
resolver = "2" resolver = "2"
[workspace.lints.rust]
warnings = "deny"
dead-code = "allow"
[workspace.lints.clippy]
type-complexity = "allow"
# We're still trying to make Nix more thread-safe, want forward-compat
arc-with-non-send-sync = "allow"

View file

@ -148,7 +148,7 @@
key = "nix-bindings-rust-add-checks"; key = "nix-bindings-rust-add-checks";
config.checks = lib.concatMapAttrs ( config.checks = lib.concatMapAttrs (
k: v: k: v:
lib.optionalAttrs (lib.strings.hasPrefix "nix-bindings-" k && !lib.strings.hasSuffix "-clippy" k) { lib.optionalAttrs (lib.strings.hasPrefix "nix-bindings-" k) {
"dependency-${k}" = v; "dependency-${k}" = v;
} }
) nix-bindings-rust-perSystemConfig.config.checks; ) nix-bindings-rust-perSystemConfig.config.checks;

View file

@ -16,3 +16,6 @@ lazy_static = "1.4"
ctor = "0.2" ctor = "0.2"
tempfile = "3.10" tempfile = "3.10"
cstr = "0.2" cstr = "0.2"
[lints]
workspace = true

View file

@ -1382,7 +1382,7 @@ mod tests {
let a = es.eval_from_string("2", "<test>").unwrap(); let a = es.eval_from_string("2", "<test>").unwrap();
let v = es.new_value_apply(&f, &a).unwrap(); let v = es.new_value_apply(&f, &a).unwrap();
let t = es.value_type_unforced(&v); let t = es.value_type_unforced(&v);
assert!(t == None); assert!(t.is_none());
let i = es.require_int(&v).unwrap(); let i = es.require_int(&v).unwrap();
assert!(i == 3); assert!(i == 3);
}) })
@ -1398,9 +1398,9 @@ mod tests {
let a = es.eval_from_string("true", "<test>").unwrap(); let a = es.eval_from_string("true", "<test>").unwrap();
let v = es.new_value_apply(&f, &a).unwrap(); let v = es.new_value_apply(&f, &a).unwrap();
let t = es.value_type_unforced(&v); let t = es.value_type_unforced(&v);
assert!(t == None); assert!(t.is_none());
let i = es.require_bool(&v).unwrap(); let i = es.require_bool(&v).unwrap();
assert!(i == false); assert!(!i);
}) })
.unwrap(); .unwrap();
} }
@ -1423,7 +1423,7 @@ mod tests {
let mut es = EvalState::new(store, []).unwrap(); let mut es = EvalState::new(store, []).unwrap();
let v = make_thunk(&mut es, "1"); let v = make_thunk(&mut es, "1");
let t = es.value_type_unforced(&v); let t = es.value_type_unforced(&v);
assert!(t == None); assert!(t.is_none());
}) })
.unwrap(); .unwrap();
} }
@ -1450,7 +1450,7 @@ mod tests {
let mut es = EvalState::new(store, []).unwrap(); let mut es = EvalState::new(store, []).unwrap();
let v = make_thunk(&mut es, "{ a = 1; b = 2; }"); let v = make_thunk(&mut es, "{ a = 1; b = 2; }");
let t = es.value_type_unforced(&v); let t = es.value_type_unforced(&v);
assert!(t == None); assert!(t.is_none());
let attrs = es.require_attrs_names_unsorted(&v).unwrap(); let attrs = es.require_attrs_names_unsorted(&v).unwrap();
assert_eq!(attrs.len(), 2); assert_eq!(attrs.len(), 2);
}) })
@ -1507,7 +1507,7 @@ mod tests {
let s = format!("{e:#}"); let s = format!("{e:#}");
if !s.contains("attribute `c` not found") { if !s.contains("attribute `c` not found") {
eprintln!("unexpected error message: {}", s); eprintln!("unexpected error message: {}", s);
assert!(false); panic!();
} }
} }
} }
@ -1542,7 +1542,7 @@ mod tests {
Err(e) => { Err(e) => {
if !e.to_string().contains("oh no the error") { if !e.to_string().contains("oh no the error") {
eprintln!("unexpected error message: {}", e); eprintln!("unexpected error message: {}", e);
assert!(false); panic!();
} }
} }
} }
@ -1594,7 +1594,7 @@ mod tests {
Err(e) => { Err(e) => {
if !e.to_string().contains("oh no the error") { if !e.to_string().contains("oh no the error") {
eprintln!("unexpected error message: {}", e); eprintln!("unexpected error message: {}", e);
assert!(false); panic!();
} }
} }
} }
@ -1730,7 +1730,7 @@ mod tests {
let t = es.value_type_unforced(&v); let t = es.value_type_unforced(&v);
assert!(t == Some(ValueType::String)); assert!(t == Some(ValueType::String));
let s = es.require_string(&v).unwrap(); let s = es.require_string(&v).unwrap();
assert!(s == ""); assert!(s.is_empty());
}) })
.unwrap(); .unwrap();
} }
@ -1746,7 +1746,7 @@ mod tests {
Err(e) => { Err(e) => {
if !e.to_string().contains("contains null byte") { if !e.to_string().contains("contains null byte") {
eprintln!("{}", e); eprintln!("{}", e);
assert!(false); panic!();
} }
} }
} }
@ -1832,7 +1832,7 @@ mod tests {
let list: Vec<Value> = es.require_list_strict(&v).unwrap(); let list: Vec<Value> = es.require_list_strict(&v).unwrap();
assert_eq!(list.len(), 2); assert_eq!(list.len(), 2);
assert_eq!(es.require_int(&list[0]).unwrap(), 42); assert_eq!(es.require_int(&list[0]).unwrap(), 42);
assert_eq!(es.require_bool(&list[1]).unwrap(), true); assert!(es.require_bool(&list[1]).unwrap());
}) })
.unwrap(); .unwrap();
} }
@ -1971,7 +1971,7 @@ mod tests {
let f = es.eval_from_string("x: x + 1", "<test>").unwrap(); let f = es.eval_from_string("x: x + 1", "<test>").unwrap();
let a = es.eval_from_string("2", "<test>").unwrap(); let a = es.eval_from_string("2", "<test>").unwrap();
let v = es.new_value_apply(&f, &a).unwrap(); let v = es.new_value_apply(&f, &a).unwrap();
assert!(es.value_type_unforced(&v) == None); assert!(es.value_type_unforced(&v).is_none());
es.force(&v).unwrap(); es.force(&v).unwrap();
let t = es.value_type_unforced(&v); let t = es.value_type_unforced(&v);
assert!(t == Some(ValueType::Int)); assert!(t == Some(ValueType::Int));
@ -1994,7 +1994,7 @@ mod tests {
Err(e) => { Err(e) => {
if !e.to_string().contains("cannot coerce") { if !e.to_string().contains("cannot coerce") {
eprintln!("{}", e); eprintln!("{}", e);
assert!(false); panic!();
} }
} }
} }
@ -2017,7 +2017,7 @@ mod tests {
Err(e) => { Err(e) => {
if !e.to_string().contains("expected an integer but found") { if !e.to_string().contains("expected an integer but found") {
eprintln!("{}", e); eprintln!("{}", e);
assert!(false); panic!();
} }
} }
} }
@ -2041,7 +2041,7 @@ mod tests {
Err(e) => { Err(e) => {
if !e.to_string().contains("cannot coerce") { if !e.to_string().contains("cannot coerce") {
eprintln!("{}", e); eprintln!("{}", e);
assert!(false); panic!();
} }
} }
} }
@ -2063,7 +2063,7 @@ mod tests {
Err(e) => { Err(e) => {
if !e.to_string().contains("called without required argument") { if !e.to_string().contains("called without required argument") {
eprintln!("{}", e); eprintln!("{}", e);
assert!(false); panic!();
} }
} }
} }
@ -2086,7 +2086,7 @@ mod tests {
Err(e) => { Err(e) => {
if !e.to_string().contains("called without required argument") { if !e.to_string().contains("called without required argument") {
eprintln!("{}", e); eprintln!("{}", e);
assert!(false); panic!();
} }
} }
} }
@ -2111,7 +2111,7 @@ mod tests {
Err(e) => { Err(e) => {
if !e.to_string().contains("called without required argument") { if !e.to_string().contains("called without required argument") {
eprintln!("{}", e); eprintln!("{}", e);
assert!(false); panic!();
} }
} }
} }
@ -2179,8 +2179,7 @@ mod tests {
for derivation in derivations { for derivation in derivations {
assert!(store_contents assert!(store_contents
.iter() .iter()
.find(|f| f.as_encoded_bytes().ends_with(derivation)) .any(|f| f.as_encoded_bytes().ends_with(derivation)));
.is_some());
} }
assert!(!empty(read_dir(state.path()).unwrap())); assert!(!empty(read_dir(state.path()).unwrap()));
@ -2214,7 +2213,7 @@ mod tests {
let a = es.require_int(a)?; let a = es.require_int(a)?;
let b = es.require_int(b)?; let b = es.require_int(b)?;
let c = *bias.lock().unwrap(); let c = *bias.lock().unwrap();
Ok(es.new_value_int(a + b + c)?) es.new_value_int(a + b + c)
}), }),
) )
.unwrap(); .unwrap();
@ -2268,7 +2267,7 @@ mod tests {
Err(e) => { Err(e) => {
if !e.to_string().contains("error with arg [2]") { if !e.to_string().contains("error with arg [2]") {
eprintln!("unexpected error message: {}", e); eprintln!("unexpected error message: {}", e);
assert!(false); panic!();
} }
} }
} }
@ -2284,7 +2283,7 @@ mod tests {
let v = es let v = es
.new_value_thunk( .new_value_thunk(
"test_thunk", "test_thunk",
Box::new(move |es: &mut EvalState| Ok(es.new_value_int(42)?)), Box::new(move |es: &mut EvalState| es.new_value_int(42)),
) )
.unwrap(); .unwrap();
es.force(&v).unwrap(); es.force(&v).unwrap();
@ -2318,11 +2317,11 @@ mod tests {
"error message in test case eval_state_primop_anon_call_no_args_lazy", "error message in test case eval_state_primop_anon_call_no_args_lazy",
) { ) {
eprintln!("unexpected error message: {}", e); eprintln!("unexpected error message: {}", e);
assert!(false); panic!();
} }
if !e.to_string().contains("test_thunk") { if !e.to_string().contains("test_thunk") {
eprintln!("unexpected error message: {}", e); eprintln!("unexpected error message: {}", e);
assert!(false); panic!();
} }
} }
} }
@ -2345,7 +2344,7 @@ mod tests {
Box::new(|es, args| { Box::new(|es, args| {
let a = es.require_int(&args[0])?; let a = es.require_int(&args[0])?;
let b = es.require_int(&args[1])?; let b = es.require_int(&args[1])?;
Ok(es.new_value_int(a + b)?) es.new_value_int(a + b)
}), }),
) )
.unwrap(); .unwrap();
@ -2385,11 +2384,11 @@ mod tests {
Err(e) => { Err(e) => {
if !e.to_string().contains("The frob unexpectedly fizzled") { if !e.to_string().contains("The frob unexpectedly fizzled") {
eprintln!("unexpected error message: {}", e); eprintln!("unexpected error message: {}", e);
assert!(false); panic!();
} }
if !e.to_string().contains("frobnicate") { if !e.to_string().contains("frobnicate") {
eprintln!("unexpected error message: {}", e); eprintln!("unexpected error message: {}", e);
assert!(false); panic!();
} }
} }
} }

View file

@ -3,11 +3,19 @@ use super::Value;
use nix_bindings_bindgen_raw as raw; use nix_bindings_bindgen_raw as raw;
/// See [Value::new]. /// See [Value::new].
///
/// # Safety
///
/// See underlying function.
pub unsafe fn raw_value_new(ptr: *mut raw::Value) -> Value { pub unsafe fn raw_value_new(ptr: *mut raw::Value) -> Value {
Value::new(ptr) Value::new(ptr)
} }
/// See [Value::new_borrowed]. /// See [Value::new_borrowed].
///
/// # Safety
///
/// See underlying function.
pub unsafe fn raw_value_new_borrowed(ptr: *mut raw::Value) -> Value { pub unsafe fn raw_value_new_borrowed(ptr: *mut raw::Value) -> Value {
Value::new_borrowed(ptr) Value::new_borrowed(ptr)
} }

View file

@ -15,3 +15,6 @@ nix-bindings-bindgen-raw = { path = "../nix-bindings-bindgen-raw" }
ctor = "0.2" ctor = "0.2"
tempfile = "3.10" tempfile = "3.10"
cstr = "0.2" cstr = "0.2"
[lints]
workspace = true

View file

@ -18,3 +18,6 @@ lazy_static = "1.4"
ctor = "0.2" ctor = "0.2"
tempfile = "3.10" tempfile = "3.10"
cstr = "0.2" cstr = "0.2"
[lints]
workspace = true

View file

@ -138,7 +138,7 @@ impl FlakeReference {
}?; }?;
let ptr = NonNull::new(ptr) let ptr = NonNull::new(ptr)
.context("flake_reference_and_fragment_from_string unexpectedly returned null")?; .context("flake_reference_and_fragment_from_string unexpectedly returned null")?;
Ok((FlakeReference { ptr: ptr }, r?)) Ok((FlakeReference { ptr }, r?))
} }
} }
@ -293,7 +293,7 @@ mod tests {
let b = eval_state.require_bool(&v).unwrap(); let b = eval_state.require_bool(&v).unwrap();
assert_eq!(b, true); assert!(b);
drop(gc_registration); drop(gc_registration);
} }
@ -353,7 +353,7 @@ mod tests {
.outputs(&flake_settings, &mut eval_state) .outputs(&flake_settings, &mut eval_state)
.unwrap(); .unwrap();
let hello = eval_state.require_attrs_select(&outputs, &"hello").unwrap(); let hello = eval_state.require_attrs_select(&outputs, "hello").unwrap();
let hello = eval_state.require_string(&hello).unwrap(); let hello = eval_state.require_string(&hello).unwrap();
assert_eq!(hello, "potato"); assert_eq!(hello, "potato");
@ -394,7 +394,7 @@ mod tests {
// a // a
std::fs::write( std::fs::write(
&tmp_dir.path().join("a/flake.nix"), tmp_dir.path().join("a/flake.nix"),
r#" r#"
{ {
inputs.b.url = "@flake_dir_b@"; inputs.b.url = "@flake_dir_b@";
@ -409,7 +409,7 @@ mod tests {
// b // b
std::fs::write( std::fs::write(
&tmp_dir.path().join("b/flake.nix"), tmp_dir.path().join("b/flake.nix"),
r#" r#"
{ {
outputs = { ... }: { outputs = { ... }: {
@ -422,7 +422,7 @@ mod tests {
// c // c
std::fs::write( std::fs::write(
&tmp_dir.path().join("c/flake.nix"), tmp_dir.path().join("c/flake.nix"),
r#" r#"
{ {
outputs = { ... }: { outputs = { ... }: {
@ -486,7 +486,7 @@ mod tests {
.outputs(&flake_settings, &mut eval_state) .outputs(&flake_settings, &mut eval_state)
.unwrap(); .unwrap();
let hello = eval_state.require_attrs_select(&outputs, &"hello").unwrap(); let hello = eval_state.require_attrs_select(&outputs, "hello").unwrap();
let hello = eval_state.require_string(&hello).unwrap(); let hello = eval_state.require_string(&hello).unwrap();
assert_eq!(hello, "BOB"); assert_eq!(hello, "BOB");
@ -527,7 +527,7 @@ mod tests {
let outputs = locked_flake let outputs = locked_flake
.outputs(&flake_settings, &mut eval_state) .outputs(&flake_settings, &mut eval_state)
.unwrap(); .unwrap();
let hello = eval_state.require_attrs_select(&outputs, &"hello").unwrap(); let hello = eval_state.require_attrs_select(&outputs, "hello").unwrap();
let hello = eval_state.require_string(&hello).unwrap(); let hello = eval_state.require_string(&hello).unwrap();
assert_eq!(hello, "BOB"); assert_eq!(hello, "BOB");
@ -547,7 +547,7 @@ mod tests {
let outputs = locked_flake let outputs = locked_flake
.outputs(&flake_settings, &mut eval_state) .outputs(&flake_settings, &mut eval_state)
.unwrap(); .unwrap();
let hello = eval_state.require_attrs_select(&outputs, &"hello").unwrap(); let hello = eval_state.require_attrs_select(&outputs, "hello").unwrap();
let hello = eval_state.require_string(&hello).unwrap(); let hello = eval_state.require_string(&hello).unwrap();
assert_eq!(hello, "BOB"); assert_eq!(hello, "BOB");
@ -581,7 +581,7 @@ mod tests {
let outputs = locked_flake let outputs = locked_flake
.outputs(&flake_settings, &mut eval_state) .outputs(&flake_settings, &mut eval_state)
.unwrap(); .unwrap();
let hello = eval_state.require_attrs_select(&outputs, &"hello").unwrap(); let hello = eval_state.require_attrs_select(&outputs, "hello").unwrap();
let hello = eval_state.require_string(&hello).unwrap(); let hello = eval_state.require_string(&hello).unwrap();
assert_eq!(hello, "Claire"); assert_eq!(hello, "Claire");
@ -604,7 +604,7 @@ mod tests {
let outputs = locked_flake let outputs = locked_flake
.outputs(&flake_settings, &mut eval_state) .outputs(&flake_settings, &mut eval_state)
.unwrap(); .unwrap();
let hello = eval_state.require_attrs_select(&outputs, &"hello").unwrap(); let hello = eval_state.require_attrs_select(&outputs, "hello").unwrap();
let hello = eval_state.require_string(&hello).unwrap(); let hello = eval_state.require_string(&hello).unwrap();
assert_eq!(hello, "BOB"); assert_eq!(hello, "BOB");

View file

@ -22,3 +22,6 @@ tempfile = "3.10"
pkg-config = "0.3" pkg-config = "0.3"
# Needed for version parsing in build.rs # Needed for version parsing in build.rs
nix-bindings-util = { path = "../nix-bindings-util" } nix-bindings-util = { path = "../nix-bindings-util" }
[lints]
workspace = true

View file

@ -514,7 +514,7 @@ mod tests {
#[test] #[test]
fn parse_store_path_fail() { fn parse_store_path_fail() {
let mut store = crate::store::Store::open(Some("dummy://"), []).unwrap(); let mut store = crate::store::Store::open(Some("dummy://"), []).unwrap();
let store_path_string = format!("bash-interactive-5.2p26"); let store_path_string = "bash-interactive-5.2p26".to_string();
let r = store.parse_store_path(store_path_string.as_str()); let r = store.parse_store_path(store_path_string.as_str());
match r { match r {
Err(e) => { Err(e) => {

View file

@ -11,3 +11,6 @@ path = "src/lib.rs"
anyhow = "1.0" anyhow = "1.0"
nix-bindings-bindgen-raw = { path = "../nix-bindings-bindgen-raw" } nix-bindings-bindgen-raw = { path = "../nix-bindings-bindgen-raw" }
ctor = "0.2" ctor = "0.2"
[lints]
workspace = true

View file

@ -1,6 +1,5 @@
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use nix_bindings_bindgen_raw as raw; use nix_bindings_bindgen_raw as raw;
use std::os::raw::c_char;
use std::ptr::null_mut; use std::ptr::null_mut;
use std::ptr::NonNull; use std::ptr::NonNull;
@ -11,6 +10,12 @@ pub struct Context {
inner: NonNull<raw::c_context>, inner: NonNull<raw::c_context>,
} }
impl Default for Context {
fn default() -> Self {
Self::new()
}
}
impl Context { impl Context {
pub fn new() -> Self { pub fn new() -> Self {
let ctx = unsafe { raw::c_context_create() }; let ctx = unsafe { raw::c_context_create() };
@ -48,11 +53,7 @@ impl Context {
pub fn clear(&mut self) { pub fn clear(&mut self) {
unsafe { unsafe {
raw::set_err_msg( raw::set_err_msg(self.inner.as_ptr(), raw::err_NIX_OK, c"".as_ptr());
self.inner.as_ptr(),
raw::err_NIX_OK,
b"\0".as_ptr() as *const c_char,
);
} }
} }
@ -143,8 +144,8 @@ mod tests {
unsafe { unsafe {
raw::set_err_msg( raw::set_err_msg(
ctx_ptr, ctx_ptr,
raw::err_NIX_ERR_UNKNOWN.try_into().unwrap(), raw::err_NIX_ERR_UNKNOWN,
b"dummy error message\0".as_ptr() as *const std::os::raw::c_char, c"dummy error message".as_ptr(),
); );
} }
} }

View file

@ -65,7 +65,7 @@ pub fn parse_version(version_str: &str) -> (u32, u32, i32) {
let parts = version_str.split('.').collect::<Vec<&str>>(); let parts = version_str.split('.').collect::<Vec<&str>>();
let major = parts[0].parse::<u32>().unwrap(); let major = parts[0].parse::<u32>().unwrap();
let minor = parts[1].parse::<u32>().unwrap(); let minor = parts[1].parse::<u32>().unwrap();
let patch = if parts.get(2).map_or(false, |s| s.contains("pre")) { let patch = if parts.get(2).is_some_and(|s| s.contains("pre")) {
-1i32 -1i32
} else { } else {
parts parts