clippy: fix warnings in various modules in components (#31568)

* clippy: fix warnings in various modules in components

* fix: unit tests

* fix: build on android

* fix: all samplers use new_boxed
This commit is contained in:
eri 2024-03-08 15:28:04 +01:00 committed by GitHub
parent 19f1f2a8f4
commit 3a5ca785d3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
24 changed files with 107 additions and 118 deletions

View file

@ -92,19 +92,19 @@ impl BackgroundHangMonitorRegister for HangMonitorRegister {
target_os = "windows",
any(target_arch = "x86_64", target_arch = "x86")
))]
let sampler = crate::sampler_windows::WindowsSampler::new();
let sampler = crate::sampler_windows::WindowsSampler::new_boxed();
#[cfg(target_os = "macos")]
let sampler = crate::sampler_mac::MacOsSampler::new();
let sampler = crate::sampler_mac::MacOsSampler::new_boxed();
#[cfg(all(
target_os = "linux",
not(any(target_arch = "arm", target_arch = "aarch64"))
))]
let sampler = crate::sampler_linux::LinuxSampler::new();
let sampler = crate::sampler_linux::LinuxSampler::new_boxed();
#[cfg(any(
target_os = "android",
all(target_os = "linux", any(target_arch = "arm", target_arch = "aarch64"))
))]
let sampler = crate::sampler::DummySampler::new();
let sampler = crate::sampler::DummySampler::new_boxed();
// When a component is registered, and there's an exit request that
// reached BHM, we want an exit signal to be delivered to the
@ -208,7 +208,7 @@ impl BackgroundHangMonitorChan {
BackgroundHangMonitorChan {
sender,
_tether: tether,
component_id: component_id,
component_id,
disconnected: Default::default(),
monitoring_enabled,
}
@ -312,14 +312,14 @@ struct BackgroundHangMonitorWorker {
monitoring_enabled: bool,
}
type MonitoredComponentSender = Sender<(MonitoredComponentId, MonitoredComponentMsg)>;
type MonitoredComponentReceiver = Receiver<(MonitoredComponentId, MonitoredComponentMsg)>;
impl BackgroundHangMonitorWorker {
fn new(
constellation_chan: IpcSender<HangMonitorAlert>,
control_port: IpcReceiver<BackgroundHangMonitorControlMsg>,
(port_sender, port): (
Arc<Sender<(MonitoredComponentId, MonitoredComponentMsg)>>,
Receiver<(MonitoredComponentId, MonitoredComponentMsg)>,
),
(port_sender, port): (Arc<MonitoredComponentSender>, MonitoredComponentReceiver),
tether_port: Receiver<Never>,
monitoring_enabled: bool,
) -> Self {
@ -360,7 +360,7 @@ impl BackgroundHangMonitorWorker {
let profile = stack.to_hangprofile();
let name = match self.component_names.get(&id) {
Some(ref s) => format!("\"{}\"", s),
None => format!("null"),
None => "null".to_string(),
};
let json = format!(
"{}{{ \"name\": {}, \"namespace\": {}, \"index\": {}, \"type\": \"{:?}\", \
@ -389,12 +389,10 @@ impl BackgroundHangMonitorWorker {
.checked_sub(Instant::now() - self.last_sample)
.unwrap_or_else(|| Duration::from_millis(0));
after(duration)
} else if self.monitoring_enabled {
after(Duration::from_millis(100))
} else {
if self.monitoring_enabled {
after(Duration::from_millis(100))
} else {
never()
}
never()
};
let received = select! {
@ -403,13 +401,8 @@ impl BackgroundHangMonitorWorker {
// gets disconnected.
Some(event.unwrap())
},
recv(self.tether_port) -> event => {
recv(self.tether_port) -> _ => {
// This arm can only reached by a tether disconnection
match event {
Ok(x) => match x {}
Err(_) => {}
}
// All associated `HangMonitorRegister` and
// `BackgroundHangMonitorChan` have been dropped. Suppress
// `signal_to_exit` and exit the BHM.

View file

@ -4,7 +4,6 @@
use std::ptr;
use backtrace;
use msg::constellation_msg::{HangProfile, HangProfileSymbol};
const MAX_NATIVE_FRAMES: usize = 1024;
@ -18,7 +17,7 @@ pub struct DummySampler;
impl DummySampler {
#[allow(dead_code)]
pub fn new() -> Box<dyn Sampler> {
pub fn new_boxed() -> Box<dyn Sampler> {
Box::new(DummySampler)
}
}
@ -64,12 +63,12 @@ impl NativeStack {
instruction_ptr: *mut std::ffi::c_void,
stack_ptr: *mut std::ffi::c_void,
) -> Result<(), ()> {
if !(self.count < MAX_NATIVE_FRAMES) {
if self.count >= MAX_NATIVE_FRAMES {
return Err(());
}
self.instruction_ptrs[self.count] = instruction_ptr;
self.stack_ptrs[self.count] = stack_ptr;
self.count = self.count + 1;
self.count += 1;
Ok(())
}
@ -85,7 +84,7 @@ impl NativeStack {
// TODO: use the demangled or C++ demangled symbols if available.
let name = symbol
.name()
.map(|n| String::from_utf8_lossy(&n.as_bytes()).to_string());
.map(|n| String::from_utf8_lossy(n.as_bytes()).to_string());
let filename = symbol.filename().map(|n| n.to_string_lossy().to_string());
let lineno = symbol.lineno();
profile.backtrace.push(HangProfileSymbol {

View file

@ -6,9 +6,8 @@
use std::cell::UnsafeCell;
use std::sync::atomic::{AtomicPtr, Ordering};
use std::{io, mem, process, ptr, thread};
use std::{cmp, io, mem, process, ptr, thread};
use libc;
use nix::sys::signal::{sigaction, SaFlags, SigAction, SigHandler, SigSet, Signal};
use unwind_sys::{
unw_cursor_t, unw_get_reg, unw_init_local, unw_step, UNW_ESUCCESS, UNW_REG_IP, UNW_REG_SP,
@ -138,7 +137,7 @@ pub struct LinuxSampler {
impl LinuxSampler {
#[allow(unsafe_code, dead_code)]
pub fn new() -> Box<dyn Sampler> {
pub fn new_boxed() -> Box<dyn Sampler> {
let thread_id = unsafe { libc::syscall(libc::SYS_gettid) as libc::pid_t };
let handler = SigHandler::SigAction(sigprof_handler);
let action = SigAction::new(
@ -181,12 +180,10 @@ fn step(cursor: *mut unw_cursor_t) -> Result<bool, i32> {
}
let ret = unw_step(cursor);
if ret > 0 {
Ok(true)
} else if ret == 0 {
Ok(false)
} else {
Err(ret)
match ret.cmp(&0) {
cmp::Ordering::Less => Err(ret),
cmp::Ordering::Greater => Ok(true),
cmp::Ordering::Equal => Ok(false),
}
}
}
@ -222,6 +219,7 @@ impl Sampler for LinuxSampler {
let ret = unsafe { unw_init_local(cursor.as_mut_ptr(), context) };
result = if ret == UNW_ESUCCESS {
let mut native_stack = NativeStack::new();
#[allow(clippy::while_let_loop)] // False positive
loop {
let ip = match get_register(cursor.as_mut_ptr(), RegNum::Ip) {
Ok(ip) => ip,

View file

@ -16,7 +16,7 @@ pub struct MacOsSampler {
impl MacOsSampler {
#[allow(unsafe_code)]
pub fn new() -> Box<dyn Sampler> {
pub fn new_boxed() -> Box<dyn Sampler> {
let thread_id = unsafe { mach2::mach_init::mach_thread_self() };
Box::new(MacOsSampler { thread_id })
}

View file

@ -13,7 +13,7 @@ pub struct WindowsSampler {
impl WindowsSampler {
#[allow(unsafe_code, dead_code)]
pub fn new() -> Box<dyn Sampler> {
pub fn new_boxed() -> Box<dyn Sampler> {
let thread_id = 0; // TODO: use winapi::um::processthreadsapi::GetThreadId
Box::new(WindowsSampler { thread_id })
}