use super::super::FfiBackend;
use super::common::*;
use crate::core::backend::Backend;
use crate::core::ir::*;
#[test]
fn crate_level_allow_list_does_not_carry_dead_entries() {
let api = ApiSurface {
crate_name: "sample_lib".to_string(),
version: "0.1.0".to_string(),
functions: vec![FunctionDef {
name: "run".to_string(),
rust_path: "sample_lib::run".to_string(),
return_type: TypeRef::Unit,
..FunctionDef::default()
}],
..ApiSurface::default()
};
let config = resolved_one(
r#"
[workspace]
languages = ["ffi"]
[[crates]]
name = "sample-lib"
sources = ["src/lib.rs"]
[crates.ffi]
prefix = "sample"
"#,
);
let files = FfiBackend.generate_bindings(&api, &config).unwrap();
let lib = files.iter().find(|file| file.path.ends_with("lib.rs")).unwrap();
let header_end = lib.content.find("use std::ffi").unwrap_or(lib.content.len());
let header = &lib.content[..header_end];
assert!(
!header.contains("missing_docs"),
"missing_docs is allow-by-default under rustc and is never escalated by -D warnings \
alone, so the crate-level allow was a no-op:\n{header}"
);
assert!(
!header.contains("clippy::too_many_arguments"),
"too_many_arguments already gets a per-item #[allow] at every site that can exceed \
the threshold (free functions, len companions, method wrappers, constructors, field \
accessors), so the crate-level copy was redundant:\n{header}"
);
assert!(
!header.contains("clippy::useless_conversion"),
"useless_conversion's only source is the Vec<u8>::from(..) polymorphic bytes \
conversion in bytes_result_match.jinja, which now carries its own narrow \
#[allow(clippy::useless_conversion)] at each of its four sites:\n{header}"
);
assert!(
!header.contains("clippy::unnecessary_cast"),
"every cast this backend emits converts a bool or a Named enum to a different \
primitive type, which clippy's same-type check can never flag as redundant:\n{header}"
);
}
#[test]
fn bytes_result_conversion_carries_its_own_narrow_useless_conversion_allow() {
let api = ApiSurface {
crate_name: "sample_lib".to_string(),
version: "0.1.0".to_string(),
functions: vec![FunctionDef {
name: "render".to_string(),
rust_path: "sample_lib::render".to_string(),
return_type: TypeRef::Bytes,
error_type: Some("String".to_string()),
..FunctionDef::default()
}],
..ApiSurface::default()
};
let config = resolved_one(
r#"
[workspace]
languages = ["ffi"]
[[crates]]
name = "sample-lib"
sources = ["src/lib.rs"]
[crates.ffi]
prefix = "sample"
"#,
);
let files = FfiBackend.generate_bindings(&api, &config).unwrap();
let lib = files.iter().find(|file| file.path.ends_with("lib.rs")).unwrap();
assert!(
lib.content
.contains("#[allow(clippy::useless_conversion)]\n let buffer = Vec::<u8>::from(val)"),
"the bytes-conversion call site must keep its own narrow allow now that the \
crate-level one is gone:\n{}",
lib.content
);
}