Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn on invalid # noqa rule codes #12811

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion crates/ruff_linter/src/checkers/noqa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ pub(crate) fn check_noqa(
let exemption = FileExemption::from(&file_noqa_directives);

// Extract all `noqa` directives.
let mut noqa_directives = NoqaDirectives::from_commented_ranges(comment_ranges, path, locator);
let mut noqa_directives =
NoqaDirectives::from_commented_ranges(comment_ranges, &settings.external, path, locator);

// Indices of diagnostics that were ignored by a `noqa` directive.
let mut ignored_diagnostics = vec![];
Expand Down
47 changes: 41 additions & 6 deletions crates/ruff_linter/src/noqa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub fn generate_noqa_edits(
let file_directives =
FileNoqaDirectives::extract(locator.contents(), comment_ranges, external, path, locator);
let exemption = FileExemption::from(&file_directives);
let directives = NoqaDirectives::from_commented_ranges(comment_ranges, path, locator);
let directives = NoqaDirectives::from_commented_ranges(comment_ranges, external, path, locator);
let comments = find_noqa_comments(diagnostics, locator, &exemption, &directives, noqa_line_for);
build_noqa_edits_by_diagnostic(comments, locator, line_ending)
}
Expand Down Expand Up @@ -183,7 +183,7 @@ impl<'a> Directive<'a> {
// Extract, e.g., the `401` in `F401`.
let suffix = line[prefix..]
.chars()
.take_while(char::is_ascii_digit)
.take_while(char::is_ascii_alphanumeric)
.count();
if prefix > 0 && suffix > 0 {
Some(&line[..prefix + suffix])
Expand Down Expand Up @@ -550,7 +550,7 @@ impl<'a> ParsedFileExemption<'a> {
// Extract, e.g., the `401` in `F401`.
let suffix = line[prefix..]
.chars()
.take_while(char::is_ascii_digit)
.take_while(char::is_ascii_alphanumeric)
.count();
if prefix > 0 && suffix > 0 {
Some(&line[..prefix + suffix])
Expand Down Expand Up @@ -623,7 +623,7 @@ fn add_noqa_inner(
FileNoqaDirectives::extract(locator.contents(), comment_ranges, external, path, locator);
let exemption = FileExemption::from(&directives);

let directives = NoqaDirectives::from_commented_ranges(comment_ranges, path, locator);
let directives = NoqaDirectives::from_commented_ranges(comment_ranges, external, path, locator);

let comments = find_noqa_comments(diagnostics, locator, &exemption, &directives, noqa_line_for);

Expand Down Expand Up @@ -897,7 +897,7 @@ pub(crate) struct NoqaDirectiveLine<'a> {
pub(crate) directive: Directive<'a>,
/// The codes that are ignored by the directive.
pub(crate) matches: Vec<NoqaCode>,
// Whether the directive applies to range.end
/// Whether the directive applies to `range.end`.
pub(crate) includes_end: bool,
}

Expand All @@ -916,6 +916,7 @@ pub(crate) struct NoqaDirectives<'a> {
impl<'a> NoqaDirectives<'a> {
pub(crate) fn from_commented_ranges(
comment_ranges: &CommentRanges,
external: &[String],
path: &Path,
locator: &'a Locator<'a>,
) -> Self {
Expand All @@ -930,7 +931,29 @@ impl<'a> NoqaDirectives<'a> {
warn!("Invalid `# noqa` directive on {path_display}:{line}: {err}");
}
Ok(Some(directive)) => {
// noqa comments are guaranteed to be single line.
if let Directive::Codes(codes) = &directive {
// Warn on invalid rule codes.
for code in &codes.codes {
// Ignore externally-defined rules.
if !external
.iter()
.any(|external| code.as_str().starts_with(external))
{
if Rule::from_code(
get_redirect_target(code.as_str()).unwrap_or(code.as_str()),
)
.is_err()
{
#[allow(deprecated)]
let line = locator.compute_line_index(range.start());
let path_display = relativize_path(path);
warn!("Invalid rule code provided to `# noqa` at {path_display}:{line}: {code}");
}
}
}
}

// `# noqa` comments are guaranteed to be single line.
let range = locator.line_range(range.start());
directives.push(NoqaDirectiveLine {
range,
Expand Down Expand Up @@ -1193,6 +1216,18 @@ mod tests {
assert_debug_snapshot!(Directive::try_extract(source, TextSize::default()));
}

#[test]
fn noqa_squashed_codes() {
let source = "# noqa: F401F841";
assert_debug_snapshot!(Directive::try_extract(source, TextSize::default()));
}

#[test]
fn noqa_empty_comma() {
let source = "# noqa: F401,,F841";
assert_debug_snapshot!(Directive::try_extract(source, TextSize::default()));
}

#[test]
fn noqa_invalid_suffix() {
let source = "# noqa[F401]";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
source: crates/ruff_linter/src/noqa.rs
expression: "Directive::try_extract(source, TextSize::default())"
---
Ok(
Some(
Codes(
Codes {
range: 0..18,
codes: [
Code {
code: "F401",
range: 8..12,
},
Code {
code: "F841",
range: 14..18,
},
],
},
),
),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
source: crates/ruff_linter/src/noqa.rs
expression: "Directive::try_extract(source, TextSize::default())"
---
Ok(
Some(
Codes(
Codes {
range: 0..16,
codes: [
Code {
code: "F401F841",
range: 8..16,
},
],
},
),
),
)
Loading