Remove result_like dependency (#11793)

## Summary

This PR removes the `result-like` dependency and instead implement the
required functionality. The motivation being that `noqa.is_enabled()` is
easier to read than `noqa.into()`.

For context, I was just trying to understand the syntax error workflow
and I saw these flags which were being converted via `into`. I always
find `into` confusing because you never know what's it being converted
into unless you know the type. Later realized that it's just a boolean
flag. After removing the usages from these two flags, it turns out that
the dependency is only being used in one rule so I thought to remove
that as well.

## Test Plan

`cargo insta test`
This commit is contained in:
Dhruv Manilawala 2024-06-07 11:53:22 +05:30 committed by GitHub
parent ea27445479
commit d22f3402e1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 60 additions and 57 deletions

33
Cargo.lock generated
View file

@ -1629,17 +1629,6 @@ version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02"
[[package]]
name = "pmutil"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "52a40bc70c2c58040d2d8b167ba9a5ff59fc9dab7ad44771cfde3dcfde7a09c6"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "portable-atomic" name = "portable-atomic"
version = "1.6.0" version = "1.6.0"
@ -1859,27 +1848,6 @@ version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56" checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56"
[[package]]
name = "result-like"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "abf7172fef6a7d056b5c26bf6c826570267562d51697f4982ff3ba4aec68a9df"
dependencies = [
"result-like-derive",
]
[[package]]
name = "result-like-derive"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8d6574c02e894d66370cfc681e5d68fedbc9a548fb55b30a96b3f0ae22d0fe5"
dependencies = [
"pmutil",
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "ring" name = "ring"
version = "0.17.8" version = "0.17.8"
@ -2086,7 +2054,6 @@ dependencies = [
"pyproject-toml", "pyproject-toml",
"quick-junit", "quick-junit",
"regex", "regex",
"result-like",
"ruff_cache", "ruff_cache",
"ruff_diagnostics", "ruff_diagnostics",
"ruff_macros", "ruff_macros",

View file

@ -99,7 +99,6 @@ quote = { version = "1.0.23" }
rand = { version = "0.8.5" } rand = { version = "0.8.5" }
rayon = { version = "1.10.0" } rayon = { version = "1.10.0" }
regex = { version = "1.10.2" } regex = { version = "1.10.2" }
result-like = { version = "0.5.0" }
rustc-hash = { version = "1.1.0" } rustc-hash = { version = "1.1.0" }
schemars = { version = "0.8.16" } schemars = { version = "0.8.16" }
seahash = { version = "4.1.0" } seahash = { version = "4.1.0" }

View file

@ -59,7 +59,7 @@ pub(crate) fn check(
); );
// Load the caches. // Load the caches.
let caches = if bool::from(cache) { let caches = if cache.is_enabled() {
Some(PackageCacheMap::init(&package_roots, &resolver)) Some(PackageCacheMap::init(&package_roots, &resolver))
} else { } else {
None None

View file

@ -186,7 +186,7 @@ pub(crate) fn lint_path(
) -> Result<Diagnostics> { ) -> Result<Diagnostics> {
// Check the cache. // Check the cache.
let caching = match cache { let caching = match cache {
Some(cache) if noqa.into() => { Some(cache) if noqa.is_enabled() => {
let relative_path = cache let relative_path = cache
.relative_path(path) .relative_path(path)
.expect("wrong package cache for file"); .expect("wrong package cache for file");

View file

@ -56,7 +56,6 @@ pep440_rs = { workspace = true, features = ["serde"] }
pyproject-toml = { workspace = true } pyproject-toml = { workspace = true }
quick-junit = { workspace = true } quick-junit = { workspace = true }
regex = { workspace = true } regex = { workspace = true }
result-like = { workspace = true }
rustc-hash = { workspace = true } rustc-hash = { workspace = true }
schemars = { workspace = true, optional = true } schemars = { workspace = true, optional = true }
serde = { workspace = true } serde = { workspace = true }

View file

@ -281,7 +281,7 @@ impl<'a> Checker<'a> {
// members from the fix that will eventually be excluded by a `noqa`. // members from the fix that will eventually be excluded by a `noqa`.
// Unfortunately, we _do_ want to register a `Diagnostic` for each // Unfortunately, we _do_ want to register a `Diagnostic` for each
// eventually-ignored import, so that our `noqa` counts are accurate. // eventually-ignored import, so that our `noqa` counts are accurate.
if !self.noqa.to_bool() { if !self.noqa.is_enabled() {
return false; return false;
} }
noqa::rule_is_ignored(code, offset, self.noqa_line_for, self.locator) noqa::rule_is_ignored(code, offset, self.noqa_line_for, self.locator)

View file

@ -299,7 +299,7 @@ pub fn check_path(
} }
// Enforce `noqa` directives. // Enforce `noqa` directives.
if (noqa.into() && !diagnostics.is_empty()) if (noqa.is_enabled() && !diagnostics.is_empty())
|| settings || settings
.rules .rules
.iter_enabled() .iter_enabled()
@ -315,7 +315,7 @@ pub fn check_path(
&per_file_ignores, &per_file_ignores,
settings, settings,
); );
if noqa.into() { if noqa.is_enabled() {
for index in ignored.iter().rev() { for index in ignored.iter().rev() {
diagnostics.swap_remove(*index); diagnostics.swap_remove(*index);
} }

View file

@ -8,12 +8,6 @@ use ruff_text_size::Ranged;
use crate::checkers::ast::Checker; use crate::checkers::ast::Checker;
#[derive(Debug, Copy, Clone, PartialEq, Eq, result_like::BoolLike)]
enum Certainty {
Certain,
Uncertain,
}
/// ## What it does /// ## What it does
/// Checks for unused variables in loops (e.g., `for` and `while` statements). /// Checks for unused variables in loops (e.g., `for` and `while` statements).
/// ///
@ -61,10 +55,13 @@ impl Violation for UnusedLoopControlVariable {
let UnusedLoopControlVariable { let UnusedLoopControlVariable {
name, certainty, .. name, certainty, ..
} = self; } = self;
if certainty.to_bool() { match certainty {
format!("Loop control variable `{name}` not used within loop body") Certainty::Certain => {
} else { format!("Loop control variable `{name}` not used within loop body")
format!("Loop control variable `{name}` may not be used within loop body") }
Certainty::Uncertain => {
format!("Loop control variable `{name}` may not be used within loop body")
}
} }
} }
@ -105,10 +102,13 @@ pub(crate) fn unused_loop_control_variable(checker: &mut Checker, stmt_for: &ast
} }
// Avoid fixing any variables that _may_ be used, but undetectably so. // Avoid fixing any variables that _may_ be used, but undetectably so.
let certainty = let certainty = if helpers::uses_magic_variable_access(&stmt_for.body, |id| {
Certainty::from(!helpers::uses_magic_variable_access(&stmt_for.body, |id| { checker.semantic().has_builtin_binding(id)
checker.semantic().has_builtin_binding(id) }) {
})); Certainty::Uncertain
} else {
Certainty::Certain
};
// Attempt to rename the variable by prepending an underscore, but avoid // Attempt to rename the variable by prepending an underscore, but avoid
// applying the fix if doing so wouldn't actually cause us to ignore the // applying the fix if doing so wouldn't actually cause us to ignore the
@ -129,7 +129,7 @@ pub(crate) fn unused_loop_control_variable(checker: &mut Checker, stmt_for: &ast
expr.range(), expr.range(),
); );
if let Some(rename) = rename { if let Some(rename) = rename {
if certainty.into() { if certainty == Certainty::Certain {
// Avoid fixing if the variable, or any future bindings to the variable, are // Avoid fixing if the variable, or any future bindings to the variable, are
// used _after_ the loop. // used _after_ the loop.
let scope = checker.semantic().current_scope(); let scope = checker.semantic().current_scope();
@ -149,3 +149,9 @@ pub(crate) fn unused_loop_control_variable(checker: &mut Checker, stmt_for: &ast
checker.diagnostics.push(diagnostic); checker.diagnostics.push(diagnostic);
} }
} }
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
enum Certainty {
Certain,
Uncertain,
}

View file

@ -5,14 +5,46 @@ pub enum FixMode {
Diff, Diff,
} }
#[derive(Debug, Copy, Clone, Hash, result_like::BoolLike)] #[derive(Debug, Copy, Clone, Hash)]
pub enum Noqa { pub enum Noqa {
Enabled, Enabled,
Disabled, Disabled,
} }
#[derive(Debug, Copy, Clone, Hash, result_like::BoolLike)] impl Noqa {
pub const fn is_enabled(self) -> bool {
matches!(self, Noqa::Enabled)
}
}
impl From<bool> for Noqa {
fn from(value: bool) -> Self {
if value {
Noqa::Enabled
} else {
Noqa::Disabled
}
}
}
#[derive(Debug, Copy, Clone, Hash)]
pub enum Cache { pub enum Cache {
Enabled, Enabled,
Disabled, Disabled,
} }
impl Cache {
pub const fn is_enabled(self) -> bool {
matches!(self, Cache::Enabled)
}
}
impl From<bool> for Cache {
fn from(value: bool) -> Self {
if value {
Cache::Enabled
} else {
Cache::Disabled
}
}
}