Skip to content

Commit 08e2467

Browse files
Refactor ruff_cli's run method to return on each branch (#7040)
## Summary I think the fallthrough here for some branches is a little confusing. Now each branch either runs a command that returns `Result<ExitStatus>`, or runs a command that returns `Result<()>` and then explicitly returns `Ok(ExitStatus::SUCCESS)`.
1 parent 0489bbc commit 08e2467

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed
+5-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
use crate::ExitStatus;
1+
use anyhow::{anyhow, Result};
2+
23
use ruff_workspace::options::Options;
34

45
#[allow(clippy::print_stdout)]
5-
pub(crate) fn config(key: Option<&str>) -> ExitStatus {
6+
pub(crate) fn config(key: Option<&str>) -> Result<()> {
67
match key {
78
None => print!("{}", Options::metadata()),
89
Some(key) => match Options::metadata().get(key) {
910
None => {
10-
println!("Unknown option");
11-
return ExitStatus::Error;
11+
return Err(anyhow!("Unknown option: {key}"));
1212
}
1313
Some(entry) => {
1414
print!("{entry}");
1515
}
1616
},
1717
}
18-
ExitStatus::Success
18+
Ok(())
1919
}

crates/ruff_cli/src/lib.rs

+16-7
Original file line numberDiff line numberDiff line change
@@ -139,18 +139,27 @@ quoting the executed command, along with the relevant file contents and `pyproje
139139
if let Some(rule) = rule {
140140
commands::rule::rule(rule, format)?;
141141
}
142+
Ok(ExitStatus::Success)
143+
}
144+
Command::Config { option } => {
145+
commands::config::config(option.as_deref())?;
146+
Ok(ExitStatus::Success)
147+
}
148+
Command::Linter { format } => {
149+
commands::linter::linter(format)?;
150+
Ok(ExitStatus::Success)
151+
}
152+
Command::Clean => {
153+
commands::clean::clean(log_level)?;
154+
Ok(ExitStatus::Success)
142155
}
143-
Command::Config { option } => return Ok(commands::config::config(option.as_deref())),
144-
Command::Linter { format } => commands::linter::linter(format)?,
145-
Command::Clean => commands::clean::clean(log_level)?,
146156
Command::GenerateShellCompletion { shell } => {
147157
shell.generate(&mut Args::command(), &mut stdout());
158+
Ok(ExitStatus::Success)
148159
}
149-
Command::Check(args) => return check(args, log_level),
150-
Command::Format(args) => return format(args, log_level),
160+
Command::Check(args) => check(args, log_level),
161+
Command::Format(args) => format(args, log_level),
151162
}
152-
153-
Ok(ExitStatus::Success)
154163
}
155164

156165
fn format(args: FormatCommand, log_level: LogLevel) -> Result<ExitStatus> {

0 commit comments

Comments
 (0)