Switch to Rust 2024 edition (#18129)

This commit is contained in:
Micha Reiser 2025-05-16 13:25:28 +02:00 committed by GitHub
parent e67b35743a
commit 9ae698fe30
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
1082 changed files with 4211 additions and 3300 deletions

View file

@ -1,7 +1,7 @@
use std::panic::{AssertUnwindSafe, RefUnwindSafe};
use std::sync::Arc;
use crate::{DummyReporter, DEFAULT_LINT_REGISTRY};
use crate::{DEFAULT_LINT_REGISTRY, DummyReporter};
use crate::{Project, ProjectMetadata, Reporter};
use ruff_db::diagnostic::Diagnostic;
use ruff_db::files::{File, Files};
@ -201,8 +201,8 @@ impl Db for ProjectDatabase {
#[cfg(feature = "format")]
mod format {
use crate::ProjectDatabase;
use ruff_db::files::File;
use ruff_db::Upcast;
use ruff_db::files::File;
use ruff_python_formatter::{Db as FormatDb, PyFormatOptions};
#[salsa::db]
@ -235,8 +235,8 @@ pub(crate) mod tests {
use ty_python_semantic::lint::{LintRegistry, RuleSelection};
use ty_python_semantic::{Db as SemanticDb, Program};
use crate::db::Db;
use crate::DEFAULT_LINT_REGISTRY;
use crate::db::Db;
use crate::{Project, ProjectMetadata};
type Events = Arc<Mutex<Vec<salsa::Event>>>;

View file

@ -5,9 +5,9 @@ use crate::{Project, ProjectMetadata};
use std::collections::BTreeSet;
use crate::walk::ProjectFilesWalker;
use ruff_db::Db as _;
use ruff_db::files::{File, Files};
use ruff_db::system::SystemPath;
use ruff_db::Db as _;
use rustc_hash::FxHashSet;
use ty_python_semantic::Program;
@ -186,7 +186,9 @@ impl ProjectDatabase {
let program = Program::get(self);
if let Err(error) = program.update_from_settings(self, program_settings) {
tracing::error!("Failed to update the program settings, keeping the old program settings: {error}");
tracing::error!(
"Failed to update the program settings, keeping the old program settings: {error}"
);
}
if metadata.root() == project.root(self) {

View file

@ -254,10 +254,10 @@ impl Drop for IndexedMut<'_> {
mod tests {
use rustc_hash::FxHashSet;
use crate::db::tests::TestDb;
use crate::db::Db;
use crate::files::Index;
use crate::ProjectMetadata;
use crate::db::Db;
use crate::db::tests::TestDb;
use crate::files::Index;
use ruff_db::files::system_path_to_file;
use ruff_db::system::{DbWithWritableSystem as _, SystemPathBuf};
use ruff_python_ast::name::Name;

View file

@ -7,12 +7,12 @@ use files::{Index, Indexed, IndexedFiles};
use metadata::settings::Settings;
pub use metadata::{ProjectDiscoveryError, ProjectMetadata};
use ruff_db::diagnostic::{
create_parse_diagnostic, create_unsupported_syntax_diagnostic, Annotation, Diagnostic,
DiagnosticId, Severity, Span, SubDiagnostic,
Annotation, Diagnostic, DiagnosticId, Severity, Span, SubDiagnostic, create_parse_diagnostic,
create_unsupported_syntax_diagnostic,
};
use ruff_db::files::File;
use ruff_db::parsed::parsed_module;
use ruff_db::source::{source_text, SourceTextError};
use ruff_db::source::{SourceTextError, source_text};
use ruff_db::system::{SystemPath, SystemPathBuf};
use rustc_hash::FxHashSet;
use salsa::Durability;
@ -409,7 +409,7 @@ impl Project {
pub fn files(self, db: &dyn Db) -> Indexed<'_> {
let files = self.file_set(db);
let indexed = match files.get() {
match files.get() {
Index::Lazy(vacant) => {
let _entered =
tracing::debug_span!("Project::index_files", project = %self.name(db))
@ -422,9 +422,7 @@ impl Project {
vacant.set(files, diagnostics)
}
Index::Indexed(indexed) => indexed,
};
indexed
}
}
pub fn reload_files(self, db: &mut dyn Db) {
@ -660,13 +658,13 @@ where
#[cfg(test)]
mod tests {
use crate::db::tests::TestDb;
use crate::{check_file_impl, ProjectMetadata};
use crate::{ProjectMetadata, check_file_impl};
use ruff_db::files::system_path_to_file;
use ruff_db::source::source_text;
use ruff_db::system::{DbWithTestSystem, DbWithWritableSystem as _, SystemPath, SystemPathBuf};
use ruff_db::testing::assert_function_query_was_not_run;
use ruff_python_ast::name::Name;
use ruff_python_ast::PythonVersion;
use ruff_python_ast::name::Name;
use ty_python_semantic::types::check_types;
use ty_python_semantic::{Program, ProgramSettings, PythonPlatform, SearchPathSettings};

View file

@ -128,7 +128,7 @@ impl ProjectMetadata {
return Err(ProjectDiscoveryError::InvalidPyProject {
path: pyproject_path,
source: Box::new(error),
})
});
}
}
} else {
@ -147,7 +147,7 @@ impl ProjectMetadata {
return Err(ProjectDiscoveryError::InvalidTyToml {
path: ty_toml_path,
source: Box::new(error),
})
});
}
};
@ -156,7 +156,9 @@ impl ProjectMetadata {
.is_some_and(|project| project.ty().is_some())
{
// TODO: Consider using a diagnostic here
tracing::warn!("Ignoring the `tool.ty` section in `{pyproject_path}` because `{ty_toml_path}` takes precedence.");
tracing::warn!(
"Ignoring the `tool.ty` section in `{pyproject_path}` because `{ty_toml_path}` takes precedence."
);
}
tracing::debug!("Found project at '{}'", project_root);
@ -211,7 +213,9 @@ impl ProjectMetadata {
closest_project
} else {
tracing::debug!("The ancestor directories contain no `pyproject.toml`. Falling back to a virtual project.");
tracing::debug!(
"The ancestor directories contain no `pyproject.toml`. Falling back to a virtual project."
);
// Create a project with a default configuration
Self::new(
@ -304,7 +308,7 @@ pub enum ProjectDiscoveryError {
mod tests {
//! Integration tests for project discovery
use anyhow::{anyhow, Context};
use anyhow::{Context, anyhow};
use insta::assert_ron_snapshot;
use ruff_db::system::{SystemPathBuf, TestSystem};
use ruff_python_ast::PythonVersion;
@ -405,7 +409,9 @@ mod tests {
.context("Failed to write files")?;
let Err(error) = ProjectMetadata::discover(&root, &system) else {
return Err(anyhow!("Expected project discovery to fail because of invalid syntax in the pyproject.toml"));
return Err(anyhow!(
"Expected project discovery to fail because of invalid syntax in the pyproject.toml"
));
};
assert_error_eq(
@ -868,10 +874,15 @@ expected `.`, `]`
.context("Failed to write file")?;
let Err(error) = ProjectMetadata::discover(&root, &system) else {
return Err(anyhow!("Expected project discovery to fail because the `requires-python` doesn't specify a lower bound (it only specifies an upper bound)."));
return Err(anyhow!(
"Expected project discovery to fail because the `requires-python` doesn't specify a lower bound (it only specifies an upper bound)."
));
};
assert_error_eq(&error, "Invalid `requires-python` version specifier (`/app/pyproject.toml`): value `<3.12` does not contain a lower bound. Add a lower bound to indicate the minimum compatible Python version (e.g., `>=3.13`) or specify a version in `environment.python-version`.");
assert_error_eq(
&error,
"Invalid `requires-python` version specifier (`/app/pyproject.toml`): value `<3.12` does not contain a lower bound. Add a lower bound to indicate the minimum compatible Python version (e.g., `>=3.13`) or specify a version in `environment.python-version`.",
);
Ok(())
}
@ -893,10 +904,15 @@ expected `.`, `]`
.context("Failed to write file")?;
let Err(error) = ProjectMetadata::discover(&root, &system) else {
return Err(anyhow!("Expected project discovery to fail because the `requires-python` specifiers are empty and don't define a lower bound."));
return Err(anyhow!(
"Expected project discovery to fail because the `requires-python` specifiers are empty and don't define a lower bound."
));
};
assert_error_eq(&error, "Invalid `requires-python` version specifier (`/app/pyproject.toml`): value `` does not contain a lower bound. Add a lower bound to indicate the minimum compatible Python version (e.g., `>=3.13`) or specify a version in `environment.python-version`.");
assert_error_eq(
&error,
"Invalid `requires-python` version specifier (`/app/pyproject.toml`): value `` does not contain a lower bound. Add a lower bound to indicate the minimum compatible Python version (e.g., `>=3.13`) or specify a version in `environment.python-version`.",
);
Ok(())
}
@ -918,10 +934,15 @@ expected `.`, `]`
.context("Failed to write file")?;
let Err(error) = ProjectMetadata::discover(&root, &system) else {
return Err(anyhow!("Expected project discovery to fail because of the requires-python major version that is larger than 255."));
return Err(anyhow!(
"Expected project discovery to fail because of the requires-python major version that is larger than 255."
));
};
assert_error_eq(&error, "Invalid `requires-python` version specifier (`/app/pyproject.toml`): The major version `999` is larger than the maximum supported value 255");
assert_error_eq(
&error,
"Invalid `requires-python` version specifier (`/app/pyproject.toml`): The major version `999` is larger than the maximum supported value 255",
);
Ok(())
}

View file

@ -1,5 +1,5 @@
use crate::metadata::value::{RangedValue, RelativePathBuf, ValueSource, ValueSourceGuard};
use crate::Db;
use crate::metadata::value::{RangedValue, RelativePathBuf, ValueSource, ValueSourceGuard};
use ruff_db::diagnostic::{Annotation, Diagnostic, DiagnosticFormat, DiagnosticId, Severity, Span};
use ruff_db::files::system_path_to_file;
use ruff_db::system::{System, SystemPath};
@ -417,11 +417,11 @@ pub struct TerminalOptions {
#[cfg(feature = "schemars")]
mod schema {
use crate::DEFAULT_LINT_REGISTRY;
use schemars::gen::SchemaGenerator;
use schemars::JsonSchema;
use schemars::r#gen::SchemaGenerator;
use schemars::schema::{
InstanceType, Metadata, ObjectValidation, Schema, SchemaObject, SubschemaValidation,
};
use schemars::JsonSchema;
use ty_python_semantic::lint::Level;
pub(super) struct Rules;
@ -431,10 +431,10 @@ mod schema {
"Rules".to_string()
}
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
fn json_schema(generator: &mut SchemaGenerator) -> Schema {
let registry = &*DEFAULT_LINT_REGISTRY;
let level_schema = gen.subschema_for::<Level>();
let level_schema = generator.subschema_for::<Level>();
let properties: schemars::Map<String, Schema> = registry
.lints()

View file

@ -1,6 +1,6 @@
use crate::metadata::options::Options;
use crate::metadata::value::{RangedValue, ValueSource, ValueSourceGuard};
use pep440_rs::{release_specifiers_to_ranges, Version, VersionSpecifiers};
use pep440_rs::{Version, VersionSpecifiers, release_specifiers_to_ranges};
use ruff_python_ast::PythonVersion;
use serde::{Deserialize, Deserializer, Serialize};
use std::collections::Bound;
@ -85,7 +85,7 @@ impl Project {
Bound::Unbounded => {
return Err(ResolveRequiresPythonError::NoLowerBound(
requires_python.to_string(),
))
));
}
};
@ -119,7 +119,9 @@ pub enum ResolveRequiresPythonError {
TooLargeMajor(u64),
#[error("The minor version `{0}` is larger than the maximum supported value 255")]
TooLargeMinor(u64),
#[error("value `{0}` does not contain a lower bound. Add a lower bound to indicate the minimum compatible Python version (e.g., `>=3.13`) or specify a version in `environment.python-version`.")]
#[error(
"value `{0}` does not contain a lower bound. Add a lower bound to indicate the minimum compatible Python version (e.g., `>=3.13`) or specify a version in `environment.python-version`."
)]
NoLowerBound(String),
}
@ -233,7 +235,8 @@ pub(crate) enum InvalidPackageNameError {
NonAlphanumericStart(char),
#[error("name must end with letter or number but it ends with '{0}'")]
NonAlphanumericEnd(char),
#[error("valid name consists only of ASCII letters and numbers, period, underscore and hyphen but name contains '{0}'"
#[error(
"valid name consists only of ASCII letters and numbers, period, underscore and hyphen but name contains '{0}'"
)]
InvalidCharacter(char),
#[error("name must not be empty")]

View file

@ -1,5 +1,5 @@
use crate::combine::Combine;
use crate::Db;
use crate::combine::Combine;
use ruff_db::system::{System, SystemPath, SystemPathBuf};
use ruff_macros::Combine;
use ruff_text_size::{TextRange, TextSize};

View file

@ -1,5 +1,5 @@
use crate::{Db, IOErrorDiagnostic, IOErrorKind, Project};
use ruff_db::files::{system_path_to_file, File};
use ruff_db::files::{File, system_path_to_file};
use ruff_db::system::walk_directory::{ErrorKind, WalkDirectoryBuilder, WalkState};
use ruff_db::system::{FileType, SystemPath, SystemPathBuf};
use ruff_python_ast::PySourceType;

View file

@ -1,6 +1,6 @@
pub use project_watcher::ProjectWatcher;
use ruff_db::system::{SystemPath, SystemPathBuf, SystemVirtualPathBuf};
pub use watcher::{directory_watcher, EventHandler, Watcher};
pub use watcher::{EventHandler, Watcher, directory_watcher};
mod project_watcher;
mod watcher;

View file

@ -4,8 +4,8 @@ use std::hash::Hasher;
use tracing::info;
use ruff_cache::{CacheKey, CacheKeyHasher};
use ruff_db::system::{SystemPath, SystemPathBuf};
use ruff_db::Upcast;
use ruff_db::system::{SystemPath, SystemPathBuf};
use ty_python_semantic::system_module_search_paths;
use crate::db::{Db, ProjectDatabase};
@ -105,7 +105,9 @@ impl ProjectWatcher {
// Ruff otherwise stills works as expected.
if let Err(error) = self.watcher.watch(path) {
// TODO: Log a user-facing warning.
tracing::warn!("Failed to setup watcher for path `{path}`: {error}. You have to restart Ruff after making changes to files under this path or you might see stale results.");
tracing::warn!(
"Failed to setup watcher for path `{path}`: {error}. You have to restart Ruff after making changes to files under this path or you might see stale results."
);
self.has_errored_paths = true;
} else {
self.watched_paths.push(path.to_path_buf());

View file

@ -1,5 +1,5 @@
use notify::event::{CreateKind, MetadataKind, ModifyKind, RemoveKind, RenameMode};
use notify::{recommended_watcher, EventKind, RecommendedWatcher, RecursiveMode, Watcher as _};
use notify::{EventKind, RecommendedWatcher, RecursiveMode, Watcher as _, recommended_watcher};
use ruff_db::system::{SystemPath, SystemPathBuf};