Skip to content

Commit 50f15d7

Browse files
committed
Use Termination trait in main
1 parent a26a789 commit 50f15d7

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

src/main.rs

+4-11
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ mod util;
6363
mod version;
6464
mod view;
6565

66-
use std::{env, ffi::OsString};
66+
use std::{env, ffi::OsString, process::Termination};
6767

6868
use crate::{
6969
arguments::{Args, Mode},
@@ -85,19 +85,12 @@ fn run(os_args: Vec<OsString>) -> Exit {
8585
}
8686
}
8787

88-
// TODO use the termination trait once rust-lang/rust#43301 is stable
89-
// allow_attributes added due to a problem on clippy::exit
90-
#[allow(
91-
clippy::allow_attributes,
92-
clippy::exit,
93-
clippy::print_stderr,
94-
reason = "While not allowed in other locations, these are needed here"
95-
)]
88+
#[expect(clippy::print_stderr, reason = "Required to print error message.")]
9689
#[cfg(not(tarpaulin_include))]
97-
fn main() {
90+
fn main() -> impl Termination {
9891
let exit = run(env::args_os().skip(1).collect());
9992
if let Some(message) = exit.get_message() {
10093
eprintln!("{message}");
10194
}
102-
std::process::exit(exit.get_status().to_code());
95+
*exit.get_status()
10396
}

src/module/exit_status.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::process::{ExitCode, Termination};
2+
13
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
24
pub(crate) enum ExitStatus {
35
None,
@@ -11,7 +13,7 @@ pub(crate) enum ExitStatus {
1113
}
1214

1315
impl ExitStatus {
14-
pub(crate) const fn to_code(self) -> i32 {
16+
pub(crate) const fn to_code(self) -> u8 {
1517
match self {
1618
Self::Abort => 5,
1719
Self::ConfigError => 1,
@@ -24,6 +26,12 @@ impl ExitStatus {
2426
}
2527
}
2628

29+
impl Termination for ExitStatus {
30+
fn report(self) -> ExitCode {
31+
ExitCode::from(self.to_code())
32+
}
33+
}
34+
2735
#[cfg(test)]
2836
mod tests {
2937
use rstest::rstest;
@@ -39,7 +47,12 @@ mod tests {
3947
#[case::good(ExitStatus::Good, 0)]
4048
#[case::state_error(ExitStatus::StateError, 4)]
4149
#[case::kill(ExitStatus::Kill, 6)]
42-
fn to_code(#[case] input: ExitStatus, #[case] expected: i32) {
50+
fn to_code(#[case] input: ExitStatus, #[case] expected: u8) {
4351
assert_eq!(ExitStatus::to_code(input), expected);
4452
}
53+
54+
#[test]
55+
fn termination() {
56+
assert_eq!(ExitStatus::ConfigError.report(), ExitCode::from(1));
57+
}
4558
}

0 commit comments

Comments
 (0)