Skip to content

Commit

Permalink
Janitor: Optimize usage of is_dir/is_file
Browse files Browse the repository at this point in the history
As suggested in #4219

Closes #4219
  • Loading branch information
ogoffart committed Dec 27, 2023
1 parent 2a56542 commit 5aa6b42
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 14 deletions.
6 changes: 3 additions & 3 deletions internal/compiler/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ fn widget_library() -> &'static [(&'static str, &'static BuiltinDirectory<'stati
)?;

for style in read_dir(library_dir)?.filter_map(Result::ok) {
let path = style.path();
if !path.is_dir() {
if !style.file_type().map_or(false, |f| f.is_dir()) {
continue;
}
let path = style.path();
writeln!(
file,
"(\"{}\", &[{}]),",
Expand All @@ -47,7 +47,7 @@ fn process_style(path: &Path) -> std::io::Result<String> {
let library_files: Vec<PathBuf> = read_dir(path)?
.filter_map(Result::ok)
.filter(|entry| {
entry.path().is_file()
entry.file_type().map_or(false, |f| f.is_file())
&& entry
.path()
.extension()
Expand Down
4 changes: 2 additions & 2 deletions internal/compiler/tests/syntax_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ fn syntax_tests() -> std::io::Result<()> {
let mut test_entries = Vec::new();
for entry in std::fs::read_dir(format!("{}/tests/syntax", env!("CARGO_MANIFEST_DIR")))? {
let entry = entry?;
let path = entry.path();
if path.is_dir() {
if entry.file_type().map_or(false, |f| f.is_dir()) {
let path = entry.path();
for test_entry in path.read_dir()? {
let test_entry = test_entry?;
let path = test_entry.path();
Expand Down
6 changes: 3 additions & 3 deletions tests/driver/driverlib/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ pub fn collect_test_cases(sub_folders: &str) -> std::io::Result<Vec<TestCase>> {

for entry in walkdir::WalkDir::new(case_root_dir.clone()).follow_links(true) {
let entry = entry?;
let absolute_path = entry.into_path();
if absolute_path.is_dir() {
println!("cargo:rerun-if-changed={}", absolute_path.display());
if entry.file_type().is_dir() {
println!("cargo:rerun-if-changed={}", entry.into_path().display());
continue;
}
let absolute_path = entry.into_path();
let relative_path =
std::path::PathBuf::from(absolute_path.strip_prefix(&case_root_dir).unwrap());
if let Some(filter) = &filter {
Expand Down
5 changes: 3 additions & 2 deletions xtask/src/cppdocs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ fn symlink_files_in_dir<S: AsRef<Path>, T: AsRef<Path>, TS: AsRef<Path>>(
let file_name = path.file_name().unwrap();
let symlink_source = target_to_source.as_ref().to_path_buf().join(&file_name);
let symlink_target = target.as_ref().to_path_buf().join(path.file_name().unwrap());
if path.is_file() {
let filetype = entry.file_type().context("Cannot determine file type")?;
if filetype.is_file() {
symlink_file(symlink_source, symlink_target).context("Could not symlink file")?;
} else if path.is_dir() {
} else if filetype.is_dir() {
symlink_dir(symlink_source, symlink_target).context("Could not symlink directory")?;
}
}
Expand Down
4 changes: 2 additions & 2 deletions xtask/src/reuse_compliance_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ fn find_licenses_directories(dir: &Path) -> Result<Vec<PathBuf>> {
let licenses_name: Option<&OsStr> = Some(OsStr::new("LICENSES"));
let dot_name: &OsStr = &OsStr::new(".");

for d in
std::fs::read_dir(dir)?.filter(|d| if let Ok(d) = d { d.path().is_dir() } else { false })
for d in std::fs::read_dir(dir)?
.filter(|d| d.as_ref().map_or(false, |e| e.file_type().map_or(false, |f| f.is_dir())))
{
let path = d?.path();
let parent_path = path.parent().expect("This is a subdirectory, so it must have a parent!");
Expand Down
5 changes: 3 additions & 2 deletions xtask/src/slintdocs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ fn symlink_files_in_dir<S: AsRef<Path>, T: AsRef<Path>, TS: AsRef<Path>>(
let file_name = path.file_name().unwrap();
let symlink_source = target_to_source.as_ref().to_path_buf().join(&file_name);
let symlink_target = target.as_ref().to_path_buf().join(path.file_name().unwrap());
if path.is_file() {
let filetype = entry.file_type().context("Cannot determine file type")?;
if filetype.is_file() {
symlink_file(symlink_source, symlink_target).context("Could not symlink file")?;
} else if path.is_dir() {
} else if filetype.is_dir() {
symlink_dir(symlink_source, symlink_target).context("Could not symlink directory")?;
}
}
Expand Down

0 comments on commit 5aa6b42

Please sign in to comment.