Skip to content

Commit a0df78a

Browse files
committed
refactor(rm): improve preserve-root parsing safety
- Replace `.unwrap()` with `.map()` to handle options safely without risking panics. - Update the abbreviation check to compare against the `PreserveRoot` enum directly.
1 parent 062315b commit a0df78a

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

src/uu/rm/src/rm.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
258258
} else {
259259
match matches
260260
.get_one::<String>(OPT_PRESERVE_ROOT)
261-
.unwrap()
262-
.as_str()
261+
.map(|s| s.as_str())
263262
{
264-
"all" => PreserveRoot::YesAll,
263+
Some("all") => PreserveRoot::YesAll,
265264
_ => PreserveRoot::Default,
266265
}
267266
},
@@ -278,7 +277,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
278277

279278
// manually parse all args to verify --no-preserve-root did not get abbreviated (clap does
280279
// allow this)
281-
if !options.preserve_root && !args.iter().any(|arg| arg == "--no-preserve-root") {
280+
if options.preserve_root == PreserveRoot::No
281+
&& !args.iter().any(|arg| arg == "--no-preserve-root")
282+
{
282283
return Err(RmError::MayNotAbbreviateNoPreserveRoot.into());
283284
}
284285

0 commit comments

Comments
 (0)