Skip to content
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
5 changes: 3 additions & 2 deletions crates/ruff_linter/src/checkers/noqa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::rules::pygrep_hooks;
use crate::rules::ruff;
use crate::rules::ruff::rules::{UnusedCodes, UnusedNOQA};
use crate::settings::LinterSettings;
use crate::suppression::Suppressions;
use crate::suppression::{Suppressions, is_suppression_diagnostic_code};
use crate::{Edit, Fix, Locator};

use super::ast::LintContext;
Expand Down Expand Up @@ -178,7 +178,8 @@ pub(crate) fn check_noqa(
} || settings
.external
.iter()
.any(|external| code.starts_with(external));
.any(|external| code.starts_with(external))
|| is_suppression_diagnostic_code(code);

if is_code_used {
valid_codes.push(original_code);
Expand Down
34 changes: 32 additions & 2 deletions crates/ruff_linter/src/suppression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use ruff_python_ast::token::{TokenKind, Tokens};
use ruff_python_index::Indexer;
use rustc_hash::FxHashSet;
use std::cell::Cell;
use std::sync::LazyLock;
use std::{error::Error, fmt::Formatter};
use thiserror::Error;

Expand All @@ -15,7 +16,7 @@ use ruff_text_size::{Ranged, TextLen, TextRange, TextSize, TextSlice};
use smallvec::{SmallVec, smallvec};

use crate::checkers::ast::LintContext;
use crate::codes::Rule;
use crate::codes::{NoqaCode, Rule};
use crate::fix::edits::delete_comment;
use crate::rule_redirects::get_redirect_target;
use crate::rules::ruff::rules::{
Expand All @@ -24,6 +25,20 @@ use crate::rules::ruff::rules::{
};
use crate::{Locator, Violation};

/// Determine if the rule code is a `RUF10x` diagnostic generated while processing diagnostics
pub fn is_suppression_diagnostic_code(code: &str) -> bool {
static SUPPRESSION_CODES: LazyLock<Vec<NoqaCode>> = LazyLock::new(|| {
vec![
Rule::UnusedNOQA.noqa_code(),
Rule::RedirectedNOQA.noqa_code(),
Rule::InvalidRuleCode.noqa_code(),
Rule::InvalidSuppressionComment.noqa_code(),
Rule::UnmatchedSuppressionComment.noqa_code(),
]
});
SUPPRESSION_CODES.iter().any(|sc| sc == &code)
}

#[derive(Clone, Debug, Eq, PartialEq)]
enum SuppressionAction {
Disable,
Expand Down Expand Up @@ -836,9 +851,24 @@ mod tests {

use crate::suppression::{
InvalidSuppression, ParseError, Suppression, SuppressionAction, SuppressionComment,
SuppressionParser, Suppressions,
SuppressionParser, Suppressions, is_suppression_diagnostic_code,
};

#[test]
fn suppression_diagnostic_codes() {
// Suppression-related diagnostics
assert!(is_suppression_diagnostic_code("RUF100")); // UnusedNOQA
assert!(is_suppression_diagnostic_code("RUF101")); // RedirectedNOQA
assert!(is_suppression_diagnostic_code("RUF102")); // InvalidRuleCode
assert!(is_suppression_diagnostic_code("RUF103")); // InvalidSuppressionComment
assert!(is_suppression_diagnostic_code("RUF104")); // UnmatchedSuppressionComment

// Random rules
assert!(!is_suppression_diagnostic_code("RUF013"));
assert!(!is_suppression_diagnostic_code("F401"));
assert!(!is_suppression_diagnostic_code("E101"));
}

#[test]
fn no_suppression() {
let source = "
Expand Down