diff --git a/crates/ruff_linter/src/rule_selector.rs b/crates/ruff_linter/src/rule_selector.rs index b7e0745d903bcb..d76c48db90ed0f 100644 --- a/crates/ruff_linter/src/rule_selector.rs +++ b/crates/ruff_linter/src/rule_selector.rs @@ -48,6 +48,7 @@ impl FromStr for RuleSelector { type Err = ParseError; fn from_str(s: &str) -> Result { + // **Changes should be reflected in `parse_no_redirect` as well** match s { "ALL" => Ok(Self::All), #[allow(deprecated)] @@ -67,7 +68,6 @@ impl FromStr for RuleSelector { return Ok(Self::Linter(linter)); } - // Does the selector select a single rule? let prefix = RuleCodePrefix::parse(&linter, code) .map_err(|_| ParseError::Unknown(s.to_string()))?; @@ -302,7 +302,7 @@ mod schema { .filter(|p| { // Exclude any prefixes where all of the rules are removed if let Ok(Self::Rule { prefix, .. } | Self::Prefix { prefix, .. }) = - RuleSelector::from_str(p) + RuleSelector::parse_no_redirect(p) { !prefix.rules().all(|rule| rule.is_removed()) } else { @@ -341,6 +341,41 @@ impl RuleSelector { } } } + + /// Parse [`RuleSelector`] from a string; but do not follow redirects. + pub fn parse_no_redirect(s: &str) -> Result { + // **Changes should be reflected in `from_str` as well** + match s { + "ALL" => Ok(Self::All), + #[allow(deprecated)] + "NURSERY" => Ok(Self::Nursery), + "C" => Ok(Self::C), + "T" => Ok(Self::T), + _ => { + let (linter, code) = + Linter::parse_code(s).ok_or_else(|| ParseError::Unknown(s.to_string()))?; + + if code.is_empty() { + return Ok(Self::Linter(linter)); + } + + let prefix = RuleCodePrefix::parse(&linter, code) + .map_err(|_| ParseError::Unknown(s.to_string()))?; + + if is_single_rule_selector(&prefix) { + Ok(Self::Rule { + prefix, + redirected_from: None, + }) + } else { + Ok(Self::Prefix { + prefix, + redirected_from: None, + }) + } + } + } + } } #[derive(EnumIter, PartialEq, Eq, PartialOrd, Ord, Copy, Clone, Debug)] diff --git a/ruff.schema.json b/ruff.schema.json index 5fb4e74e89a1c8..7e1d8a62d017c3 100644 --- a/ruff.schema.json +++ b/ruff.schema.json @@ -3120,7 +3120,6 @@ "PGH", "PGH0", "PGH00", - "PGH001", "PGH003", "PGH004", "PGH005", @@ -3721,7 +3720,6 @@ "TRY004", "TRY2", "TRY20", - "TRY200", "TRY201", "TRY3", "TRY30",