refactor(ffi): audit and remove unjustified #[allow(dead_code)] annotations

Every reachable pub(crate) helper (convert/ helpers, tree/flatten::flatten_tree,
error::{set_last_error, clear_last_error, last_error_code, error_code_to_result})
now stands without the annotation — these are all called from #[no_mangle]
entrypoints and rustc sees them correctly.

Move the three test-only last_error readers (last_error_message_str,
last_error_suggestion_str, last_error_platform_detail_str) plus the
MessageSource::to_owned_string helper into #[cfg(test)] scope. They were
only ever used by tests; having them stand at module scope required the
annotation and muddied the public-surface read.

Zero #[allow(dead_code)] remains in crates/ffi/src/. Tree still compiles,
27 tests still pass, cbindgen header symbol set unchanged.
This commit is contained in:
Lahfir 2026-04-16 03:29:37 -07:00
parent 95c3da2352
commit f28dd95555
7 changed files with 25 additions and 45 deletions

View file

@ -4,7 +4,6 @@ use agent_desktop_core::node::AppInfo;
use std::os::raw::c_char;
use std::ptr;
#[allow(dead_code)]
pub(crate) fn app_info_to_c(a: &AppInfo) -> AdAppInfo {
AdAppInfo {
name: string_to_c(&a.name),
@ -13,7 +12,6 @@ pub(crate) fn app_info_to_c(a: &AppInfo) -> AdAppInfo {
}
}
#[allow(dead_code)]
pub(crate) unsafe fn free_app_info_fields(a: &mut AdAppInfo) {
free_c_string(a.name as *mut c_char);
free_c_string(a.bundle_id as *mut c_char);

View file

@ -1,7 +1,6 @@
use crate::types::AdRect;
use agent_desktop_core::node::Rect;
#[allow(dead_code)]
pub(crate) fn rect_to_c(r: &Rect) -> AdRect {
AdRect {
x: r.x,

View file

@ -2,7 +2,6 @@ use std::ffi::{CStr, CString};
use std::os::raw::c_char;
use std::ptr;
#[allow(dead_code)]
pub(crate) fn string_to_c(s: &str) -> *mut c_char {
match CString::new(s) {
Ok(cs) => cs.into_raw(),
@ -10,7 +9,6 @@ pub(crate) fn string_to_c(s: &str) -> *mut c_char {
}
}
#[allow(dead_code)]
pub(crate) fn opt_string_to_c(s: Option<&str>) -> *mut c_char {
match s {
Some(s) => string_to_c(s),
@ -18,14 +16,12 @@ pub(crate) fn opt_string_to_c(s: Option<&str>) -> *mut c_char {
}
}
#[allow(dead_code)]
pub(crate) unsafe fn free_c_string(ptr: *mut c_char) {
if !ptr.is_null() {
drop(CString::from_raw(ptr));
}
}
#[allow(dead_code)]
pub(crate) unsafe fn c_to_str<'a>(ptr: *const c_char) -> Option<&'a str> {
if ptr.is_null() {
return None;

View file

@ -4,7 +4,6 @@ use agent_desktop_core::node::SurfaceInfo;
use std::os::raw::c_char;
use std::ptr;
#[allow(dead_code)]
pub(crate) fn surface_info_to_c(s: &SurfaceInfo) -> AdSurfaceInfo {
AdSurfaceInfo {
kind: string_to_c(&s.kind),
@ -13,7 +12,6 @@ pub(crate) fn surface_info_to_c(s: &SurfaceInfo) -> AdSurfaceInfo {
}
}
#[allow(dead_code)]
pub(crate) unsafe fn free_surface_info_fields(s: &mut AdSurfaceInfo) {
free_c_string(s.kind as *mut c_char);
free_c_string(s.title as *mut c_char);

View file

@ -5,7 +5,6 @@ use agent_desktop_core::node::WindowInfo;
use std::os::raw::c_char;
use std::ptr;
#[allow(dead_code)]
pub(crate) fn window_info_to_c(w: &WindowInfo) -> AdWindowInfo {
let (bounds, has_bounds) = match &w.bounds {
Some(r) => (rect_to_c(r), true),
@ -30,7 +29,6 @@ pub(crate) fn window_info_to_c(w: &WindowInfo) -> AdWindowInfo {
}
}
#[allow(dead_code)]
pub(crate) unsafe fn free_window_info_fields(w: &mut AdWindowInfo) {
free_c_string(w.id as *mut c_char);
free_c_string(w.title as *mut c_char);

View file

@ -33,6 +33,7 @@ impl MessageSource {
}
}
#[cfg(test)]
fn to_owned_string(&self) -> String {
match self {
MessageSource::Owned(cs) => cs.to_string_lossy().into_owned(),
@ -54,7 +55,6 @@ thread_local! {
static LAST_ERROR: RefCell<Option<StoredError>> = const { RefCell::new(None) };
}
#[allow(dead_code)]
fn error_code_to_result(code: &ErrorCode) -> AdResult {
match code {
ErrorCode::PermDenied => AdResult::ErrPermDenied,
@ -72,7 +72,6 @@ fn error_code_to_result(code: &ErrorCode) -> AdResult {
}
}
#[allow(dead_code)]
pub(crate) fn set_last_error(err: &AdapterError) {
let code = error_code_to_result(&err.code);
let message = match CString::new(err.message.as_str()) {
@ -94,14 +93,12 @@ pub(crate) fn set_last_error(err: &AdapterError) {
});
}
#[allow(dead_code)]
pub(crate) fn clear_last_error() {
LAST_ERROR.with(|cell| {
*cell.borrow_mut() = None;
});
}
#[allow(dead_code)]
pub(crate) fn last_error_code() -> AdResult {
LAST_ERROR.with(|cell| {
cell.borrow()
@ -111,33 +108,6 @@ pub(crate) fn last_error_code() -> AdResult {
})
}
#[allow(dead_code)]
pub(crate) fn last_error_message_str() -> Option<String> {
LAST_ERROR.with(|cell| cell.borrow().as_ref().map(|e| e.message.to_owned_string()))
}
#[allow(dead_code)]
pub(crate) fn last_error_suggestion_str() -> Option<String> {
LAST_ERROR.with(|cell| {
cell.borrow().as_ref().and_then(|e| {
e.suggestion
.as_ref()
.map(|s| s.to_string_lossy().into_owned())
})
})
}
#[allow(dead_code)]
pub(crate) fn last_error_platform_detail_str() -> Option<String> {
LAST_ERROR.with(|cell| {
cell.borrow().as_ref().and_then(|e| {
e.platform_detail
.as_ref()
.map(|s| s.to_string_lossy().into_owned())
})
})
}
#[no_mangle]
pub extern "C" fn ad_last_error_code() -> AdResult {
last_error_code()
@ -177,6 +147,30 @@ pub extern "C" fn ad_last_error_platform_detail() -> *const c_char {
mod tests {
use super::*;
fn last_error_message_str() -> Option<String> {
LAST_ERROR.with(|cell| cell.borrow().as_ref().map(|e| e.message.to_owned_string()))
}
fn last_error_suggestion_str() -> Option<String> {
LAST_ERROR.with(|cell| {
cell.borrow().as_ref().and_then(|e| {
e.suggestion
.as_ref()
.map(|s| s.to_string_lossy().into_owned())
})
})
}
fn last_error_platform_detail_str() -> Option<String> {
LAST_ERROR.with(|cell| {
cell.borrow().as_ref().and_then(|e| {
e.platform_detail
.as_ref()
.map(|s| s.to_string_lossy().into_owned())
})
})
}
#[test]
fn test_no_error_initially() {
clear_last_error();

View file

@ -4,7 +4,6 @@ use agent_desktop_core::node::AccessibilityNode;
use std::os::raw::c_char;
use std::ptr;
#[allow(dead_code)]
pub(crate) fn flatten_tree(root: &AccessibilityNode) -> AdNodeTree {
let mut flat: Vec<AdNode> = Vec::new();
flatten_recursive(root, -1, &mut flat);
@ -20,7 +19,6 @@ pub(crate) fn flatten_tree(root: &AccessibilityNode) -> AdNodeTree {
AdNodeTree { nodes, count }
}
#[allow(dead_code)]
fn flatten_recursive(node: &AccessibilityNode, parent_index: i32, flat: &mut Vec<AdNode>) {
let my_index = flat.len() as i32;
@ -62,7 +60,6 @@ fn flatten_recursive(node: &AccessibilityNode, parent_index: i32, flat: &mut Vec
}
}
#[allow(dead_code)]
fn strings_to_c_array(strings: &[String]) -> (*mut *mut c_char, u32) {
if strings.is_empty() {
return (ptr::null_mut(), 0);