[`ruff`] Stabilize checking for file-level directives in `unused-noqa` (`RUF100`) (#18497)

Note that the preview behavior was not documented (shame on us!) so the
documentation was not modified.

---------

Co-authored-by: Brent Westbrook <brentrwestbrook@gmail.com>
This commit is contained in:
Dylan 2025-06-06 16:51:49 -05:00 committed by Brent Westbrook
parent eb5abda8ac
commit 8b1ce32f04
3 changed files with 17 additions and 38 deletions

View File

@ -12,7 +12,6 @@ use crate::fix::edits::delete_comment;
use crate::noqa::{ use crate::noqa::{
Code, Directive, FileExemption, FileNoqaDirectives, NoqaDirectives, NoqaMapping, Code, Directive, FileExemption, FileNoqaDirectives, NoqaDirectives, NoqaMapping,
}; };
use crate::preview::is_check_file_level_directives_enabled;
use crate::registry::{AsRule, Rule, RuleSet}; use crate::registry::{AsRule, Rule, RuleSet};
use crate::rule_redirects::get_redirect_target; use crate::rule_redirects::get_redirect_target;
use crate::rules::pygrep_hooks; use crate::rules::pygrep_hooks;
@ -112,25 +111,16 @@ pub(crate) fn check_noqa(
&& !exemption.includes(Rule::UnusedNOQA) && !exemption.includes(Rule::UnusedNOQA)
&& !per_file_ignores.contains(Rule::UnusedNOQA) && !per_file_ignores.contains(Rule::UnusedNOQA)
{ {
let directives: Vec<_> = if is_check_file_level_directives_enabled(settings) { let directives = noqa_directives
noqa_directives .lines()
.lines() .iter()
.iter() .map(|line| (&line.directive, &line.matches, false))
.map(|line| (&line.directive, &line.matches, false)) .chain(
.chain( file_noqa_directives
file_noqa_directives .lines()
.lines() .iter()
.iter() .map(|line| (&line.parsed_file_exemption, &line.matches, true)),
.map(|line| (&line.parsed_file_exemption, &line.matches, true)), );
)
.collect()
} else {
noqa_directives
.lines()
.iter()
.map(|line| (&line.directive, &line.matches, false))
.collect()
};
for (directive, matches, is_file_level) in directives { for (directive, matches, is_file_level) in directives {
match directive { match directive {
Directive::All(directive) => { Directive::All(directive) => {

View File

@ -109,11 +109,6 @@ pub(crate) const fn is_allow_nested_roots_enabled(settings: &LinterSettings) ->
settings.preview.is_enabled() settings.preview.is_enabled()
} }
// https://github.com/astral-sh/ruff/pull/17061
pub(crate) const fn is_check_file_level_directives_enabled(settings: &LinterSettings) -> bool {
settings.preview.is_enabled()
}
// https://github.com/astral-sh/ruff/pull/18208 // https://github.com/astral-sh/ruff/pull/18208
pub(crate) const fn is_multiple_with_statements_fix_safe_enabled( pub(crate) const fn is_multiple_with_statements_fix_safe_enabled(
settings: &LinterSettings, settings: &LinterSettings,

View File

@ -324,10 +324,7 @@ mod tests {
fn ruff_noqa_filedirective_unused() -> Result<()> { fn ruff_noqa_filedirective_unused() -> Result<()> {
let diagnostics = test_path( let diagnostics = test_path(
Path::new("ruff/RUF100_6.py"), Path::new("ruff/RUF100_6.py"),
&settings::LinterSettings { &settings::LinterSettings::for_rules(vec![Rule::UnusedNOQA]),
preview: PreviewMode::Enabled,
..settings::LinterSettings::for_rules(vec![Rule::UnusedNOQA])
},
)?; )?;
assert_messages!(diagnostics); assert_messages!(diagnostics);
Ok(()) Ok(())
@ -337,15 +334,12 @@ mod tests {
fn ruff_noqa_filedirective_unused_last_of_many() -> Result<()> { fn ruff_noqa_filedirective_unused_last_of_many() -> Result<()> {
let diagnostics = test_path( let diagnostics = test_path(
Path::new("ruff/RUF100_7.py"), Path::new("ruff/RUF100_7.py"),
&settings::LinterSettings { &settings::LinterSettings::for_rules(vec![
preview: PreviewMode::Enabled, Rule::UnusedNOQA,
..settings::LinterSettings::for_rules(vec![ Rule::FStringMissingPlaceholders,
Rule::UnusedNOQA, Rule::LineTooLong,
Rule::FStringMissingPlaceholders, Rule::UnusedVariable,
Rule::LineTooLong, ]),
Rule::UnusedVariable,
])
},
)?; )?;
assert_messages!(diagnostics); assert_messages!(diagnostics);
Ok(()) Ok(())