From 7e1fdfe809b7ead14edcadab8fe08c7e48252240 Mon Sep 17 00:00:00 2001 From: augustelalande Date: Sun, 17 Mar 2024 20:48:48 -0400 Subject: [PATCH 1/2] fix pylint upstream category --- crates/ruff_dev/src/generate_rules_table.rs | 11 ++++- crates/ruff_linter/src/upstream_categories.rs | 40 +++++++------------ 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/crates/ruff_dev/src/generate_rules_table.rs b/crates/ruff_dev/src/generate_rules_table.rs index c167c018d05c2..0492615cb71cf 100644 --- a/crates/ruff_dev/src/generate_rules_table.rs +++ b/crates/ruff_dev/src/generate_rules_table.rs @@ -180,8 +180,17 @@ pub(crate) fn generate() -> String { .map(|rule| (rule.upstream_category(&linter), rule)) .into_group_map(); + let mut rules_by_upstream_category: Vec<_> = rules_by_upstream_category.iter().collect(); + + // Sort the upstream categories alphabetically by prefix. + rules_by_upstream_category.sort_by(|a, b| { + a.0.as_ref() + .map_or("", |x| x.prefix) + .cmp(b.0.as_ref().map_or("", |x| x.prefix)) + }); + if rules_by_upstream_category.len() > 1 { - for (opt, rules) in &rules_by_upstream_category { + for (opt, rules) in rules_by_upstream_category { if opt.is_some() { let UpstreamCategoryAndPrefix { category, prefix } = opt.unwrap(); table_out.push_str(&format!("#### {category} ({prefix})")); diff --git a/crates/ruff_linter/src/upstream_categories.rs b/crates/ruff_linter/src/upstream_categories.rs index c27c79e2f5b9c..9f94759e26a50 100644 --- a/crates/ruff_linter/src/upstream_categories.rs +++ b/crates/ruff_linter/src/upstream_categories.rs @@ -8,29 +8,19 @@ pub struct UpstreamCategoryAndPrefix { pub prefix: &'static str, } -const PLC: UpstreamCategoryAndPrefix = UpstreamCategoryAndPrefix { +const C: UpstreamCategoryAndPrefix = UpstreamCategoryAndPrefix { category: "Convention", - prefix: "PLC", + prefix: "C", }; -const PLE: UpstreamCategoryAndPrefix = UpstreamCategoryAndPrefix { +const E: UpstreamCategoryAndPrefix = UpstreamCategoryAndPrefix { category: "Error", - prefix: "PLE", + prefix: "E", }; -const PLR: UpstreamCategoryAndPrefix = UpstreamCategoryAndPrefix { +const R: UpstreamCategoryAndPrefix = UpstreamCategoryAndPrefix { category: "Refactor", - prefix: "PLR", -}; - -const PLW: UpstreamCategoryAndPrefix = UpstreamCategoryAndPrefix { - category: "Warning", - prefix: "PLW", -}; - -const E: UpstreamCategoryAndPrefix = UpstreamCategoryAndPrefix { - category: "Error", - prefix: "E", + prefix: "R", }; const W: UpstreamCategoryAndPrefix = UpstreamCategoryAndPrefix { @@ -52,14 +42,14 @@ impl Rule { } } Linter::Pylint => { - if code.starts_with("PLC") { - Some(PLC) - } else if code.starts_with("PLE") { - Some(PLE) - } else if code.starts_with("PLR") { - Some(PLR) - } else if code.starts_with("PLW") { - Some(PLW) + if code.starts_with('C') { + Some(C) + } else if code.starts_with('E') { + Some(E) + } else if code.starts_with('R') { + Some(R) + } else if code.starts_with('W') { + Some(W) } else { None } @@ -73,7 +63,7 @@ impl Linter { pub const fn upstream_categories(&self) -> Option<&'static [UpstreamCategoryAndPrefix]> { match self { Linter::Pycodestyle => Some(&[E, W]), - Linter::Pylint => Some(&[PLC, PLE, PLR, PLW]), + Linter::Pylint => Some(&[C, E, R, W]), _ => None, } } From 01557eff669e1a0812400bbca32ab37c534c2966 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sun, 17 Mar 2024 21:19:47 -0400 Subject: [PATCH 2/2] Unwrap or default --- crates/ruff_dev/src/generate_rules_table.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/crates/ruff_dev/src/generate_rules_table.rs b/crates/ruff_dev/src/generate_rules_table.rs index 0492615cb71cf..453700c1e8b03 100644 --- a/crates/ruff_dev/src/generate_rules_table.rs +++ b/crates/ruff_dev/src/generate_rules_table.rs @@ -183,10 +183,15 @@ pub(crate) fn generate() -> String { let mut rules_by_upstream_category: Vec<_> = rules_by_upstream_category.iter().collect(); // Sort the upstream categories alphabetically by prefix. - rules_by_upstream_category.sort_by(|a, b| { - a.0.as_ref() - .map_or("", |x| x.prefix) - .cmp(b.0.as_ref().map_or("", |x| x.prefix)) + rules_by_upstream_category.sort_by(|(a, _), (b, _)| { + a.as_ref() + .map(|category| category.prefix) + .unwrap_or_default() + .cmp( + b.as_ref() + .map(|category| category.prefix) + .unwrap_or_default(), + ) }); if rules_by_upstream_category.len() > 1 {