Skip to content

Commit 4367ec4

Browse files
committed
Use Path methods instead of fs::metadata.
1 parent f9047dc commit 4367ec4

File tree

13 files changed

+29
-40
lines changed

13 files changed

+29
-40
lines changed

crates/cargo-test-support/src/registry.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ pub struct Dependency {
162162
pub fn init() {
163163
let config = paths::home().join(".cargo/config");
164164
t!(fs::create_dir_all(config.parent().unwrap()));
165-
if fs::metadata(&config).is_ok() {
165+
if config.exists() {
166166
return;
167167
}
168168
t!(t!(File::create(&config)).write_all(

src/bin/cargo/main.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,7 @@ fn is_executable<P: AsRef<Path>>(path: P) -> bool {
165165
}
166166
#[cfg(windows)]
167167
fn is_executable<P: AsRef<Path>>(path: P) -> bool {
168-
fs::metadata(path)
169-
.map(|metadata| metadata.is_file())
170-
.unwrap_or(false)
168+
path.as_ref().is_file()
171169
}
172170

173171
fn search_directories(config: &Config) -> Vec<PathBuf> {

src/cargo/ops/cargo_new.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,7 @@ fn detect_source_paths_and_types(
273273
let pp = i.proposed_path;
274274

275275
// path/pp does not exist or is not a file
276-
if !fs::metadata(&path.join(&pp))
277-
.map(|x| x.is_file())
278-
.unwrap_or(false)
279-
{
276+
if !path.join(&pp).is_file() {
280277
continue;
281278
}
282279

@@ -358,7 +355,7 @@ fn plan_new_source_file(bin: bool, package_name: String) -> SourceFileInformatio
358355

359356
pub fn new(opts: &NewOptions, config: &Config) -> CargoResult<()> {
360357
let path = &opts.path;
361-
if fs::metadata(path).is_ok() {
358+
if path.exists() {
362359
anyhow::bail!(
363360
"destination `{}` already exists\n\n\
364361
Use `cargo init` to initialize the directory",
@@ -397,7 +394,7 @@ pub fn init(opts: &NewOptions, config: &Config) -> CargoResult<()> {
397394

398395
let path = &opts.path;
399396

400-
if fs::metadata(&path.join("Cargo.toml")).is_ok() {
397+
if path.join("Cargo.toml").exists() {
401398
anyhow::bail!("`cargo init` cannot be run on existing Cargo packages")
402399
}
403400

@@ -428,22 +425,22 @@ pub fn init(opts: &NewOptions, config: &Config) -> CargoResult<()> {
428425
if version_control == None {
429426
let mut num_detected_vsces = 0;
430427

431-
if fs::metadata(&path.join(".git")).is_ok() {
428+
if path.join(".git").exists() {
432429
version_control = Some(VersionControl::Git);
433430
num_detected_vsces += 1;
434431
}
435432

436-
if fs::metadata(&path.join(".hg")).is_ok() {
433+
if path.join(".hg").exists() {
437434
version_control = Some(VersionControl::Hg);
438435
num_detected_vsces += 1;
439436
}
440437

441-
if fs::metadata(&path.join(".pijul")).is_ok() {
438+
if path.join(".pijul").exists() {
442439
version_control = Some(VersionControl::Pijul);
443440
num_detected_vsces += 1;
444441
}
445442

446-
if fs::metadata(&path.join(".fossil")).is_ok() {
443+
if path.join(".fossil").exists() {
447444
version_control = Some(VersionControl::Fossil);
448445
num_detected_vsces += 1;
449446
}
@@ -743,10 +740,7 @@ mod tests {
743740
"
744741
};
745742

746-
if !fs::metadata(&path_of_source_file)
747-
.map(|x| x.is_file())
748-
.unwrap_or(false)
749-
{
743+
if !path_of_source_file.is_file() {
750744
paths::write(&path_of_source_file, default_file_content)?;
751745

752746
// Format the newly created source file

src/cargo/ops/cargo_read_manifest.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub fn read_packages(
6060
}
6161

6262
// Don't automatically discover packages across git submodules
63-
if fs::metadata(&dir.join(".git")).is_ok() {
63+
if dir.join(".git").exists() {
6464
return Ok(false);
6565
}
6666
}

src/cargo/ops/registry.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::collections::{BTreeMap, HashSet};
2-
use std::fs::{self, File};
2+
use std::fs::File;
33
use std::io::{self, BufRead};
44
use std::iter::repeat;
55
use std::str;
@@ -233,7 +233,7 @@ fn transmit(
233233
None => None,
234234
};
235235
if let Some(ref file) = *license_file {
236-
if fs::metadata(&pkg.root().join(file)).is_err() {
236+
if !pkg.root().join(file).exists() {
237237
bail!("the license file `{}` does not exist", file)
238238
}
239239
}

src/cargo/sources/path.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,14 +373,14 @@ impl<'cfg> PathSource<'cfg> {
373373
is_root: bool,
374374
filter: &mut dyn FnMut(&Path) -> CargoResult<bool>,
375375
) -> CargoResult<()> {
376-
if !fs::metadata(&path).map(|m| m.is_dir()).unwrap_or(false) {
376+
if !path.is_dir() {
377377
if (*filter)(path)? {
378378
ret.push(path.to_path_buf());
379379
}
380380
return Ok(());
381381
}
382382
// Don't recurse into any sub-packages that we have.
383-
if !is_root && fs::metadata(&path.join("Cargo.toml")).is_ok() {
383+
if !is_root && path.join("Cargo.toml").exists() {
384384
return Ok(());
385385
}
386386

src/cargo/util/command_prelude.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use crate::CargoResult;
1313
use anyhow::bail;
1414
use clap::{self, SubCommand};
1515
use std::ffi::{OsStr, OsString};
16-
use std::fs;
1716
use std::path::PathBuf;
1817

1918
pub use crate::core::compiler::CompileMode;
@@ -285,7 +284,7 @@ pub trait ArgMatchesExt {
285284
if !path.ends_with("Cargo.toml") {
286285
anyhow::bail!("the manifest-path must be a path to a Cargo.toml file")
287286
}
288-
if fs::metadata(&path).is_err() {
287+
if !path.exists() {
289288
anyhow::bail!(
290289
"manifest path `{}` does not exist",
291290
self._value_of("manifest-path").unwrap()

src/cargo/util/config/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -949,8 +949,8 @@ impl Config {
949949
let possible = dir.join(filename_without_extension);
950950
let possible_with_extension = dir.join(format!("{}.toml", filename_without_extension));
951951

952-
if fs::metadata(&possible).is_ok() {
953-
if warn && fs::metadata(&possible_with_extension).is_ok() {
952+
if possible.exists() {
953+
if warn && possible_with_extension.exists() {
954954
// We don't want to print a warning if the version
955955
// without the extension is just a symlink to the version
956956
// WITH an extension, which people may want to do to
@@ -973,7 +973,7 @@ impl Config {
973973
}
974974

975975
Ok(Some(possible))
976-
} else if fs::metadata(&possible_with_extension).is_ok() {
976+
} else if possible_with_extension.exists() {
977977
Ok(Some(possible_with_extension))
978978
} else {
979979
Ok(None)

src/cargo/util/important_paths.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
use crate::util::errors::CargoResult;
22
use crate::util::paths;
3-
use std::fs;
43
use std::path::{Path, PathBuf};
54

65
/// Finds the root `Cargo.toml`.
76
pub fn find_root_manifest_for_wd(cwd: &Path) -> CargoResult<PathBuf> {
87
let file = "Cargo.toml";
98
for current in paths::ancestors(cwd) {
109
let manifest = current.join(file);
11-
if fs::metadata(&manifest).is_ok() {
10+
if manifest.exists() {
1211
return Ok(manifest);
1312
}
1413
}

src/cargo/util/toml/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
22
use std::fmt;
3-
use std::fs;
43
use std::path::{Path, PathBuf};
54
use std::rc::Rc;
65
use std::str;
@@ -1441,11 +1440,12 @@ impl TomlManifest {
14411440
Some(StringOrBool::Bool(true)) => Some(build_rs),
14421441
Some(StringOrBool::String(ref s)) => Some(PathBuf::from(s)),
14431442
None => {
1444-
match fs::metadata(&build_rs) {
1445-
// If there is a `build.rs` file next to the `Cargo.toml`, assume it is
1446-
// a build script.
1447-
Ok(ref e) if e.is_file() => Some(build_rs),
1448-
Ok(_) | Err(_) => None,
1443+
// If there is a `build.rs` file next to the `Cargo.toml`, assume it is
1444+
// a build script.
1445+
if build_rs.is_file() {
1446+
Some(build_rs)
1447+
} else {
1448+
None
14491449
}
14501450
}
14511451
}

src/cargo/util/toml/targets.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ fn clean_targets_with_legacy_path(
557557

558558
fn inferred_lib(package_root: &Path) -> Option<PathBuf> {
559559
let lib = package_root.join("src").join("lib.rs");
560-
if fs::metadata(&lib).is_ok() {
560+
if lib.exists() {
561561
Some(lib)
562562
} else {
563563
None

tests/testsuite/build_script.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ fn custom_build_env_vars() {
8585
use std::env;
8686
use std::io::prelude::*;
8787
use std::path::Path;
88-
use std::fs;
8988
9089
fn main() {{
9190
let _target = env::var("TARGET").unwrap();
@@ -103,7 +102,7 @@ fn custom_build_env_vars() {
103102
104103
let out = env::var("OUT_DIR").unwrap();
105104
assert!(out.starts_with(r"{0}"));
106-
assert!(fs::metadata(&out).map(|m| m.is_dir()).unwrap_or(false));
105+
assert!(Path::new(&out).is_dir());
107106
108107
let _host = env::var("HOST").unwrap();
109108

tests/testsuite/clean.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ fn build_script() {
185185
if env::var("FIRST").is_ok() {
186186
std::fs::File::create(out.join("out")).unwrap();
187187
} else {
188-
assert!(!std::fs::metadata(out.join("out")).is_ok());
188+
assert!(!out.join("out").exists());
189189
}
190190
}
191191
"#,

0 commit comments

Comments
 (0)