Add fs_err to disallowed_method in clippy.toml (#1950)

## Summary

Resolve #1916

---------

Co-authored-by: konsti <konstin@mailbox.org>
This commit is contained in:
Taniguchi Yasufumi 2024-02-26 23:15:07 +09:00 committed by GitHub
parent a5a917169b
commit 70e877d11c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 49 additions and 11 deletions

1
Cargo.lock generated
View file

@ -4461,6 +4461,7 @@ dependencies = [
"base64 0.21.7",
"cache-key",
"cargo-util",
"fs-err",
"git2",
"glob",
"hex",

View file

@ -2,3 +2,31 @@ doc-valid-idents = [
"PyPI",
".." # Include the defaults
]
disallowed-types = [
"std::fs::DirEntry",
"std::fs::File",
"std::fs::OpenOptions",
"std::fs::ReadDir",
]
disallowed-methods = [
"std::fs::canonicalize",
"std::fs::copy",
"std::fs::create_dir",
"std::fs::create_dir_all",
"std::fs::hard_link",
"std::fs::metadata",
"std::fs::read",
"std::fs::read_dir",
"std::fs::read_link",
"std::fs::read_to_string",
"std::fs::remove_dir",
"std::fs::remove_dir_all",
"std::fs::remove_file",
"std::fs::rename",
"std::fs::set_permissions",
"std::fs::soft_link",
"std::fs::symlink_metadata",
"std::fs::write",
]

View file

@ -17,7 +17,7 @@ pub(crate) struct RenderBenchmarksArgs {
}
pub(crate) fn render_benchmarks(args: &RenderBenchmarksArgs) -> Result<()> {
let mut results: BenchmarkResults = serde_json::from_slice(&std::fs::read(&args.path)?)?;
let mut results: BenchmarkResults = serde_json::from_slice(&fs_err::read(&args.path)?)?;
// Replace the command with a shorter name. (The command typically includes the benchmark name,
// but we assume we're running over a single benchmark here.)
@ -85,7 +85,7 @@ fn render_to_png(data: &str, path: &Path, fontdb: &fontdb::Database) -> Result<(
pixmap.as_mut(),
)
.ok_or_else(|| anyhow!("failed to render"))?;
std::fs::create_dir_all(path.parent().unwrap())?;
fs_err::create_dir_all(path.parent().unwrap())?;
pixmap.save_png(path)?;
Ok(())
}

View file

@ -980,7 +980,7 @@ impl<'a, T: BuildContext> SourceDistCachedBuilder<'a, T> {
/// Read an existing HTTP-cached [`Manifest`], if it exists.
pub(crate) fn read_http_manifest(cache_entry: &CacheEntry) -> Result<Option<Manifest>, Error> {
match std::fs::File::open(cache_entry.path()) {
match fs_err::File::open(cache_entry.path()) {
Ok(file) => {
let data = DataWithCachePolicy::from_reader(file)?.data;
Ok(Some(rmp_serde::from_slice::<Manifest>(&data)?))
@ -998,7 +998,7 @@ pub(crate) fn read_timestamp_manifest(
modified: ArchiveTimestamp,
) -> Result<Option<Manifest>, Error> {
// If the cache entry is up-to-date, return it.
match std::fs::read(cache_entry.path()) {
match fs_err::read(cache_entry.path()) {
Ok(cached) => {
let cached = rmp_serde::from_slice::<CachedByTimestamp<Manifest>>(&cached)?;
if cached.timestamp == modified.timestamp() {

View file

@ -117,6 +117,7 @@ impl<R: HasLength> HasLength for BufReader<R> {
}
}
#[allow(clippy::disallowed_types)]
impl HasLength for std::fs::File {
fn len(&self) -> u64 {
self.metadata().unwrap().len()

View file

@ -32,6 +32,7 @@ sha1 = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }
url = { workspace = true }
fs-err = { workspace = true }
[features]
vendored-libgit2 = ["git2/vendored-libgit2"]

View file

@ -388,7 +388,7 @@ impl<'a> GitCheckout<'a> {
//
// TODO(git2): remove this when git2 supports shallow clone correctly
if database.repo.is_shallow() {
std::fs::copy(
fs_err::copy(
database.repo.path().join("shallow"),
r.path().join("shallow"),
)?;

View file

@ -97,3 +97,6 @@ pypi = []
git = []
# Introduces a dependency on Maturin.
maturin = []
[build-dependencies]
fs-err = { workspace = true }

View file

@ -1,4 +1,5 @@
use std::{fs, path::Path, process::Command};
use fs_err as fs;
use std::{path::Path, process::Command};
fn main() {
// The workspace root directory is not available without walking up the tree

View file

@ -482,11 +482,13 @@ fn cmd(include_index_url: bool, include_find_links: bool) -> String {
}
/// A multi-casting writer that writes to both the standard output and an output file, if present.
#[allow(clippy::disallowed_types)]
struct OutputWriter {
stdout: Option<AutoStream<std::io::Stdout>>,
output_file: Option<AutoStream<std::fs::File>>,
}
#[allow(clippy::disallowed_types)]
impl OutputWriter {
/// Create a new output writer.
fn new(include_stdout: bool, output_file: Option<&Path>) -> Result<Self> {

View file

@ -1,4 +1,5 @@
#![cfg(all(feature = "python", feature = "pypi"))]
#![cfg_attr(feature = "cargo-clippy", allow(clippy::disallowed_types))]
use std::fs;
use std::path::PathBuf;

View file

@ -1,7 +1,7 @@
#![cfg(all(feature = "python", feature = "pypi"))]
use fs_err as fs;
use std::env::consts::EXE_SUFFIX;
use std::fs;
use std::path::Path;
use std::process::Command;

View file

@ -328,7 +328,7 @@ fn missing_record() -> Result<()> {
unimplemented!("Only Windows and Unix are supported")
})
.unwrap();
std::fs::remove_file(dist_info.join("RECORD"))?;
fs_err::remove_file(dist_info.join("RECORD"))?;
let dist_info_str = regex::escape(&format!(
"RECORD file not found at: {}",

View file

@ -550,8 +550,8 @@ fn windows_shims() -> Result<()> {
assert!(py38.to_str().unwrap().contains("3.8"));
// Write the shim script that forwards the arguments to the python3.8 installation.
std::fs::create_dir(&shim_path)?;
std::fs::write(
fs_err::create_dir(&shim_path)?;
fs_err::write(
shim_path.child("python.bat"),
format!("@echo off\r\n{}/python.exe %*", py38.display()),
)?;