Skip to content

Commit

Permalink
Restore RUF011 documentation (#9758)
Browse files Browse the repository at this point in the history
For consistency with other redirected rules as in
#9755

Follow-up to #9428
  • Loading branch information
zanieb committed Feb 1, 2024
1 parent 994514d commit 836d2ea
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 10 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
1 change: 1 addition & 0 deletions crates/ruff_linter/src/codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,7 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> {
(Ruff, "008") => (RuleGroup::Stable, rules::ruff::rules::MutableDataclassDefault),
(Ruff, "009") => (RuleGroup::Stable, rules::ruff::rules::FunctionCallInDataclassDefaultArgument),
(Ruff, "010") => (RuleGroup::Stable, rules::ruff::rules::ExplicitFStringTypeConversion),
(Ruff, "011") => (RuleGroup::Removed, rules::ruff::rules::RuffStaticKeyDictComprehension),
(Ruff, "012") => (RuleGroup::Stable, rules::ruff::rules::MutableClassDefault),
(Ruff, "013") => (RuleGroup::Stable, rules::ruff::rules::ImplicitOptional),
(Ruff, "015") => (RuleGroup::Stable, rules::ruff::rules::UnnecessaryIterableAllocationForFirstElement),
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> {
Some(self.cmp(other))
}
}

impl FromStr for RuleSelector {
type Err = ParseError;

Expand Down
5 changes: 3 additions & 2 deletions crates/ruff_linter/src/rules/ruff/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub(crate) use parenthesize_logical_operators::*;
pub(crate) use quadratic_list_summation::*;
pub(crate) use sort_dunder_all::*;
pub(crate) use sort_dunder_slots::*;
pub(crate) use static_key_dict_comprehension::*;
#[cfg(feature = "test-rules")]
pub(crate) use test_rules::*;
pub(crate) use unnecessary_dict_comprehension_for_iterable::*;
Expand All @@ -42,9 +43,11 @@ mod mutable_fromkeys_value;
mod never_union;
mod pairwise_over_zipped;
mod parenthesize_logical_operators;
mod quadratic_list_summation;
mod sequence_sorting;
mod sort_dunder_all;
mod sort_dunder_slots;
mod static_key_dict_comprehension;
#[cfg(feature = "test-rules")]
pub(crate) mod test_rules;
mod unnecessary_dict_comprehension_for_iterable;
Expand All @@ -58,5 +61,3 @@ pub(crate) enum Context {
Docstring,
Comment,
}

mod quadratic_list_summation;
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use ruff_diagnostics::Violation;
use ruff_macros::{derive_message_formats, violation};

/// ## Removed
/// This rule was implemented in `flake8-bugbear` and has been remapped to [B035]
///
/// ## What it does
/// Checks for dictionary comprehensions that use a static key, like a string
/// literal or a variable defined outside the comprehension.
///
/// ## Why is this bad?
/// Using a static key (like a string literal) in a dictionary comprehension
/// is usually a mistake, as it will result in a dictionary with only one key,
/// despite the comprehension iterating over multiple values.
///
/// ## Example
/// ```python
/// data = ["some", "Data"]
/// {"key": value.upper() for value in data}
/// ```
///
/// Use instead:
/// ```python
/// data = ["some", "Data"]
/// {value: value.upper() for value in data}
/// ```
///
/// [B035]: https://docs.astral.sh/ruff/rules/static-key-dict-comprehension/
#[violation]
pub struct RuffStaticKeyDictComprehension;

impl Violation for RuffStaticKeyDictComprehension {
#[derive_message_formats]
fn message(&self) -> String {
format!("Dictionary comprehension uses static key")
}
}
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 836d2ea

Please sign in to comment.