Skip to content

Commit

Permalink
Sort rule selection warnings for determinism
Browse files Browse the repository at this point in the history
  • Loading branch information
zanieb committed Feb 1, 2024
1 parent 12b5046 commit fa7c2b4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
4 changes: 2 additions & 2 deletions crates/ruff/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1228,8 +1228,8 @@ fn deprecated_multiple_direct() {
Found 2 errors.
----- stderr -----
warning: Rule `RUF921` is deprecated and will be removed in a future release.
warning: Rule `RUF920` is deprecated and will be removed in a future release.
warning: Rule `RUF921` is deprecated and will be removed in a future release.
"###);
}

Expand Down Expand Up @@ -1297,8 +1297,8 @@ fn deprecated_multiple_direct_preview_enabled() {
----- stderr -----
ruff failed
Cause: Selection of deprecated rules is not allowed when preview is enabled. Remove selection of:
- RUF921
- RUF920
- RUF921
"###);
}
Expand Down
14 changes: 14 additions & 0 deletions crates/ruff_linter/src/rule_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ impl From<Linter> for RuleSelector {
}
}

impl Ord for RuleSelector {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
// TODO(zanieb): We ought to put "ALL" and "Linter" selectors
// above those that are rule specific but it's not critical for now
self.prefix_and_code().cmp(&other.prefix_and_code())
}
}

impl PartialOrd for RuleSelector {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.prefix_and_code().partial_cmp(&other.prefix_and_code())
}
}

impl FromStr for RuleSelector {
type Err = ParseError;

Expand Down
15 changes: 9 additions & 6 deletions crates/ruff_workspace/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ impl LintConfiguration {
}
}

let removed_selectors = removed_selectors.iter().collect::<Vec<_>>();
let removed_selectors = removed_selectors.iter().sorted().collect::<Vec<_>>();
match removed_selectors.as_slice() {
[] => (),
[selection] => {
Expand All @@ -974,7 +974,7 @@ impl LintConfiguration {
}
}

for (from, target) in redirects {
for (from, target) in redirects.iter().sorted_by_key(|item| item.0) {
// TODO(martin): This belongs into the ruff crate.
warn_user_once_by_id!(
from,
Expand All @@ -984,7 +984,10 @@ impl LintConfiguration {
);
}

let deprecated_nursery_selectors = deprecated_nursery_selectors.iter().collect::<Vec<_>>();
let deprecated_nursery_selectors = deprecated_nursery_selectors
.iter()
.sorted()
.collect::<Vec<_>>();
match deprecated_nursery_selectors.as_slice() {
[] => (),
[selection] => {
Expand All @@ -1005,14 +1008,14 @@ impl LintConfiguration {
}

if preview.mode.is_disabled() {
for selection in deprecated_selectors {
for selection in deprecated_selectors.iter().sorted() {
let (prefix, code) = selection.prefix_and_code();
warn_user!(
"Rule `{prefix}{code}` is deprecated and will be removed in a future release.",
);
}
} else {
let deprecated_selectors = deprecated_selectors.iter().collect::<Vec<_>>();
let deprecated_selectors = deprecated_selectors.iter().sorted().collect::<Vec<_>>();
match deprecated_selectors.as_slice() {
[] => (),
[selection] => {
Expand All @@ -1033,7 +1036,7 @@ impl LintConfiguration {
}
}

for selection in ignored_preview_selectors {
for selection in ignored_preview_selectors.iter().sorted() {
let (prefix, code) = selection.prefix_and_code();
warn_user!("Selection `{prefix}{code}` has no effect because preview is not enabled.",);
}
Expand Down

0 comments on commit fa7c2b4

Please sign in to comment.