Fix CI: Resolve all Clippy warnings and improve code quality

- Fixed redundant continue statements in loops
- Replaced manual string prefix stripping with strip_prefix method
- Fixed wildcard pattern that covered other patterns
- Removed redundant clone and unnecessary return value
- Used format string interpolation for cleaner code
- Removed unused imports and variables
- All Clippy checks now pass with -D warnings
This commit is contained in:
Peter Steinberger 2025-06-18 13:13:05 +02:00
parent 68dd22e183
commit ec0bbcfdf4
4 changed files with 28 additions and 31 deletions

View file

@ -681,7 +681,10 @@ fn handle_session_snapshot(control_path: &Path, path: &str) -> Response<String>
let original_lines = content.lines().count();
let optimized_lines = optimized_content.lines().count();
let reduction = if original_lines > 0 {
(original_lines - optimized_lines) as f64 / original_lines as f64 * 100.0
#[allow(clippy::cast_precision_loss)]
{
(original_lines - optimized_lines) as f64 / original_lines as f64 * 100.0
}
} else {
0.0
};
@ -716,7 +719,7 @@ fn handle_session_snapshot(control_path: &Path, path: &str) -> Response<String>
}
fn optimize_snapshot_content(content: &str) -> String {
let lines: Vec<&str> = content.trim().split('\n').collect();
let lines: Vec<&str> = content.lines().collect();
let mut header_line: Option<&str> = None;
let mut all_events: Vec<&str> = Vec::new();
@ -1177,11 +1180,17 @@ fn handle_multi_stream(control_path: &Path, req: &mut HttpRequest) {
};
if let Err(e) = watcher.watch(&control_path_clone, RecursiveMode::NonRecursive) {
println!("Failed to watch control directory {control_path_clone:?}: {e}");
println!(
"Failed to watch control directory {}: {e}",
control_path_clone.display()
);
return;
}
println!("Session discovery thread started, watching {control_path_clone:?}");
println!(
"Session discovery thread started, watching {}",
control_path_clone.display()
);
// Also discover existing sessions at startup
if let Ok(sessions) = sessions::list_sessions(&control_path_clone) {
@ -1313,7 +1322,8 @@ fn handle_multi_stream(control_path: &Path, req: &mut HttpRequest) {
if let Err(e) = watcher.watch(parent_dir, RecursiveMode::NonRecursive) {
println!(
"Failed to watch directory {parent_dir:?} for session {session_id_clone}: {e}"
"Failed to watch directory {} for session {session_id_clone}: {e}",
parent_dir.display()
);
return;
}

View file

@ -194,7 +194,7 @@ fn main() -> Result<(), anyhow::Error> {
// Handle serve command
if let Some(addr) = serve_address {
// Setup signal handler to update session statuses on shutdown
crate::term_socket::setup_shutdown_handler()?;
crate::term_socket::setup_shutdown_handler();
ctrlc::set_handler(move || {
println!("Ctrl-C received, updating session statuses and exiting...");

View file

@ -352,8 +352,8 @@ impl StreamWriter {
// Skip parameter and intermediate characters
while pos < buf.len() {
match buf[pos] {
0x30..=0x3F => pos += 1, // Parameter characters 0-9 : ; < = > ?
0x20..=0x2F => pos += 1, // Intermediate characters (space) ! " # $ % & ' ( ) * + , - . /
// Parameter characters 0-9 : ; < = > ? and Intermediate characters
0x20..=0x3F => pos += 1,
0x40..=0x7E => return Some(pos + 1), // Final character @ A-Z [ \ ] ^ _ ` a-z { | } ~
_ => return Some(pos), // Invalid sequence, stop here
}
@ -377,8 +377,6 @@ impl StreamWriter {
}
// Simple two-character sequences: ESC letter
0x40..=0x7E => Some(2),
// Other escape sequences - assume two characters for now
_ => Some(2),
}

View file

@ -11,8 +11,6 @@ use std::ffi::CString;
use std::io::{Read, Write};
use std::os::unix::io::{AsRawFd, RawFd};
use std::os::unix::net::UnixStream;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use uuid::Uuid;
/// Spawn a terminal session with PTY fallback
@ -157,11 +155,11 @@ fn spawn_via_pty(command: &[String], working_dir: Option<&str>) -> Result<String
let expanded_working_dir = if let Some(dir) = working_dir {
if dir == "~/" || dir == "~" {
std::env::var("HOME").unwrap_or_else(|_| "/".to_string())
} else if dir.starts_with("~/") {
} else if let Some(stripped) = dir.strip_prefix("~/") {
format!(
"{}/{}",
std::env::var("HOME").unwrap_or_else(|_| "/".to_string()),
&dir[2..]
stripped
)
} else {
dir.to_string()
@ -256,11 +254,11 @@ fn spawn_via_pty(command: &[String], working_dir: Option<&str>) -> Result<String
if let Some(dir) = working_dir {
let expanded_dir = if dir == "~/" || dir == "~" {
std::env::var("HOME").unwrap_or_else(|_| "/".to_string())
} else if dir.starts_with("~/") {
} else if let Some(stripped) = dir.strip_prefix("~/") {
format!(
"{}/{}",
std::env::var("HOME").unwrap_or_else(|_| "/".to_string()),
&dir[2..]
stripped
)
} else {
dir.to_string()
@ -406,7 +404,7 @@ fn handle_pty_session(
0 => {
eprintln!("PTY closed (EOF), updating session status");
// Update session status to exited
let session_json_path = format!("{}/session.json", session_dir);
let session_json_path = format!("{session_dir}/session.json");
if let Ok(content) = std::fs::read_to_string(&session_json_path) {
if let Ok(mut session_info) =
serde_json::from_str::<serde_json::Value>(&content)
@ -457,7 +455,6 @@ fn handle_stdin_to_pty(master_fd: RawFd, stdin_path: &str) -> Result<()> {
Ok(0) => {
// No data available, sleep briefly to avoid busy waiting
std::thread::sleep(std::time::Duration::from_millis(10));
continue;
}
Ok(n) => {
// Write to PTY master using libc::write
@ -471,7 +468,6 @@ fn handle_stdin_to_pty(master_fd: RawFd, stdin_path: &str) -> Result<()> {
Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
// No data available, sleep briefly
std::thread::sleep(std::time::Duration::from_millis(10));
continue;
}
Err(e) => {
eprintln!("Error reading from stdin FIFO: {e}");
@ -530,23 +526,16 @@ pub fn update_all_sessions_to_exited() -> Result<()> {
}
/// Setup signal handler to cleanup sessions on shutdown
pub fn setup_shutdown_handler() -> Result<()> {
let shutdown = Arc::new(AtomicBool::new(false));
let shutdown_clone = shutdown.clone();
pub fn setup_shutdown_handler() {
std::thread::spawn(move || {
let mut signals =
Signals::new(&[SIGTERM, SIGINT]).expect("Failed to create signals iterator");
Signals::new([SIGTERM, SIGINT]).expect("Failed to create signals iterator");
for sig in signals.forever() {
eprintln!("Received signal {:?}, updating session statuses...", sig);
if let Some(sig) = signals.forever().next() {
eprintln!("Received signal {sig:?}, updating session statuses...");
if let Err(e) = update_all_sessions_to_exited() {
eprintln!("Failed to update session statuses: {}", e);
eprintln!("Failed to update session statuses: {e}");
}
shutdown_clone.store(true, Ordering::Relaxed);
break;
}
});
Ok(())
}