Skip to content

Commit 6281ee1

Browse files
committed
fix: resolve clippy warnings in cortex-app-server
- Replace manual string prefix stripping with strip_prefix() in git.rs - Use struct initialization syntax instead of field reassignment in session_manager.rs and streaming.rs - Change &PathBuf to &Path in function signatures (filesystem.rs, search.rs, security.rs, shell.rs)
1 parent f2d62a5 commit 6281ee1

7 files changed

Lines changed: 61 additions & 63 deletions

File tree

src/cortex-app-server/src/api/git.rs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ pub async fn git_status(Query(query): Query<GitPathQuery>) -> AppResult<Json<Git
3030
let mut conflicts = vec![];
3131

3232
for line in stdout.lines() {
33-
if line.starts_with("# branch.head ") {
34-
branch = Some(line[14..].to_string());
35-
} else if line.starts_with("# branch.ab ") {
33+
if let Some(stripped) = line.strip_prefix("# branch.head ") {
34+
branch = Some(stripped.to_string());
35+
} else if let Some(stripped) = line.strip_prefix("# branch.ab ") {
3636
// Parse ahead/behind: # branch.ab +1 -2
37-
let parts: Vec<&str> = line[12..].split_whitespace().collect();
37+
let parts: Vec<&str> = stripped.split_whitespace().collect();
3838
if parts.len() >= 2 {
3939
ahead = parts[0].trim_start_matches('+').parse().ok();
4040
behind = parts[1].trim_start_matches('-').parse().ok();
@@ -66,9 +66,9 @@ pub async fn git_status(Query(query): Query<GitPathQuery>) -> AppResult<Json<Git
6666
});
6767
}
6868
}
69-
} else if line.starts_with("? ") {
69+
} else if let Some(stripped) = line.strip_prefix("? ") {
7070
// Untracked: ? path
71-
let path = line[2..].to_string();
71+
let path = stripped.to_string();
7272
unstaged.push(GitStatusFile {
7373
path,
7474
status: "untracked".to_string(),
@@ -255,10 +255,10 @@ fn parse_tracking_info(track: &str) -> (Option<u32>, Option<u32>) {
255255

256256
for part in track.split(',') {
257257
let part = part.trim();
258-
if part.starts_with("ahead ") {
259-
ahead = part[6..].trim().parse().ok();
260-
} else if part.starts_with("behind ") {
261-
behind = part[7..].trim().parse().ok();
258+
if let Some(stripped) = part.strip_prefix("ahead ") {
259+
ahead = stripped.trim().parse().ok();
260+
} else if let Some(stripped) = part.strip_prefix("behind ") {
261+
behind = stripped.trim().parse().ok();
262262
}
263263
}
264264

@@ -333,14 +333,14 @@ fn parse_diff_with_stats(diff: &str) -> (Vec<serde_json::Value>, usize, usize) {
333333
}
334334
}
335335
} else if current_hunk.is_some() {
336-
let (line_type, content) = if line.starts_with('+') {
336+
let (line_type, content) = if let Some(stripped) = line.strip_prefix('+') {
337337
total_additions += 1;
338-
("addition", &line[1..])
339-
} else if line.starts_with('-') {
338+
("addition", stripped)
339+
} else if let Some(stripped) = line.strip_prefix('-') {
340340
total_deletions += 1;
341-
("deletion", &line[1..])
342-
} else if line.starts_with(' ') {
343-
("context", &line[1..])
341+
("deletion", stripped)
342+
} else if let Some(stripped) = line.strip_prefix(' ') {
343+
("context", stripped)
344344
} else {
345345
("context", line)
346346
};
@@ -425,30 +425,30 @@ fn parse_blame(blame: &str) -> Vec<serde_json::Value> {
425425
"message": "",
426426
}));
427427
} else if let Some(ref mut commit) = current_commit {
428-
if line.starts_with("author ") {
429-
commit["author"] = serde_json::json!(line[7..].to_string());
430-
} else if line.starts_with("author-mail ") {
428+
if let Some(stripped) = line.strip_prefix("author ") {
429+
commit["author"] = serde_json::json!(stripped.to_string());
430+
} else if let Some(stripped) = line.strip_prefix("author-mail ") {
431431
commit["email"] = serde_json::json!(
432-
line[12..]
432+
stripped
433433
.trim_matches(|c| c == '<' || c == '>')
434434
.to_string()
435435
);
436-
} else if line.starts_with("author-time ") {
437-
if let Ok(timestamp) = line[12..].parse::<i64>() {
436+
} else if let Some(stripped) = line.strip_prefix("author-time ") {
437+
if let Ok(timestamp) = stripped.parse::<i64>() {
438438
commit["date"] = serde_json::json!(
439439
chrono::DateTime::from_timestamp(timestamp, 0)
440440
.map(|dt| dt.to_rfc3339())
441441
.unwrap_or_default()
442442
);
443443
}
444-
} else if line.starts_with("summary ") {
445-
commit["message"] = serde_json::json!(line[8..].to_string());
446-
} else if line.starts_with('\t') {
444+
} else if let Some(stripped) = line.strip_prefix("summary ") {
445+
commit["message"] = serde_json::json!(stripped.to_string());
446+
} else if let Some(stripped) = line.strip_prefix('\t') {
447447
// This is the actual code line
448448
if let Some(commit) = current_commit.take() {
449449
result.push(serde_json::json!({
450450
"lineNumber": line_number,
451-
"content": &line[1..],
451+
"content": stripped,
452452
"commit": commit,
453453
}));
454454
}

src/cortex-app-server/src/session_manager.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,14 @@ impl SessionManager {
9191
let session_id = Uuid::new_v4().to_string();
9292

9393
// Build cortex-core Config
94-
let mut config = CoreConfig::default();
95-
96-
// In cloud/server mode, never ask for approval - auto-approve everything
97-
config.approval_policy = AskForApproval::Never;
98-
99-
if let Some(model) = &options.model {
100-
config.model = model.clone();
101-
}
102-
if let Some(cwd) = &options.cwd {
103-
config.cwd = cwd.clone();
104-
}
105-
if let Some(provider) = &options.provider {
106-
config.model_provider_id = provider.clone();
107-
}
94+
let config = CoreConfig {
95+
// In cloud/server mode, never ask for approval - auto-approve everything
96+
approval_policy: AskForApproval::Never,
97+
model: options.model.clone().unwrap_or_default(),
98+
cwd: options.cwd.clone().unwrap_or_default(),
99+
model_provider_id: options.provider.clone().unwrap_or_default(),
100+
..Default::default()
101+
};
108102

109103
// Create the real CLI session
110104
let (mut session, handle) =
@@ -520,10 +514,12 @@ impl SessionManager {
520514
let new_session_id = Uuid::new_v4().to_string();
521515

522516
// Build cortex-core Config from original
523-
let mut config = CoreConfig::default();
524-
config.model = model;
525-
config.cwd = cwd;
526-
config.approval_policy = AskForApproval::Never;
517+
let config = CoreConfig {
518+
model,
519+
cwd,
520+
approval_policy: AskForApproval::Never,
521+
..Default::default()
522+
};
527523

528524
// Create the real CLI session using fork
529525
let (mut session, handle) = Session::fork(config.clone(), conversation_id, message_index)

src/cortex-app-server/src/streaming.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,11 @@ async fn fork_cli_session(
327327
let new_session_id = Uuid::new_v4().to_string();
328328

329329
// Build cortex-core Config from original
330-
let mut config = CoreConfig::default();
331-
config.model = model;
332-
config.cwd = cwd;
330+
let config = CoreConfig {
331+
model,
332+
cwd,
333+
..Default::default()
334+
};
333335

334336
// Create the real CLI session using fork
335337
let (mut session, handle) =

src/cortex-app-server/src/tools/filesystem.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
//! Filesystem operations (read, write, edit, list).
22
3-
use std::path::PathBuf;
3+
use std::path::{Path, PathBuf};
44

55
use serde_json::{Value, json};
66

77
use super::types::ToolResult;
88

99
/// Read file contents.
10-
pub async fn read_file(cwd: &PathBuf, args: Value) -> ToolResult {
10+
pub async fn read_file(cwd: &Path, args: Value) -> ToolResult {
1111
let path = match args
1212
.get("file_path")
1313
.or_else(|| args.get("path"))
@@ -75,7 +75,7 @@ pub async fn read_file(cwd: &PathBuf, args: Value) -> ToolResult {
7575
}
7676

7777
/// Write content to a file.
78-
pub async fn write_file(cwd: &PathBuf, args: Value) -> ToolResult {
78+
pub async fn write_file(cwd: &Path, args: Value) -> ToolResult {
7979
let path = match args
8080
.get("file_path")
8181
.or_else(|| args.get("path"))
@@ -127,7 +127,7 @@ pub async fn write_file(cwd: &PathBuf, args: Value) -> ToolResult {
127127
}
128128

129129
/// Edit a file by finding and replacing text.
130-
pub async fn edit_file(cwd: &PathBuf, args: Value) -> ToolResult {
130+
pub async fn edit_file(cwd: &Path, args: Value) -> ToolResult {
131131
let path = match args.get("file_path").and_then(|v| v.as_str()) {
132132
Some(p) => p,
133133
None => return ToolResult::error("file_path is required"),
@@ -201,7 +201,7 @@ pub async fn edit_file(cwd: &PathBuf, args: Value) -> ToolResult {
201201
}
202202

203203
/// List directory contents.
204-
pub async fn list_dir(cwd: &PathBuf, args: Value) -> ToolResult {
204+
pub async fn list_dir(cwd: &Path, args: Value) -> ToolResult {
205205
let path = args
206206
.get("directory_path")
207207
.or_else(|| args.get("path"))
@@ -261,7 +261,7 @@ pub async fn list_dir(cwd: &PathBuf, args: Value) -> ToolResult {
261261
}
262262

263263
/// Apply multiple edits to files in a single operation.
264-
pub async fn multi_edit(cwd: &PathBuf, args: Value) -> ToolResult {
264+
pub async fn multi_edit(cwd: &Path, args: Value) -> ToolResult {
265265
let edits = match args.get("edits").and_then(|v| v.as_array()) {
266266
Some(arr) => arr,
267267
None => return ToolResult::error("edits array is required"),
@@ -319,7 +319,7 @@ pub async fn multi_edit(cwd: &PathBuf, args: Value) -> ToolResult {
319319
}
320320

321321
/// Apply a unified diff patch.
322-
pub async fn apply_patch(cwd: &PathBuf, args: Value) -> ToolResult {
322+
pub async fn apply_patch(cwd: &Path, args: Value) -> ToolResult {
323323
use tokio::process::Command;
324324

325325
let patch = match args.get("patch").and_then(|v| v.as_str()) {

src/cortex-app-server/src/tools/search.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
//! Search operations (grep, glob).
22
3-
use std::path::PathBuf;
3+
use std::path::{Path, PathBuf};
44

55
use serde_json::Value;
66
use tokio::process::Command;
77

88
use super::types::ToolResult;
99

1010
/// Search file contents for a pattern.
11-
pub async fn grep(cwd: &PathBuf, args: Value) -> ToolResult {
11+
pub async fn grep(cwd: &Path, args: Value) -> ToolResult {
1212
let pattern = match args.get("pattern").and_then(|v| v.as_str()) {
1313
Some(p) => p,
1414
None => return ToolResult::error("pattern is required"),
@@ -82,7 +82,7 @@ pub async fn grep(cwd: &PathBuf, args: Value) -> ToolResult {
8282
}
8383

8484
/// Find files matching glob patterns.
85-
pub async fn glob(cwd: &PathBuf, args: Value) -> ToolResult {
85+
pub async fn glob(cwd: &Path, args: Value) -> ToolResult {
8686
let patterns = match args.get("patterns").and_then(|v| v.as_array()) {
8787
Some(arr) => arr.iter().filter_map(|v| v.as_str()).collect::<Vec<_>>(),
8888
None => return ToolResult::error("patterns is required (array of strings)"),

src/cortex-app-server/src/tools/security.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Security helper functions for command execution.
22
3-
use std::path::PathBuf;
3+
use std::path::{Path, PathBuf};
44

55
/// Parse a shell command string into program and arguments safely.
66
/// Does NOT use sh -c to prevent command injection via shell metacharacters.
@@ -173,15 +173,15 @@ pub fn is_dangerous_command(program: &str) -> bool {
173173
}
174174

175175
/// Validate and resolve working directory.
176-
pub fn validate_working_directory(requested: &PathBuf, base: &PathBuf) -> Result<PathBuf, String> {
176+
pub fn validate_working_directory(requested: &Path, base: &Path) -> Result<PathBuf, String> {
177177
// Canonicalize paths to resolve symlinks and ..
178178
let base_canonical = base
179179
.canonicalize()
180180
.map_err(|e| format!("Cannot resolve base path: {}", e))?;
181181

182182
// If the requested path is relative, join it with base
183183
let full_path = if requested.is_absolute() {
184-
requested.clone()
184+
requested.to_path_buf()
185185
} else {
186186
base.join(requested)
187187
};

src/cortex-app-server/src/tools/shell.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Shell command execution.
22
3-
use std::path::PathBuf;
3+
use std::path::{Path, PathBuf};
44
use std::process::Stdio;
55

66
use serde_json::{Value, json};
@@ -10,7 +10,7 @@ use super::security::{is_dangerous_command, parse_shell_command, validate_workin
1010
use super::types::ToolResult;
1111

1212
/// Execute a shell command with proper sandboxing.
13-
pub async fn execute_shell(cwd: &PathBuf, timeout_secs: u64, args: Value) -> ToolResult {
13+
pub async fn execute_shell(cwd: &Path, timeout_secs: u64, args: Value) -> ToolResult {
1414
let command = match args.get("command") {
1515
Some(Value::String(cmd)) => vec![cmd.clone()],
1616
Some(Value::Array(arr)) => arr
@@ -29,7 +29,7 @@ pub async fn execute_shell(cwd: &PathBuf, timeout_secs: u64, args: Value) -> Too
2929
.or_else(|| args.get("cwd"))
3030
.and_then(|v| v.as_str())
3131
.map(PathBuf::from)
32-
.unwrap_or_else(|| cwd.clone());
32+
.unwrap_or_else(|| cwd.to_path_buf());
3333

3434
let timeout = args
3535
.get("timeout")

0 commit comments

Comments
 (0)