mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-08 01:20:29 +00:00

## 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`
78 lines
2.5 KiB
Rust
78 lines
2.5 KiB
Rust
#![warn(
|
|
clippy::disallowed_methods,
|
|
reason = "Prefer System trait methods over std methods in ty crates"
|
|
)]
|
|
use std::hash::BuildHasherDefault;
|
|
|
|
use rustc_hash::FxHasher;
|
|
|
|
use crate::lint::{LintRegistry, LintRegistryBuilder};
|
|
use crate::suppression::{INVALID_IGNORE_COMMENT, UNKNOWN_RULE, UNUSED_IGNORE_COMMENT};
|
|
pub use db::Db;
|
|
pub use module_name::ModuleName;
|
|
pub use module_resolver::{
|
|
Module, SearchPath, SearchPathValidationError, SearchPaths, list_modules, resolve_module,
|
|
resolve_real_module, system_module_search_paths,
|
|
};
|
|
pub use program::{
|
|
Program, ProgramSettings, PythonVersionFileSource, PythonVersionSource,
|
|
PythonVersionWithSource, SearchPathSettings,
|
|
};
|
|
pub use python_platform::PythonPlatform;
|
|
pub use semantic_model::{
|
|
Completion, CompletionKind, HasDefinition, HasType, NameKind, SemanticModel,
|
|
};
|
|
pub use site_packages::{PythonEnvironment, SitePackagesPaths, SysPrefixPathOrigin};
|
|
pub use types::DisplaySettings;
|
|
pub use types::ide_support::{
|
|
ImportAliasResolution, ResolvedDefinition, definitions_for_attribute,
|
|
definitions_for_imported_symbol, definitions_for_name, map_stub_definition,
|
|
};
|
|
pub use util::diagnostics::add_inferred_python_version_hint_to_diagnostic;
|
|
|
|
pub mod ast_node_ref;
|
|
mod db;
|
|
mod dunder_all;
|
|
pub mod lint;
|
|
pub(crate) mod list;
|
|
mod module_name;
|
|
mod module_resolver;
|
|
mod node_key;
|
|
pub(crate) mod place;
|
|
mod program;
|
|
mod python_platform;
|
|
mod rank;
|
|
pub mod semantic_index;
|
|
mod semantic_model;
|
|
pub(crate) mod site_packages;
|
|
mod suppression;
|
|
pub mod types;
|
|
mod unpack;
|
|
mod util;
|
|
|
|
#[cfg(feature = "testing")]
|
|
pub mod pull_types;
|
|
|
|
type FxOrderMap<K, V> = ordermap::map::OrderMap<K, V, BuildHasherDefault<FxHasher>>;
|
|
type FxOrderSet<V> = ordermap::set::OrderSet<V, BuildHasherDefault<FxHasher>>;
|
|
type FxIndexMap<K, V> = indexmap::IndexMap<K, V, BuildHasherDefault<FxHasher>>;
|
|
type FxIndexSet<V> = indexmap::IndexSet<V, BuildHasherDefault<FxHasher>>;
|
|
|
|
/// Returns the default registry with all known semantic lints.
|
|
pub fn default_lint_registry() -> &'static LintRegistry {
|
|
static REGISTRY: std::sync::LazyLock<LintRegistry> = std::sync::LazyLock::new(|| {
|
|
let mut registry = LintRegistryBuilder::default();
|
|
register_lints(&mut registry);
|
|
registry.build()
|
|
});
|
|
|
|
®ISTRY
|
|
}
|
|
|
|
/// Register all known semantic lints.
|
|
pub fn register_lints(registry: &mut LintRegistryBuilder) {
|
|
types::register_lints(registry);
|
|
registry.register_lint(&UNUSED_IGNORE_COMMENT);
|
|
registry.register_lint(&UNKNOWN_RULE);
|
|
registry.register_lint(&INVALID_IGNORE_COMMENT);
|
|
}
|