From c23bf395e2cb2d925aef83fa3e09a714f9c835ba Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sun, 11 Aug 2024 19:23:33 -0400 Subject: [PATCH] Warn on invalid # noqa rule codes --- crates/ruff_linter/src/checkers/noqa.rs | 3 ++- crates/ruff_linter/src/noqa.rs | 29 ++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/crates/ruff_linter/src/checkers/noqa.rs b/crates/ruff_linter/src/checkers/noqa.rs index e893551dfc..f392f568cc 100644 --- a/crates/ruff_linter/src/checkers/noqa.rs +++ b/crates/ruff_linter/src/checkers/noqa.rs @@ -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![]; diff --git a/crates/ruff_linter/src/noqa.rs b/crates/ruff_linter/src/noqa.rs index 7830330344..ebfc40823f 100644 --- a/crates/ruff_linter/src/noqa.rs +++ b/crates/ruff_linter/src/noqa.rs @@ -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) } @@ -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); @@ -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 { @@ -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,