ci: add GitHub Actions workflows with semantic-release auto-versioning
chore: fix all 702 clippy warnings across codebase - auto-fix 475 via cargo clippy --fix - fix remaining 227 manually: uninlined_format_args, redundant_closure, match_same_arms, underscore_binding, format_push_string, items_after_statements, needless_pass_by_value, clone_on_copy, case_sensitive_extension, single_match/let-else, write_with_newline, and other clippy lints
This commit is contained in:
+37
-39
@@ -30,12 +30,12 @@ fn file_path_to_uri(path: &str) -> String {
|
||||
if cfg!(windows) {
|
||||
let path_str = path_str.replace('\\', "/");
|
||||
if path_str.starts_with('/') {
|
||||
format!("file://{}", path_str)
|
||||
format!("file://{path_str}")
|
||||
} else {
|
||||
format!("file:///{}", path_str)
|
||||
format!("file:///{path_str}")
|
||||
}
|
||||
} else {
|
||||
format!("file://{}", path_str)
|
||||
format!("file://{path_str}")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ impl LspClient {
|
||||
cmd.stderr(Stdio::piped());
|
||||
|
||||
let mut child = cmd.spawn()
|
||||
.map_err(|e| anyhow::anyhow!("failed to spawn LSP server '{}': {}", command, e))?;
|
||||
.map_err(|e| anyhow::anyhow!("failed to spawn LSP server '{command}': {e}"))?;
|
||||
|
||||
let stdin = child.stdin.take()
|
||||
.ok_or_else(|| anyhow::anyhow!("failed to capture stdin for LSP server"))?;
|
||||
@@ -106,10 +106,10 @@ impl LspClient {
|
||||
}
|
||||
});
|
||||
|
||||
let result = client.call_with_timeout("initialize", init_params, Duration::from_millis(LSP_INIT_TIMEOUT_MS))?;
|
||||
let result = client.call_with_timeout("initialize", &init_params, Duration::from_millis(LSP_INIT_TIMEOUT_MS))?;
|
||||
client.server_capabilities = result.get("capabilities").cloned().unwrap_or_default();
|
||||
|
||||
client.notify("initialized", json!({}))?;
|
||||
client.notify("initialized", &json!({}))?;
|
||||
|
||||
Ok(client)
|
||||
}
|
||||
@@ -118,11 +118,11 @@ impl LspClient {
|
||||
&self.server_capabilities
|
||||
}
|
||||
|
||||
pub fn call(&mut self, method: &str, params: Value) -> anyhow::Result<Value> {
|
||||
pub fn call(&mut self, method: &str, params: &Value) -> anyhow::Result<Value> {
|
||||
self.call_with_timeout(method, params, Duration::from_millis(LSP_CALL_TIMEOUT_MS))
|
||||
}
|
||||
|
||||
fn call_with_timeout(&mut self, method: &str, params: Value, timeout: Duration) -> anyhow::Result<Value> {
|
||||
fn call_with_timeout(&mut self, method: &str, params: &Value, timeout: Duration) -> anyhow::Result<Value> {
|
||||
self.next_id += 1;
|
||||
let id = self.next_id;
|
||||
let req = json!({
|
||||
@@ -135,7 +135,7 @@ impl LspClient {
|
||||
self.read_response(id, timeout)
|
||||
}
|
||||
|
||||
pub fn notify(&mut self, method: &str, params: Value) -> anyhow::Result<()> {
|
||||
pub fn notify(&mut self, method: &str, params: &Value) -> anyhow::Result<()> {
|
||||
let req = json!({
|
||||
"jsonrpc": "2.0",
|
||||
"method": method,
|
||||
@@ -146,14 +146,14 @@ impl LspClient {
|
||||
|
||||
fn send_frame(&mut self, msg: &Value) -> anyhow::Result<()> {
|
||||
let body = serde_json::to_string(msg)
|
||||
.map_err(|e| anyhow::anyhow!("failed to serialize LSP message: {}", e))?;
|
||||
.map_err(|e| anyhow::anyhow!("failed to serialize LSP message: {e}"))?;
|
||||
let header = format!("Content-Length: {}\r\n\r\n", body.len());
|
||||
self.stdin.write_all(header.as_bytes())
|
||||
.map_err(|e| anyhow::anyhow!("failed to write LSP frame header: {}", e))?;
|
||||
.map_err(|e| anyhow::anyhow!("failed to write LSP frame header: {e}"))?;
|
||||
self.stdin.write_all(body.as_bytes())
|
||||
.map_err(|e| anyhow::anyhow!("failed to write LSP frame body: {}", e))?;
|
||||
.map_err(|e| anyhow::anyhow!("failed to write LSP frame body: {e}"))?;
|
||||
self.stdin.flush()
|
||||
.map_err(|e| anyhow::anyhow!("failed to flush LSP stdin: {}", e))?;
|
||||
.map_err(|e| anyhow::anyhow!("failed to flush LSP stdin: {e}"))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -166,9 +166,9 @@ impl LspClient {
|
||||
let frame = self.read_frame()?;
|
||||
if frame.get("id") == Some(&json!(expected_id)) {
|
||||
if let Some(err) = frame.get("error") {
|
||||
let code = err.get("code").and_then(|c| c.as_i64()).unwrap_or(0);
|
||||
let code = err.get("code").and_then(serde_json::Value::as_i64).unwrap_or(0);
|
||||
let msg = err.get("message").and_then(|m| m.as_str()).unwrap_or("unknown error");
|
||||
anyhow::bail!("LSP error {}: {}", code, msg);
|
||||
anyhow::bail!("LSP error {code}: {msg}");
|
||||
}
|
||||
return Ok(frame.get("result").cloned().unwrap_or(Value::Null));
|
||||
}
|
||||
@@ -179,7 +179,7 @@ impl LspClient {
|
||||
let deadline = Instant::now() + timeout;
|
||||
loop {
|
||||
if Instant::now() > deadline {
|
||||
anyhow::bail!("timed out waiting for LSP notification '{}'", method);
|
||||
anyhow::bail!("timed out waiting for LSP notification '{method}'");
|
||||
}
|
||||
let frame = self.read_frame()?;
|
||||
if frame.get("method") == Some(&json!(method)) {
|
||||
@@ -195,22 +195,21 @@ impl LspClient {
|
||||
match self.stdout.read_line(&mut line) {
|
||||
Ok(0) => anyhow::bail!("LSP server closed the connection"),
|
||||
Ok(_) => {}
|
||||
Err(e) => anyhow::bail!("LSP read error: {}", e),
|
||||
Err(e) => anyhow::bail!("LSP read error: {e}"),
|
||||
}
|
||||
let trimmed = line.trim();
|
||||
if trimmed.is_empty() {
|
||||
break;
|
||||
}
|
||||
if let Some(len_str) = trimmed.strip_prefix("Content-Length: ") {
|
||||
let length: usize = len_str.trim().parse::<usize>()
|
||||
.map_err(|e| anyhow::anyhow!("invalid Content-Length '{}': {}", len_str.trim(), e))?;
|
||||
// Cap Content-Length at 64 MiB to prevent OOM from a
|
||||
// malicious or misconfigured LSP server (CWE-400).
|
||||
const MAX_CONTENT_LENGTH: usize = 64 * 1024 * 1024;
|
||||
let length: usize = len_str.trim().parse::<usize>()
|
||||
.map_err(|e| anyhow::anyhow!("invalid Content-Length '{}': {}", len_str.trim(), e))?;
|
||||
if length > MAX_CONTENT_LENGTH {
|
||||
anyhow::bail!(
|
||||
"Content-Length {} exceeds maximum allowed size of {} bytes",
|
||||
length, MAX_CONTENT_LENGTH,
|
||||
"Content-Length {length} exceeds maximum allowed size of {MAX_CONTENT_LENGTH} bytes",
|
||||
);
|
||||
}
|
||||
content_length = Some(length);
|
||||
@@ -222,17 +221,17 @@ impl LspClient {
|
||||
|
||||
let mut body = vec![0u8; length];
|
||||
self.stdout.read_exact(&mut body)
|
||||
.map_err(|e| anyhow::anyhow!("failed to read LSP body ({} bytes): {}", length, e))?;
|
||||
.map_err(|e| anyhow::anyhow!("failed to read LSP body ({length} bytes): {e}"))?;
|
||||
|
||||
let json_str = String::from_utf8(body)
|
||||
.map_err(|e| anyhow::anyhow!("invalid UTF-8 in LSP response: {}", e))?;
|
||||
.map_err(|e| anyhow::anyhow!("invalid UTF-8 in LSP response: {e}"))?;
|
||||
|
||||
serde_json::from_str(&json_str)
|
||||
.map_err(|e| anyhow::anyhow!("invalid JSON in LSP response: {}", e))
|
||||
.map_err(|e| anyhow::anyhow!("invalid JSON in LSP response: {e}"))
|
||||
}
|
||||
|
||||
pub fn did_open(&mut self, uri: &str, language_id: &str, version: i32, text: &str) -> anyhow::Result<()> {
|
||||
self.notify("textDocument/didOpen", json!({
|
||||
self.notify("textDocument/didOpen", &json!({
|
||||
"textDocument": {
|
||||
"uri": uri,
|
||||
"languageId": language_id,
|
||||
@@ -244,7 +243,7 @@ impl LspClient {
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn did_change(&mut self, uri: &str, version: i32, text: &str) -> anyhow::Result<()> {
|
||||
self.notify("textDocument/didChange", json!({
|
||||
self.notify("textDocument/didChange", &json!({
|
||||
"textDocument": {
|
||||
"uri": uri,
|
||||
"version": version
|
||||
@@ -256,7 +255,7 @@ impl LspClient {
|
||||
}
|
||||
|
||||
pub fn did_close(&mut self, uri: &str) -> anyhow::Result<()> {
|
||||
self.notify("textDocument/didClose", json!({
|
||||
self.notify("textDocument/didClose", &json!({
|
||||
"textDocument": {
|
||||
"uri": uri
|
||||
}
|
||||
@@ -264,28 +263,28 @@ impl LspClient {
|
||||
}
|
||||
|
||||
pub fn hover(&mut self, uri: &str, line: u32, character: u32) -> anyhow::Result<Value> {
|
||||
self.call("textDocument/hover", json!({
|
||||
self.call("textDocument/hover", &json!({
|
||||
"textDocument": { "uri": uri },
|
||||
"position": { "line": line, "character": character }
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn completion(&mut self, uri: &str, line: u32, character: u32) -> anyhow::Result<Value> {
|
||||
self.call("textDocument/completion", json!({
|
||||
self.call("textDocument/completion", &json!({
|
||||
"textDocument": { "uri": uri },
|
||||
"position": { "line": line, "character": character }
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn goto_definition(&mut self, uri: &str, line: u32, character: u32) -> anyhow::Result<Value> {
|
||||
self.call("textDocument/definition", json!({
|
||||
self.call("textDocument/definition", &json!({
|
||||
"textDocument": { "uri": uri },
|
||||
"position": { "line": line, "character": character }
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn references(&mut self, uri: &str, line: u32, character: u32) -> anyhow::Result<Value> {
|
||||
self.call("textDocument/references", json!({
|
||||
self.call("textDocument/references", &json!({
|
||||
"textDocument": { "uri": uri },
|
||||
"position": { "line": line, "character": character },
|
||||
"context": {
|
||||
@@ -296,7 +295,7 @@ impl LspClient {
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn document_symbols(&mut self, uri: &str) -> anyhow::Result<Value> {
|
||||
self.call("textDocument/documentSymbol", json!({
|
||||
self.call("textDocument/documentSymbol", &json!({
|
||||
"textDocument": { "uri": uri }
|
||||
}))
|
||||
}
|
||||
@@ -327,7 +326,7 @@ impl LspClient {
|
||||
/// still proves the process is up and the JSON-RPC channel is live.
|
||||
/// Returns `false` on timeout, EOF, or any read/write error.
|
||||
///
|
||||
/// Flow: build request → send_frame → poll frames until id matches
|
||||
/// Flow: build request → `send_frame` → poll frames until id matches
|
||||
/// (alive) or deadline/read error fires (dead).
|
||||
#[allow(dead_code)]
|
||||
pub fn is_alive(&mut self) -> bool {
|
||||
@@ -369,19 +368,18 @@ impl LspClient {
|
||||
/// not block on any reply.
|
||||
#[allow(dead_code)]
|
||||
pub fn exit(&mut self) -> anyhow::Result<()> {
|
||||
self.notify("exit", json!({}))
|
||||
self.notify("exit", &json!({}))
|
||||
}
|
||||
|
||||
pub fn shutdown(&mut self) -> anyhow::Result<()> {
|
||||
let _ = self.call_with_timeout("shutdown", json!({}), Duration::from_secs(5));
|
||||
let _ = self.notify("exit", json!({}));
|
||||
Ok(())
|
||||
pub fn shutdown(&mut self) {
|
||||
let _ = self.call_with_timeout("shutdown", &json!({}), Duration::from_secs(5));
|
||||
let _ = self.notify("exit", &json!({}));
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for LspClient {
|
||||
fn drop(&mut self) {
|
||||
let _ = self.notify("exit", json!({}));
|
||||
let _ = self.notify("exit", &json!({}));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user