1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 19:17:43 +00:00

Prevent potential unwrap on a None value

This commit is contained in:
electricboogie 2022-01-27 22:07:07 -06:00
parent 5e82d6069f
commit 72c53219e3

View file

@ -1328,17 +1328,19 @@ impl PathData {
Dereference::None => false, Dereference::None => false,
}; };
let de = match dir_entry { let de: Option<DirEntry> = match dir_entry {
Some(de) => de.ok(), Some(de) => de.ok(),
None => None, None => None,
}; };
// Why prefer to check the DirEntry file_type()? B/c the call is // Why prefer to check the DirEntry file_type()? B/c the call is
// nearly free compared to a metadata() or file_type() call on a dir/file. // nearly free compared to a metadata() call on a Path
let ft = match de { let ft = match de {
Some(ref de) => { Some(ref de) => {
if let Ok(ft_de) = de.file_type() { if let Ok(ft_de) = de.file_type() {
OnceCell::from(Some(ft_de)) OnceCell::from(Some(ft_de))
} else if let Ok(md_pb) = p_buf.metadata() {
OnceCell::from(Some(md_pb.file_type()))
} else { } else {
OnceCell::new() OnceCell::new()
} }
@ -1353,8 +1355,8 @@ impl PathData {
}; };
Self { Self {
ft,
md: OnceCell::new(), md: OnceCell::new(),
ft,
de, de,
display_name, display_name,
p_buf, p_buf,
@ -1594,8 +1596,7 @@ fn enter_directory(
for e in entries for e in entries
.iter() .iter()
.skip(if config.files == Files::All { 2 } else { 0 }) .skip(if config.files == Files::All { 2 } else { 0 })
// Already requested file_type for the dir_entries above. So we know the OnceCell is set. .filter(|p| p.ft.get().is_some())
// And can unwrap again because we tested whether path has is_some here
.filter(|p| p.ft.get().unwrap().is_some()) .filter(|p| p.ft.get().unwrap().is_some())
.filter(|p| p.ft.get().unwrap().unwrap().is_dir()) .filter(|p| p.ft.get().unwrap().unwrap().is_dir())
{ {