diff --git a/crates/ruff/src/rule_redirects.rs b/crates/ruff/src/rule_redirects.rs index c934d1ead96cf..8a8c214965156 100644 --- a/crates/ruff/src/rule_redirects.rs +++ b/crates/ruff/src/rule_redirects.rs @@ -18,9 +18,8 @@ static REDIRECTS: Lazy> = Lazy::new(|| { // The following are here because we don't yet have the many-to-one mapping enabled. ("SIM111", "SIM110"), // The following are deprecated. - ("C", "C4"), + ("C4", "C40"), ("C9", "C90"), - ("T", "T10"), ("T1", "T10"), ("T2", "T20"), // TODO(charlie): Remove by 2023-02-01. diff --git a/crates/ruff/src/rule_selector.rs b/crates/ruff/src/rule_selector.rs index ec806961e636d..9a01f03ed0803 100644 --- a/crates/ruff/src/rule_selector.rs +++ b/crates/ruff/src/rule_selector.rs @@ -15,9 +15,17 @@ use crate::rule_redirects::get_redirect; #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum RuleSelector { - /// All rules + /// Select all rules. All, + /// Legacy category to select both the `mccabe` and `flake8-comprehensions` linters + /// via a single selector. + C, + /// Legacy category to select both the `flake8-debugger` and `flake8-print` linters + /// via a single selector. + T, + /// Select all rules for a given linter. Linter(Linter), + /// Select all rules for a given linter with a given prefix. Prefix { prefix: RuleCodePrefix, redirected_from: Option<&'static str>, @@ -36,6 +44,10 @@ impl FromStr for RuleSelector { fn from_str(s: &str) -> Result { if s == "ALL" { Ok(Self::All) + } else if s == "C" { + Ok(Self::C) + } else if s == "T" { + Ok(Self::T) } else { let (s, redirected_from) = match get_redirect(s) { Some((from, target)) => (target, Some(from)), @@ -70,6 +82,8 @@ impl RuleSelector { pub fn prefix_and_code(&self) -> (&'static str, &'static str) { match self { RuleSelector::All => ("", "ALL"), + RuleSelector::C => ("", "C"), + RuleSelector::T => ("", "T"), RuleSelector::Prefix { prefix, .. } => { (prefix.linter().common_prefix(), prefix.short_code()) } @@ -138,6 +152,16 @@ impl IntoIterator for &RuleSelector { fn into_iter(self) -> Self::IntoIter { match self { RuleSelector::All => RuleSelectorIter::All(Rule::iter()), + RuleSelector::C => RuleSelectorIter::Chain( + Linter::Flake8Comprehensions + .into_iter() + .chain(Linter::McCabe.into_iter()), + ), + RuleSelector::T => RuleSelectorIter::Chain( + Linter::Flake8Debugger + .into_iter() + .chain(Linter::Flake8Print.into_iter()), + ), RuleSelector::Linter(linter) => RuleSelectorIter::Vec(linter.into_iter()), RuleSelector::Prefix { prefix, .. } => RuleSelectorIter::Vec(prefix.into_iter()), } @@ -146,6 +170,7 @@ impl IntoIterator for &RuleSelector { pub enum RuleSelectorIter { All(RuleIter), + Chain(std::iter::Chain, std::vec::IntoIter>), Vec(std::vec::IntoIter), } @@ -155,13 +180,14 @@ impl Iterator for RuleSelectorIter { fn next(&mut self) -> Option { match self { RuleSelectorIter::All(iter) => iter.next(), + RuleSelectorIter::Chain(iter) => iter.next(), RuleSelectorIter::Vec(iter) => iter.next(), } } } /// A const alternative to the `impl From for RuleSelector` -// to let us keep the fields of RuleSelector private. +/// to let us keep the fields of [`RuleSelector`] private. // Note that Rust doesn't yet support `impl const From for // RuleSelector` (see https://github.com/rust-lang/rust/issues/67792). // TODO(martin): Remove once RuleSelector is an enum with Linter & Rule variants @@ -177,7 +203,7 @@ impl JsonSchema for RuleSelector { "RuleSelector".to_string() } - fn json_schema(_gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema { + fn json_schema(_gen: &mut schemars::gen::SchemaGenerator) -> Schema { Schema::Object(SchemaObject { instance_type: Some(InstanceType::String.into()), enum_values: Some( @@ -221,6 +247,8 @@ impl RuleSelector { pub(crate) fn specificity(&self) -> Specificity { match self { RuleSelector::All => Specificity::All, + RuleSelector::T => Specificity::LinterGroup, + RuleSelector::C => Specificity::LinterGroup, RuleSelector::Linter(..) => Specificity::Linter, RuleSelector::Prefix { prefix, .. } => { let prefix: &'static str = prefix.short_code(); @@ -240,6 +268,7 @@ impl RuleSelector { #[derive(EnumIter, PartialEq, Eq, PartialOrd, Ord)] pub(crate) enum Specificity { All, + LinterGroup, Linter, Code1Char, Code2Chars,