mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 05:44:56 +00:00
[ty] Disallow std::env and io methods in most ty crates (#20046)
## Summary We use the `System` abstraction in ty to abstract away the host/system on which ty runs. This has a few benefits: * Tests can run in full isolation using a memory system (that uses an in-memory file system) * The LSP has a custom implementation where `read_to_string` returns the content as seen by the editor (e.g. unsaved changes) instead of always returning the content as it is stored on disk * We don't require any file system polyfills for wasm in the browser However, it does require extra care that we don't accidentally use `std::fs` or `std::env` (etc.) methods in ty's code base (which is very easy). This PR sets up Clippy and disallows the most common methods, instead pointing users towards the corresponding `System` methods. The setup is a bit awkward because clippy doesn't support inheriting configurations. That means, a crate can only override the entire workspace configuration or not at all. The approach taken in this PR is: * Configure the disallowed methods at the workspace level * Allow `disallowed_methods` at the workspace level * Enable the lint at the crate level using the warn attribute (in code) The obvious downside is that it won't work if we ever want to disallow other methods, but we can figure that out once we reach that point. What about false positives: Just add an `allow` and move on with your life :) This isn't something that we have to enforce strictly; the goal is to catch accidental misuse. ## Test Plan Clippy found a place where we incorrectly used `std::fs::read_to_string`
This commit is contained in:
parent
5508e8e528
commit
796819e7a0
20 changed files with 192 additions and 42 deletions
|
@ -363,6 +363,11 @@ fn is_python_extension(ext: &str) -> bool {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#![expect(
|
||||
clippy::disallowed_methods,
|
||||
reason = "These are tests, so it's fine to do I/O by-passing System."
|
||||
)]
|
||||
|
||||
use camino::{Utf8Component, Utf8Path};
|
||||
use ruff_db::Db as _;
|
||||
use ruff_db::files::{File, FilePath, FileRootKind};
|
||||
|
|
|
@ -1115,6 +1115,10 @@ impl fmt::Display for RelaxedModuleName {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#![expect(
|
||||
clippy::disallowed_methods,
|
||||
reason = "These are tests, so it's fine to do I/O by-passing System."
|
||||
)]
|
||||
use ruff_db::Db;
|
||||
use ruff_db::files::{File, FilePath, system_path_to_file};
|
||||
use ruff_db::system::{DbWithTestSystem as _, DbWithWritableSystem as _};
|
||||
|
|
|
@ -304,6 +304,11 @@ impl fmt::Display for PyVersionRange {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#![expect(
|
||||
clippy::disallowed_methods,
|
||||
reason = "These are tests, so it's fine to do I/O by-passing System."
|
||||
)]
|
||||
|
||||
use std::fmt::Write as _;
|
||||
use std::num::{IntErrorKind, NonZeroU16};
|
||||
use std::path::Path;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue