From b2f15de58cbbf195201d543523144a82baa74594 Mon Sep 17 00:00:00 2001 From: Riz Ashraf Date: Tue, 22 Sep 2026 06:07:50 +0100 Subject: [PATCH] style: fix clippy collapsible_if warnings in nvim-core --- linux-nvim/tests/integration_test.rs | 2 +- nvim-core/src/lib.rs | 166 +++++++++++++++------------ server/src/api/rest.rs | 6 +- server/src/db.rs | 9 +- server/src/error.rs | 2 +- server/src/handlers/notes.rs | 3 +- server/src/main.rs | 4 +- win-nvim/tests/integration_test.rs | 2 +- 8 files changed, 105 insertions(+), 89 deletions(-) diff --git a/linux-nvim/tests/integration_test.rs b/linux-nvim/tests/integration_test.rs index 9efec15..1672aca 100644 --- a/linux-nvim/tests/integration_test.rs +++ b/linux-nvim/tests/integration_test.rs @@ -1,6 +1,6 @@ #![cfg(unix)] -use serde_json::{json, Value}; +use serde_json::{Value, json}; use std::io::{BufRead, BufReader, Read, Write}; use std::process::{Command, Stdio}; diff --git a/nvim-core/src/lib.rs b/nvim-core/src/lib.rs index b8c7cad..d186503 100644 --- a/nvim-core/src/lib.rs +++ b/nvim-core/src/lib.rs @@ -1,5 +1,5 @@ use serde::{Deserialize, Serialize}; -use serde_json::{json, Value}; +use serde_json::{Value, json}; use tokio::io::{AsyncReadExt, AsyncWriteExt}; #[derive(Serialize, Deserialize, Debug, Clone)] @@ -133,10 +133,10 @@ static NVIM_CONN: LazyLock async fn get_nvim_connection() -> Result, String> { { let conn_lock = NVIM_CONN.lock().unwrap_or_else(|e| e.into_inner()); - if let Some(sender) = conn_lock.as_ref() { - if !sender.is_closed() { - return Ok(sender.clone()); - } + if let Some(sender) = conn_lock.as_ref() + && !sender.is_closed() + { + return Ok(sender.clone()); } } @@ -200,20 +200,21 @@ async fn get_nvim_connection() -> Result, String> { Ok(val) => { offset += cursor.position() as usize; - if let rmpv::Value::Array(ref arr) = val { - if arr.len() >= 4 && arr[0] == rmpv::Value::Integer(1.into()) { - let msgid = match &arr[1] { - rmpv::Value::Integer(i) => i.as_u64().unwrap_or(0), - _ => 0, - }; + if let rmpv::Value::Array(ref arr) = val + && arr.len() >= 4 + && arr[0] == rmpv::Value::Integer(1.into()) + { + let msgid = match &arr[1] { + rmpv::Value::Integer(i) => i.as_u64().unwrap_or(0), + _ => 0, + }; - if let Some(reply_sender) = pending_clone2 - .lock() - .unwrap_or_else(|e| e.into_inner()) - .remove(&msgid) - { - let _ = reply_sender.send(Ok(val)); - } + if let Some(reply_sender) = pending_clone2 + .lock() + .unwrap_or_else(|e| e.into_inner()) + .remove(&msgid) + { + let _ = reply_sender.send(Ok(val)); } } if offset == resp_buf.len() { @@ -225,11 +226,17 @@ async fn get_nvim_connection() -> Result, String> { } continue; } - Err(e) if match &e { - rmpv::decode::Error::InvalidMarkerRead(io_err) => io_err.kind() == std::io::ErrorKind::UnexpectedEof, - rmpv::decode::Error::InvalidDataRead(io_err) => io_err.kind() == std::io::ErrorKind::UnexpectedEof, - _ => false, - } => { + Err(e) + if match &e { + rmpv::decode::Error::InvalidMarkerRead(io_err) => { + io_err.kind() == std::io::ErrorKind::UnexpectedEof + } + rmpv::decode::Error::InvalidDataRead(io_err) => { + io_err.kind() == std::io::ErrorKind::UnexpectedEof + } + _ => false, + } => + { resp_buf.drain(..offset); offset = 0; @@ -277,11 +284,11 @@ async fn get_nvim_connection() -> Result, String> { }); let mut conn_lock = NVIM_CONN.lock().unwrap_or_else(|e| e.into_inner()); - if let Some(existing_sender) = conn_lock.as_ref() { - if !existing_sender.is_closed() { - // Another task established the connection while we were waiting - return Ok(existing_sender.clone()); - } + if let Some(existing_sender) = conn_lock.as_ref() + && !existing_sender.is_closed() + { + // Another task established the connection while we were waiting + return Ok(existing_sender.clone()); } *conn_lock = Some(tx.clone()); Ok(tx) @@ -348,21 +355,25 @@ async fn send_nvim_command(cmd: &str) -> Result<(), String> { } async fn get_nvim_active_buffer() -> Result { - let result = call_nvim_method("nvim_buf_get_lines", vec![ - rmpv::Value::Integer(0.into()), - rmpv::Value::Integer(0.into()), - rmpv::Value::Integer((-1).into()), - rmpv::Value::Boolean(true), - ]).await?; - + let result = call_nvim_method( + "nvim_buf_get_lines", + vec![ + rmpv::Value::Integer(0.into()), + rmpv::Value::Integer(0.into()), + rmpv::Value::Integer((-1).into()), + rmpv::Value::Boolean(true), + ], + ) + .await?; + if let rmpv::Value::Array(lines) = result { let mut text = String::new(); for line in lines { - if let rmpv::Value::String(s) = line { - if let Some(s) = s.as_str() { - text.push_str(s); - text.push('\n'); - } + if let rmpv::Value::String(s) = line + && let Some(s) = s.as_str() + { + text.push_str(s); + text.push('\n'); } } return Ok(text); @@ -371,14 +382,14 @@ async fn get_nvim_active_buffer() -> Result { } async fn get_nvim_cursor() -> Result { - let result = call_nvim_method("nvim_win_get_cursor", vec![rmpv::Value::Integer(0.into())]).await?; - - if let rmpv::Value::Array(pos) = result { - if pos.len() == 2 { - if let (rmpv::Value::Integer(row), rmpv::Value::Integer(col)) = (&pos[0], &pos[1]) { - return Ok(format!("Line: {row}, Column: {col}")); - } - } + let result = + call_nvim_method("nvim_win_get_cursor", vec![rmpv::Value::Integer(0.into())]).await?; + + if let rmpv::Value::Array(pos) = result + && pos.len() == 2 + && let (rmpv::Value::Integer(row), rmpv::Value::Integer(col)) = (&pos[0], &pos[1]) + { + return Ok(format!("Line: {row}, Column: {col}")); } Err("Invalid response format".to_string()) } @@ -395,15 +406,19 @@ async fn get_nvim_visual_selection() -> Result { end "#; - let result = call_nvim_method("nvim_exec_lua", vec![ - rmpv::Value::String(lua_script.into()), - rmpv::Value::Array(vec![]), - ]).await?; - - if let rmpv::Value::String(s) = result { - if let Some(text) = s.as_str() { - return Ok(text.to_string()); - } + let result = call_nvim_method( + "nvim_exec_lua", + vec![ + rmpv::Value::String(lua_script.into()), + rmpv::Value::Array(vec![]), + ], + ) + .await?; + + if let rmpv::Value::String(s) = result + && let Some(text) = s.as_str() + { + return Ok(text.to_string()); } Err("Invalid response format".to_string()) } @@ -423,11 +438,15 @@ async fn set_nvim_diagnostics(line: i64, message: &str) -> Result<(), String> { "# ); - call_nvim_method("nvim_exec_lua", vec![ - rmpv::Value::String(lua_script.into()), - rmpv::Value::Array(vec![]), - ]).await?; - + call_nvim_method( + "nvim_exec_lua", + vec![ + rmpv::Value::String(lua_script.into()), + rmpv::Value::Array(vec![]), + ], + ) + .await?; + Ok(()) } @@ -489,22 +508,21 @@ fn json_to_rmpv(val: &serde_json::Value) -> rmpv::Value { } } serde_json::Value::String(s) => rmpv::Value::String(s.as_str().into()), - serde_json::Value::Array(arr) => { - rmpv::Value::Array(arr.iter().map(json_to_rmpv).collect()) - } - serde_json::Value::Object(obj) => { - rmpv::Value::Map(obj.iter().map(|(k, v)| { - (rmpv::Value::String(k.as_str().into()), json_to_rmpv(v)) - }).collect()) - } + serde_json::Value::Array(arr) => rmpv::Value::Array(arr.iter().map(json_to_rmpv).collect()), + serde_json::Value::Object(obj) => rmpv::Value::Map( + obj.iter() + .map(|(k, v)| (rmpv::Value::String(k.as_str().into()), json_to_rmpv(v))) + .collect(), + ), } } async fn execute_nvim_lua_with_args(code: &str, args: Vec) -> Result { - let result = call_nvim_method("nvim_exec_lua", vec![ - rmpv::Value::String(code.into()), - rmpv::Value::Array(args), - ]).await?; + let result = call_nvim_method( + "nvim_exec_lua", + vec![rmpv::Value::String(code.into()), rmpv::Value::Array(args)], + ) + .await?; let json_res = rmpv_to_json(&result); // If it's a simple string, return it unquoted, otherwise format it pretty if let serde_json::Value::String(s) = json_res { diff --git a/server/src/api/rest.rs b/server/src/api/rest.rs index a5f5500..66bae06 100644 --- a/server/src/api/rest.rs +++ b/server/src/api/rest.rs @@ -68,7 +68,9 @@ pub async fn gate_verify_handler( Err(AppError::Forbidden(msg)) } } - None => Err(AppError::NotFound("Action not yet authorized (no gate record found).".to_string())), + None => Err(AppError::NotFound( + "Action not yet authorized (no gate record found).".to_string(), + )), } } @@ -98,7 +100,7 @@ pub async fn gate_set_handler( gates.retain(|g| !(g.action == record.action && g.target == record.target)); gates.push(record); }); - + Ok((axum::http::StatusCode::OK, "Gate state updated.")) } diff --git a/server/src/db.rs b/server/src/db.rs index fed58f6..2a5320f 100644 --- a/server/src/db.rs +++ b/server/src/db.rs @@ -1,7 +1,7 @@ +use crate::store::STORE_TABLE; +use redb::{Database, ReadableTable}; use std::path::Path; use std::sync::Arc; -use redb::{Database, ReadableTable}; -use crate::store::STORE_TABLE; pub fn init_redb(base: &Path) -> Arc { let redb_path = base.join("mcp_store.redb"); @@ -50,10 +50,7 @@ pub fn init_redb(base: &Path) -> Arc { table .insert(*key, data.as_slice()) .expect("Failed to insert migrated data"); - let _ = std::fs::rename( - &json_path, - json_path.with_extension("json.migrated"), - ); + let _ = std::fs::rename(&json_path, json_path.with_extension("json.migrated")); } } } diff --git a/server/src/error.rs b/server/src/error.rs index 5a98846..47ff5a6 100644 --- a/server/src/error.rs +++ b/server/src/error.rs @@ -1,7 +1,7 @@ use axum::{ + Json, http::StatusCode, response::{IntoResponse, Response}, - Json, }; use serde_json::json; use thiserror::Error; diff --git a/server/src/handlers/notes.rs b/server/src/handlers/notes.rs index d59dbfd..7f3c351 100644 --- a/server/src/handlers/notes.rs +++ b/server/src/handlers/notes.rs @@ -244,8 +244,7 @@ impl McpTool for GenerateStandupReportHandler { async fn execute(&self, args: Value, state: Arc) -> Result { let req: GenerateStandupReportTool = serde_json::from_value(args).map_err(|e| e.to_string())?; - let cutoff = - crate::handlers::utils::now_secs().saturating_sub(req.hours_lookback * 3600); + let cutoff = crate::handlers::utils::now_secs().saturating_sub(req.hours_lookback * 3600); let report_str = state.tasks.read_with(|items| { state.ledger.read_with(|changes| { diff --git a/server/src/main.rs b/server/src/main.rs index 0717353..5bf7ca2 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -4,13 +4,13 @@ )] mod api; +pub mod db; +pub mod error; mod handlers; mod mcp; mod models; mod router; mod search; -pub mod db; -pub mod error; mod state; mod store; mod tools; diff --git a/win-nvim/tests/integration_test.rs b/win-nvim/tests/integration_test.rs index 80818fa..a1deedf 100644 --- a/win-nvim/tests/integration_test.rs +++ b/win-nvim/tests/integration_test.rs @@ -1,4 +1,4 @@ -use serde_json::{json, Value}; +use serde_json::{Value, json}; use std::io::{BufRead, BufReader, Write}; use std::process::{Command, Stdio};