Make the use of Self consistent. (#15074)

## Summary

Make the use of `Self` consistent. Mostly done by running `cargo clippy
--fix -- -A clippy::all -W clippy::use_self`.

## Test Plan

<!-- How was it tested? -->
No need.
This commit is contained in:
adamnemecek 2025-08-05 12:17:12 -07:00 committed by GitHub
parent 57f900ad0d
commit 3f83390e34
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
121 changed files with 1305 additions and 1376 deletions

View file

@ -74,7 +74,7 @@ impl<'a> serde::Deserialize<'a> for PythonRequest {
D: serde::Deserializer<'a>,
{
let s = String::deserialize(deserializer)?;
Ok(PythonRequest::parse(&s))
Ok(Self::parse(&s))
}
}
@ -129,9 +129,9 @@ impl FromStr for PythonDownloads {
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_ascii_lowercase().as_str() {
"auto" | "automatic" | "true" | "1" => Ok(PythonDownloads::Automatic),
"manual" => Ok(PythonDownloads::Manual),
"never" | "false" | "0" => Ok(PythonDownloads::Never),
"auto" | "automatic" | "true" | "1" => Ok(Self::Automatic),
"manual" => Ok(Self::Manual),
"never" | "false" | "0" => Ok(Self::Never),
_ => Err(format!("Invalid value for `python-download`: '{s}'")),
}
}
@ -139,11 +139,7 @@ impl FromStr for PythonDownloads {
impl From<bool> for PythonDownloads {
fn from(value: bool) -> Self {
if value {
PythonDownloads::Automatic
} else {
PythonDownloads::Never
}
if value { Self::Automatic } else { Self::Never }
}
}
@ -966,7 +962,7 @@ impl Error {
match self {
// When querying the Python interpreter fails, we will only raise errors that demonstrate that something is broken
// If the Python interpreter returned a bad response, we'll continue searching for one that works
Error::Query(err, _, source) => match &**err {
Self::Query(err, _, source) => match &**err {
InterpreterError::Encode(_)
| InterpreterError::Io(_)
| InterpreterError::SpawnFailed { .. } => true,
@ -1007,7 +1003,7 @@ impl Error {
}
}
},
Error::VirtualEnv(VirtualEnvError::MissingPyVenvCfg(path)) => {
Self::VirtualEnv(VirtualEnvError::MissingPyVenvCfg(path)) => {
trace!("Skipping broken virtualenv at {}", path.display());
false
}
@ -1603,8 +1599,8 @@ fn is_windows_store_shim(_path: &Path) -> bool {
impl PythonVariant {
fn matches_interpreter(self, interpreter: &Interpreter) -> bool {
match self {
PythonVariant::Default => !interpreter.gil_disabled(),
PythonVariant::Freethreaded => interpreter.gil_disabled(),
Self::Default => !interpreter.gil_disabled(),
Self::Freethreaded => interpreter.gil_disabled(),
}
}
@ -1717,7 +1713,7 @@ impl PythonRequest {
///
/// This can only return `Err` if `@` is used. Otherwise, if no match is found, it returns
/// `Ok(None)`.
pub fn try_from_tool_name(value: &str) -> Result<Option<PythonRequest>, Error> {
pub fn try_from_tool_name(value: &str) -> Result<Option<Self>, Error> {
let lowercase_value = &value.to_ascii_lowercase();
// Omitting the empty string from these lists excludes bare versions like "39".
let abstract_version_prefixes = if cfg!(windows) {
@ -1751,7 +1747,7 @@ impl PythonRequest {
implementation_names: impl IntoIterator<Item = &'a str>,
// the string to parse
lowercase_value: &str,
) -> Result<Option<PythonRequest>, Error> {
) -> Result<Option<Self>, Error> {
for prefix in abstract_version_prefixes {
if let Some(version_request) =
Self::try_split_prefix_and_version(prefix, lowercase_value)?
@ -1826,15 +1822,15 @@ impl PythonRequest {
/// Check if this request includes a specific patch version.
pub fn includes_patch(&self) -> bool {
match self {
PythonRequest::Default => false,
PythonRequest::Any => false,
PythonRequest::Version(version_request) => version_request.patch().is_some(),
PythonRequest::Directory(..) => false,
PythonRequest::File(..) => false,
PythonRequest::ExecutableName(..) => false,
PythonRequest::Implementation(..) => false,
PythonRequest::ImplementationVersion(_, version) => version.patch().is_some(),
PythonRequest::Key(request) => request
Self::Default => false,
Self::Any => false,
Self::Version(version_request) => version_request.patch().is_some(),
Self::Directory(..) => false,
Self::File(..) => false,
Self::ExecutableName(..) => false,
Self::Implementation(..) => false,
Self::ImplementationVersion(_, version) => version.patch().is_some(),
Self::Key(request) => request
.version
.as_ref()
.is_some_and(|request| request.patch().is_some()),
@ -1849,11 +1845,9 @@ impl PythonRequest {
}
match self {
PythonRequest::Default | PythonRequest::Any => true,
PythonRequest::Version(version_request) => {
version_request.matches_interpreter(interpreter)
}
PythonRequest::Directory(directory) => {
Self::Default | Self::Any => true,
Self::Version(version_request) => version_request.matches_interpreter(interpreter),
Self::Directory(directory) => {
// `sys.prefix` points to the environment root or `sys.executable` is the same
is_same_executable(directory, interpreter.sys_prefix())
|| is_same_executable(
@ -1861,7 +1855,7 @@ impl PythonRequest {
interpreter.sys_executable(),
)
}
PythonRequest::File(file) => {
Self::File(file) => {
// The interpreter satisfies the request both if it is the venv...
if is_same_executable(interpreter.sys_executable(), file) {
return true;
@ -1893,7 +1887,7 @@ impl PythonRequest {
}
false
}
PythonRequest::ExecutableName(name) => {
Self::ExecutableName(name) => {
// First, see if we have a match in the venv ...
if interpreter
.sys_executable()
@ -1922,16 +1916,16 @@ impl PythonRequest {
}
false
}
PythonRequest::Implementation(implementation) => interpreter
Self::Implementation(implementation) => interpreter
.implementation_name()
.eq_ignore_ascii_case(implementation.into()),
PythonRequest::ImplementationVersion(implementation, version) => {
Self::ImplementationVersion(implementation, version) => {
version.matches_interpreter(interpreter)
&& interpreter
.implementation_name()
.eq_ignore_ascii_case(implementation.into())
}
PythonRequest::Key(request) => request.satisfied_by_interpreter(interpreter),
Self::Key(request) => request.satisfied_by_interpreter(interpreter),
}
}
@ -2076,12 +2070,12 @@ impl PythonPreference {
}
match self {
PythonPreference::OnlyManaged => matches!(source, PythonSource::Managed),
Self::OnlyManaged => matches!(source, PythonSource::Managed),
Self::Managed | Self::System => matches!(
source,
PythonSource::Managed | PythonSource::SearchPath | PythonSource::Registry
),
PythonPreference::OnlySystem => {
Self::OnlySystem => {
matches!(source, PythonSource::SearchPath | PythonSource::Registry)
}
}

View file

@ -121,12 +121,12 @@ impl Error {
// Unfortunately different variants of `Error` track retry counts in different ways. We
// could consider unifying the variants we handle here in `Error::from_reqwest_middleware`
// instead, but both approaches will be fragile as new variants get added over time.
if let Error::NetworkErrorWithRetries { retries, .. } = self {
if let Self::NetworkErrorWithRetries { retries, .. } = self {
return retries + 1;
}
// TODO(jack): let-chains are stable as of Rust 1.88. We should use them here as soon as
// our rust-version is high enough.
if let Error::NetworkMiddlewareError(_, anyhow_error) = self {
if let Self::NetworkMiddlewareError(_, anyhow_error) = self {
if let Some(RetryError::WithRetries { retries, .. }) =
anyhow_error.downcast_ref::<RetryError>()
{
@ -632,7 +632,7 @@ impl ManagedPythonDownload {
pub fn from_request(
request: &PythonDownloadRequest,
python_downloads_json_url: Option<&str>,
) -> Result<&'static ManagedPythonDownload, Error> {
) -> Result<&'static Self, Error> {
if let Some(download) = request.iter_downloads(python_downloads_json_url)?.next() {
return Ok(download);
}
@ -658,7 +658,7 @@ impl ManagedPythonDownload {
/// so `python_downloads_json_url` is only used in the first call to this function.
pub fn iter_all(
python_downloads_json_url: Option<&str>,
) -> Result<impl Iterator<Item = &'static ManagedPythonDownload>, Error> {
) -> Result<impl Iterator<Item = &'static Self>, Error> {
let downloads = PYTHON_DOWNLOADS.get_or_try_init(|| {
let json_downloads: HashMap<String, JsonPythonDownload> = if let Some(json_source) =
python_downloads_json_url
@ -1244,8 +1244,8 @@ pub enum Direction {
impl Direction {
fn as_str(&self) -> &str {
match self {
Direction::Download => "download",
Direction::Extract => "extract",
Self::Download => "download",
Self::Extract => "extract",
}
}
}

View file

@ -690,6 +690,6 @@ impl Hash for PythonInstallationMinorVersionKey {
impl From<PythonInstallationKey> for PythonInstallationMinorVersionKey {
fn from(key: PythonInstallationKey) -> Self {
PythonInstallationMinorVersionKey(key)
Self(key)
}
}

View file

@ -104,7 +104,7 @@ pub enum Error {
impl Error {
pub(crate) fn with_missing_python_hint(self, hint: String) -> Self {
match self {
Error::MissingPython(err, _) => Error::MissingPython(err, Some(hint)),
Self::MissingPython(err, _) => Self::MissingPython(err, Some(hint)),
_ => self,
}
}
@ -112,7 +112,7 @@ impl Error {
impl From<PythonNotFound> for Error {
fn from(err: PythonNotFound) -> Self {
Error::MissingPython(err, None)
Self::MissingPython(err, None)
}
}
@ -360,7 +360,7 @@ mod tests {
///
/// Adds them to the test context search path.
fn add_python_to_workdir(&self, name: &str, version: &str) -> Result<()> {
TestContext::create_mock_interpreter(
Self::create_mock_interpreter(
self.workdir.child(name).as_ref(),
&PythonVersion::from_str(version).expect("Test uses valid version"),
ImplementationName::default(),
@ -419,7 +419,7 @@ mod tests {
.parent()
.expect("A Python executable path should always have a parent"),
)?;
TestContext::create_mock_interpreter(
Self::create_mock_interpreter(
&executable,
&PythonVersion::from_str(version)
.expect("A valid Python version is used for tests"),
@ -441,7 +441,7 @@ mod tests {
.parent()
.expect("A Python executable path should always have a parent"),
)?;
TestContext::create_mock_interpreter(
Self::create_mock_interpreter(
&executable,
&PythonVersion::from_str(version)
.expect("A valid Python version is used for tests"),

View file

@ -263,7 +263,7 @@ impl ManagedPythonInstallations {
let arch = Arch::from_env();
let libc = Libc::from_env()?;
let iter = ManagedPythonInstallations::from_settings(None)?
let iter = Self::from_settings(None)?
.find_all()?
.filter(move |installation| {
installation.key.os == os
@ -627,7 +627,7 @@ impl ManagedPythonInstallation {
}
/// Returns `true` if self is a suitable upgrade of other.
pub fn is_upgrade_of(&self, other: &ManagedPythonInstallation) -> bool {
pub fn is_upgrade_of(&self, other: &Self) -> bool {
// Require matching implementation
if self.key.implementation != other.key.implementation {
return false;
@ -764,7 +764,7 @@ impl PythonMinorVersionLink {
installation: &ManagedPythonInstallation,
preview: Preview,
) -> Option<Self> {
PythonMinorVersionLink::from_executable(
Self::from_executable(
installation.executable(false).as_path(),
installation.key(),
preview,

View file

@ -227,7 +227,7 @@ impl PythonVersionFile {
/// Returns `true` if the version file is a global version file.
pub fn is_global(&self) -> bool {
PythonVersionFile::global().is_some_and(|global| self.path() == global.path())
Self::global().is_some_and(|global| self.path() == global.path())
}
/// Return the first request declared in the file, if any.

View file

@ -85,22 +85,22 @@ impl CondaEnvironmentKind {
fn from_prefix_path(path: &Path) -> Self {
// If we cannot read `CONDA_DEFAULT_ENV`, there's no way to know if the base environment
let Ok(default_env) = env::var(EnvVars::CONDA_DEFAULT_ENV) else {
return CondaEnvironmentKind::Child;
return Self::Child;
};
// These are the expected names for the base environment
if default_env != "base" && default_env != "root" {
return CondaEnvironmentKind::Child;
return Self::Child;
}
let Some(name) = path.file_name() else {
return CondaEnvironmentKind::Child;
return Self::Child;
};
if name.to_str().is_some_and(|name| name == default_env) {
CondaEnvironmentKind::Base
Self::Base
} else {
CondaEnvironmentKind::Child
Self::Child
}
}
}