Re-enable the T and C linter prefix selectors (#3452)

This commit is contained in:
Charlie Marsh 2023-03-13 08:20:30 -04:00 committed by GitHub
parent c2750a59ab
commit 227679b5cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 5 deletions

View File

@ -18,9 +18,8 @@ static REDIRECTS: Lazy<HashMap<&'static str, &'static str>> = Lazy::new(|| {
// The following are here because we don't yet have the many-to-one mapping enabled. // The following are here because we don't yet have the many-to-one mapping enabled.
("SIM111", "SIM110"), ("SIM111", "SIM110"),
// The following are deprecated. // The following are deprecated.
("C", "C4"), ("C4", "C40"),
("C9", "C90"), ("C9", "C90"),
("T", "T10"),
("T1", "T10"), ("T1", "T10"),
("T2", "T20"), ("T2", "T20"),
// TODO(charlie): Remove by 2023-02-01. // TODO(charlie): Remove by 2023-02-01.

View File

@ -15,9 +15,17 @@ use crate::rule_redirects::get_redirect;
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum RuleSelector { pub enum RuleSelector {
/// All rules /// Select all rules.
All, 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), Linter(Linter),
/// Select all rules for a given linter with a given prefix.
Prefix { Prefix {
prefix: RuleCodePrefix, prefix: RuleCodePrefix,
redirected_from: Option<&'static str>, redirected_from: Option<&'static str>,
@ -36,6 +44,10 @@ impl FromStr for RuleSelector {
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
if s == "ALL" { if s == "ALL" {
Ok(Self::All) Ok(Self::All)
} else if s == "C" {
Ok(Self::C)
} else if s == "T" {
Ok(Self::T)
} else { } else {
let (s, redirected_from) = match get_redirect(s) { let (s, redirected_from) = match get_redirect(s) {
Some((from, target)) => (target, Some(from)), Some((from, target)) => (target, Some(from)),
@ -70,6 +82,8 @@ impl RuleSelector {
pub fn prefix_and_code(&self) -> (&'static str, &'static str) { pub fn prefix_and_code(&self) -> (&'static str, &'static str) {
match self { match self {
RuleSelector::All => ("", "ALL"), RuleSelector::All => ("", "ALL"),
RuleSelector::C => ("", "C"),
RuleSelector::T => ("", "T"),
RuleSelector::Prefix { prefix, .. } => { RuleSelector::Prefix { prefix, .. } => {
(prefix.linter().common_prefix(), prefix.short_code()) (prefix.linter().common_prefix(), prefix.short_code())
} }
@ -138,6 +152,16 @@ impl IntoIterator for &RuleSelector {
fn into_iter(self) -> Self::IntoIter { fn into_iter(self) -> Self::IntoIter {
match self { match self {
RuleSelector::All => RuleSelectorIter::All(Rule::iter()), 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::Linter(linter) => RuleSelectorIter::Vec(linter.into_iter()),
RuleSelector::Prefix { prefix, .. } => RuleSelectorIter::Vec(prefix.into_iter()), RuleSelector::Prefix { prefix, .. } => RuleSelectorIter::Vec(prefix.into_iter()),
} }
@ -146,6 +170,7 @@ impl IntoIterator for &RuleSelector {
pub enum RuleSelectorIter { pub enum RuleSelectorIter {
All(RuleIter), All(RuleIter),
Chain(std::iter::Chain<std::vec::IntoIter<Rule>, std::vec::IntoIter<Rule>>),
Vec(std::vec::IntoIter<Rule>), Vec(std::vec::IntoIter<Rule>),
} }
@ -155,13 +180,14 @@ impl Iterator for RuleSelectorIter {
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
match self { match self {
RuleSelectorIter::All(iter) => iter.next(), RuleSelectorIter::All(iter) => iter.next(),
RuleSelectorIter::Chain(iter) => iter.next(),
RuleSelectorIter::Vec(iter) => iter.next(), RuleSelectorIter::Vec(iter) => iter.next(),
} }
} }
} }
/// A const alternative to the `impl From<RuleCodePrefix> for RuleSelector` /// A const alternative to the `impl From<RuleCodePrefix> 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<RuleCodePrefix> for // Note that Rust doesn't yet support `impl const From<RuleCodePrefix> for
// RuleSelector` (see https://github.com/rust-lang/rust/issues/67792). // RuleSelector` (see https://github.com/rust-lang/rust/issues/67792).
// TODO(martin): Remove once RuleSelector is an enum with Linter & Rule variants // TODO(martin): Remove once RuleSelector is an enum with Linter & Rule variants
@ -177,7 +203,7 @@ impl JsonSchema for RuleSelector {
"RuleSelector".to_string() "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 { Schema::Object(SchemaObject {
instance_type: Some(InstanceType::String.into()), instance_type: Some(InstanceType::String.into()),
enum_values: Some( enum_values: Some(
@ -221,6 +247,8 @@ impl RuleSelector {
pub(crate) fn specificity(&self) -> Specificity { pub(crate) fn specificity(&self) -> Specificity {
match self { match self {
RuleSelector::All => Specificity::All, RuleSelector::All => Specificity::All,
RuleSelector::T => Specificity::LinterGroup,
RuleSelector::C => Specificity::LinterGroup,
RuleSelector::Linter(..) => Specificity::Linter, RuleSelector::Linter(..) => Specificity::Linter,
RuleSelector::Prefix { prefix, .. } => { RuleSelector::Prefix { prefix, .. } => {
let prefix: &'static str = prefix.short_code(); let prefix: &'static str = prefix.short_code();
@ -240,6 +268,7 @@ impl RuleSelector {
#[derive(EnumIter, PartialEq, Eq, PartialOrd, Ord)] #[derive(EnumIter, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) enum Specificity { pub(crate) enum Specificity {
All, All,
LinterGroup,
Linter, Linter,
Code1Char, Code1Char,
Code2Chars, Code2Chars,