Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Summary Based on discussion in #10850. As it stands today `RUF100` will attempt to replace code redirects with their target codes even though this is not the "goal" of `RUF100`. This behavior is confusing and inconsistent, since code redirects which don't otherwise violate `RUF100` will not be updated. The behavior is also undocumented. Additionally, users who want to use `RUF100` but do not want to update redirects have no way to opt out. This PR explicitly detects redirects with a new rule `RUF101` and patches `RUF100` to keep original codes in fixes and reporting. ## Test Plan Added fixture.
- Loading branch information
1 parent
632965d
commit 5994414
Showing
27 changed files
with
411 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
x = 2 # noqa: RUF940 | ||
x = 2 # noqa: RUF950 | ||
x = 2 # noqa: RUF940, RUF950 | ||
x = 2 # noqa: RUF950, RUF940, RUF950, RUF950, RUF950 | ||
x = 2 # noqa: RUF940, RUF950, RUF940 | ||
x = 2 # noqa: RUF940, RUF950, RUF940, RUF950 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
crates/ruff_linter/src/rules/ruff/rules/redirected_noqa.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_text_size::Ranged; | ||
|
||
use crate::noqa::{Directive, NoqaDirectives}; | ||
use crate::rule_redirects::get_redirect_target; | ||
|
||
/// ## What it does | ||
/// Checks for `noqa` directives that use redirected rule codes. | ||
/// | ||
/// ## Why is this bad? | ||
/// When a rule code has been redirected, the implication is that the rule has | ||
/// been deprecated in favor of another rule or code. To keep the codebase | ||
/// consistent and up-to-date, prefer the canonical rule code over the deprecated | ||
/// code. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// x = eval(command) # noqa: PGH001 | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// x = eval(command) # noqa: S307 | ||
/// ``` | ||
#[violation] | ||
pub struct RedirectedNOQA { | ||
original: String, | ||
target: String, | ||
} | ||
|
||
impl AlwaysFixableViolation for RedirectedNOQA { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
let RedirectedNOQA { original, target } = self; | ||
format!("`{original}` is a redirect to `{target}`") | ||
} | ||
|
||
fn fix_title(&self) -> String { | ||
let RedirectedNOQA { target, .. } = self; | ||
format!("Replace with `{target}`") | ||
} | ||
} | ||
|
||
/// RUF101 | ||
pub(crate) fn redirected_noqa(diagnostics: &mut Vec<Diagnostic>, noqa_directives: &NoqaDirectives) { | ||
for line in noqa_directives.lines() { | ||
let Directive::Codes(directive) = &line.directive else { | ||
continue; | ||
}; | ||
|
||
for code in directive.iter() { | ||
if let Some(redirected) = get_redirect_target(code.as_str()) { | ||
let mut diagnostic = Diagnostic::new( | ||
RedirectedNOQA { | ||
original: code.to_string(), | ||
target: redirected.to_string(), | ||
}, | ||
code.range(), | ||
); | ||
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement( | ||
redirected.to_string(), | ||
code.range(), | ||
))); | ||
diagnostics.push(diagnostic); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.