From d88123d649dbda80d5c41dbd1985be50f899ee39 Mon Sep 17 00:00:00 2001 From: Simon Brugman Date: Sun, 12 Feb 2023 12:10:07 +0100 Subject: [PATCH 1/2] [`flake8-comprehensions`] improve autofix for C401, C402 and C417 handle autofix when parent is f-string --- README.md | 6 +- .../fixtures/flake8_comprehensions/C401.py | 4 +- .../fixtures/flake8_comprehensions/C402.py | 2 + .../fixtures/flake8_comprehensions/C417.py | 4 ++ crates/ruff/src/checkers/ast.rs | 14 ++++- .../src/rules/flake8_comprehensions/fixes.rs | 40 ++++++++++++- .../rules/unnecessary_generator_dict.rs | 19 +++++++ .../rules/unnecessary_generator_list.rs | 17 ++++++ .../rules/unnecessary_generator_set.rs | 25 ++++++++- .../rules/unnecessary_map.rs | 3 + ...8_comprehensions__tests__C401_C401.py.snap | 56 ++++++++++++++++++- ...8_comprehensions__tests__C402_C402.py.snap | 38 ++++++++++++- ...8_comprehensions__tests__C417_C417.py.snap | 42 +++++++++++++- docs/rules/unnecessary-generator-dict.md | 22 ++++++++ docs/rules/unnecessary-generator-list.md | 22 ++++++++ docs/rules/unnecessary-generator-set.md | 22 ++++++++ 16 files changed, 322 insertions(+), 14 deletions(-) create mode 100644 docs/rules/unnecessary-generator-dict.md create mode 100644 docs/rules/unnecessary-generator-list.md create mode 100644 docs/rules/unnecessary-generator-set.md diff --git a/README.md b/README.md index faef76d6a47f4..08ceb808a6a30 100644 --- a/README.md +++ b/README.md @@ -1036,9 +1036,9 @@ For more, see [flake8-comprehensions](https://pypi.org/project/flake8-comprehens | Code | Name | Message | Fix | | ---- | ---- | ------- | --- | -| C400 | unnecessary-generator-list | Unnecessary generator (rewrite as a `list` comprehension) | 🛠 | -| C401 | unnecessary-generator-set | Unnecessary generator (rewrite as a `set` comprehension) | 🛠 | -| C402 | unnecessary-generator-dict | Unnecessary generator (rewrite as a `dict` comprehension) | 🛠 | +| C400 | [unnecessary-generator-list](https://github.com/charliermarsh/ruff/blob/main/docs/rules/unnecessary-generator-list.md) | Unnecessary generator (rewrite as a `list` comprehension) | 🛠 | +| C401 | [unnecessary-generator-set](https://github.com/charliermarsh/ruff/blob/main/docs/rules/unnecessary-generator-set.md) | Unnecessary generator (rewrite as a `set` comprehension) | 🛠 | +| C402 | [unnecessary-generator-dict](https://github.com/charliermarsh/ruff/blob/main/docs/rules/unnecessary-generator-dict.md) | Unnecessary generator (rewrite as a `dict` comprehension) | 🛠 | | C403 | unnecessary-list-comprehension-set | Unnecessary `list` comprehension (rewrite as a `set` comprehension) | 🛠 | | C404 | unnecessary-list-comprehension-dict | Unnecessary `list` comprehension (rewrite as a `dict` comprehension) | 🛠 | | C405 | unnecessary-literal-set | Unnecessary `{obj_type}` literal (rewrite as a `set` literal) | 🛠 | diff --git a/crates/ruff/resources/test/fixtures/flake8_comprehensions/C401.py b/crates/ruff/resources/test/fixtures/flake8_comprehensions/C401.py index fd4637cb4366a..f38ca1e8737ab 100644 --- a/crates/ruff/resources/test/fixtures/flake8_comprehensions/C401.py +++ b/crates/ruff/resources/test/fixtures/flake8_comprehensions/C401.py @@ -2,7 +2,9 @@ x = set( x for x in range(3) ) - +y = f'{set(a if a < 6 else 0 for a in range(3))}' +_ = '{}'.format(set(a if a < 6 else 0 for a in range(3))) +print(f'Hello {set(a for a in range(3))} World') def set(*args, **kwargs): return None diff --git a/crates/ruff/resources/test/fixtures/flake8_comprehensions/C402.py b/crates/ruff/resources/test/fixtures/flake8_comprehensions/C402.py index fa4db2511733c..20db252700f48 100644 --- a/crates/ruff/resources/test/fixtures/flake8_comprehensions/C402.py +++ b/crates/ruff/resources/test/fixtures/flake8_comprehensions/C402.py @@ -3,3 +3,5 @@ (x, x) for x in range(3) ) dict(((x, x) for x in range(3)), z=3) +y = f'{dict((x, x) for x in range(3))}' +print(f'Hello {dict((x, x) for x in range(3))} World') diff --git a/crates/ruff/resources/test/fixtures/flake8_comprehensions/C417.py b/crates/ruff/resources/test/fixtures/flake8_comprehensions/C417.py index 8abbdab6c812b..e0353b49c3dad 100644 --- a/crates/ruff/resources/test/fixtures/flake8_comprehensions/C417.py +++ b/crates/ruff/resources/test/fixtures/flake8_comprehensions/C417.py @@ -11,6 +11,10 @@ all(map(lambda v: isinstance(v, dict), nums)) filter(func, map(lambda v: v, nums)) +# When inside f-string, then the fix should be surrounded by whitespace +_ = f"{set(map(lambda x: x % 2 == 0, nums))}" +_ = f"{dict(map(lambda v: (v, v**2), nums))}" + # Error, but unfixable. # For simple expressions, this could be: `(x if x else 1 for x in nums)`. # For more complex expressions, this would differ: `(x + 2 if x else 3 for x in nums)`. diff --git a/crates/ruff/src/checkers/ast.rs b/crates/ruff/src/checkers/ast.rs index 5bead3b1cc69f..14596b849b89a 100644 --- a/crates/ruff/src/checkers/ast.rs +++ b/crates/ruff/src/checkers/ast.rs @@ -2434,12 +2434,22 @@ where } if self.settings.rules.enabled(&Rule::UnnecessaryGeneratorSet) { flake8_comprehensions::rules::unnecessary_generator_set( - self, expr, func, args, keywords, + self, + expr, + self.current_expr_parent().map(Into::into), + func, + args, + keywords, ); } if self.settings.rules.enabled(&Rule::UnnecessaryGeneratorDict) { flake8_comprehensions::rules::unnecessary_generator_dict( - self, expr, func, args, keywords, + self, + expr, + self.current_expr_parent().map(Into::into), + func, + args, + keywords, ); } if self diff --git a/crates/ruff/src/rules/flake8_comprehensions/fixes.rs b/crates/ruff/src/rules/flake8_comprehensions/fixes.rs index 90b93e73aa341..ae52d6fcc9720 100644 --- a/crates/ruff/src/rules/flake8_comprehensions/fixes.rs +++ b/crates/ruff/src/rules/flake8_comprehensions/fixes.rs @@ -79,6 +79,7 @@ pub fn fix_unnecessary_generator_set( locator: &Locator, stylist: &Stylist, expr: &rustpython_parser::ast::Expr, + parent: Option<&rustpython_parser::ast::Expr>, ) -> Result { // Expr(Call(GeneratorExp)))) -> Expr(SetComp))) let module_text = locator.slice_source_code_range(&Range::from_located(expr)); @@ -113,8 +114,17 @@ pub fn fix_unnecessary_generator_set( }; tree.codegen(&mut state); + let mut content = state.to_string(); + + // If parent is f-string then surround with spaces + if let Some(parent_element) = parent { + if let &rustpython_parser::ast::ExprKind::FormattedValue { .. } = &parent_element.node { + content = format!(" {content} "); + } + } + Ok(Fix::replacement( - state.to_string(), + content, expr.location, expr.end_location.unwrap(), )) @@ -126,6 +136,7 @@ pub fn fix_unnecessary_generator_dict( locator: &Locator, stylist: &Stylist, expr: &rustpython_parser::ast::Expr, + parent: Option<&rustpython_parser::ast::Expr>, ) -> Result { let module_text = locator.slice_source_code_range(&Range::from_located(expr)); let mut tree = match_module(module_text)?; @@ -176,8 +187,17 @@ pub fn fix_unnecessary_generator_dict( }; tree.codegen(&mut state); + let mut content = state.to_string(); + + // If parent is f-string then surround with spaces + if let Some(parent_element) = parent { + if let &rustpython_parser::ast::ExprKind::FormattedValue { .. } = &parent_element.node { + content = format!(" {content} "); + } + } + Ok(Fix::replacement( - state.to_string(), + content, expr.location, expr.end_location.unwrap(), )) @@ -921,6 +941,7 @@ pub fn fix_unnecessary_map( locator: &Locator, stylist: &Stylist, expr: &rustpython_parser::ast::Expr, + parent: Option<&rustpython_parser::ast::Expr>, kind: &str, ) -> Result { let module_text = locator.slice_source_code_range(&Range::from_located(expr)); @@ -1061,8 +1082,21 @@ pub fn fix_unnecessary_map( }; tree.codegen(&mut state); + let mut content = state.to_string(); + + // If parent is f-string then surround with spaces + if kind == "set" || kind == "dict" { + if let Some(parent_element) = parent { + if let &rustpython_parser::ast::ExprKind::FormattedValue { .. } = + &parent_element.node + { + content = format!(" {content} "); + } + } + } + Ok(Fix::replacement( - state.to_string(), + content, expr.location, expr.end_location.unwrap(), )) diff --git a/crates/ruff/src/rules/flake8_comprehensions/rules/unnecessary_generator_dict.rs b/crates/ruff/src/rules/flake8_comprehensions/rules/unnecessary_generator_dict.rs index 3b8ad8bb0f8a7..771ca456771e5 100644 --- a/crates/ruff/src/rules/flake8_comprehensions/rules/unnecessary_generator_dict.rs +++ b/crates/ruff/src/rules/flake8_comprehensions/rules/unnecessary_generator_dict.rs @@ -10,6 +10,23 @@ use crate::rules::flake8_comprehensions::fixes; use crate::violation::AlwaysAutofixableViolation; define_violation!( + /// ## What it does + /// Checks for unnecessary generator that can be rewritten as `dict` comprehension. + /// + /// ## Why is this bad? + /// It is unnecessary to use `dict` around a generator expression, since there are + /// equivalent comprehensions for these types. + /// + /// ## Examples + /// ```python + /// dict((x, f(x)) for x in foo) + /// ``` + /// + /// Use instead: + /// ```python + /// {x: f(x) for x in foo} + /// ``` + /// pub struct UnnecessaryGeneratorDict; ); impl AlwaysAutofixableViolation for UnnecessaryGeneratorDict { @@ -27,6 +44,7 @@ impl AlwaysAutofixableViolation for UnnecessaryGeneratorDict { pub fn unnecessary_generator_dict( checker: &mut Checker, expr: &Expr, + parent: Option<&Expr>, func: &Expr, args: &[Expr], keywords: &[Keyword], @@ -44,6 +62,7 @@ pub fn unnecessary_generator_dict( checker.locator, checker.stylist, expr, + parent, ) { Ok(fix) => { diagnostic.amend(fix); diff --git a/crates/ruff/src/rules/flake8_comprehensions/rules/unnecessary_generator_list.rs b/crates/ruff/src/rules/flake8_comprehensions/rules/unnecessary_generator_list.rs index 147cb8ea2d78d..4950c064464c9 100644 --- a/crates/ruff/src/rules/flake8_comprehensions/rules/unnecessary_generator_list.rs +++ b/crates/ruff/src/rules/flake8_comprehensions/rules/unnecessary_generator_list.rs @@ -10,6 +10,23 @@ use crate::rules::flake8_comprehensions::fixes; use crate::violation::AlwaysAutofixableViolation; define_violation!( + /// ## What it does + /// Checks for unnecessary generator that can be rewritten as `list` comprehension. + /// + /// ## Why is this bad? + /// It is unnecessary to use `list` around a generator expression, since there are + /// equivalent comprehensions for these types. + /// + /// ## Examples + /// ```python + /// list(f(x) for x in foo) + /// ``` + /// + /// Use instead: + /// ```python + /// [f(x) for x in foo] + /// ``` + /// pub struct UnnecessaryGeneratorList; ); impl AlwaysAutofixableViolation for UnnecessaryGeneratorList { diff --git a/crates/ruff/src/rules/flake8_comprehensions/rules/unnecessary_generator_set.rs b/crates/ruff/src/rules/flake8_comprehensions/rules/unnecessary_generator_set.rs index 0a8a8f7ad7ecb..aa3cfdb0fd11b 100644 --- a/crates/ruff/src/rules/flake8_comprehensions/rules/unnecessary_generator_set.rs +++ b/crates/ruff/src/rules/flake8_comprehensions/rules/unnecessary_generator_set.rs @@ -10,6 +10,23 @@ use crate::rules::flake8_comprehensions::fixes; use crate::violation::AlwaysAutofixableViolation; define_violation!( + /// ## What it does + /// Checks for unnecessary generator that can be rewritten as `set` comprehension. + /// + /// ## Why is this bad? + /// It is unnecessary to use `set` around a generator expression, since there are + /// equivalent comprehensions for these types. + /// + /// ## Examples + /// ```python + /// set(f(x) for x in foo) + /// ``` + /// + /// Use instead: + /// ```python + /// {f(x) for x in foo} + /// ``` + /// pub struct UnnecessaryGeneratorSet; ); impl AlwaysAutofixableViolation for UnnecessaryGeneratorSet { @@ -27,6 +44,7 @@ impl AlwaysAutofixableViolation for UnnecessaryGeneratorSet { pub fn unnecessary_generator_set( checker: &mut Checker, expr: &Expr, + parent: Option<&Expr>, func: &Expr, args: &[Expr], keywords: &[Keyword], @@ -40,7 +58,12 @@ pub fn unnecessary_generator_set( if let ExprKind::GeneratorExp { .. } = argument { let mut diagnostic = Diagnostic::new(UnnecessaryGeneratorSet, Range::from_located(expr)); if checker.patch(diagnostic.kind.rule()) { - match fixes::fix_unnecessary_generator_set(checker.locator, checker.stylist, expr) { + match fixes::fix_unnecessary_generator_set( + checker.locator, + checker.stylist, + expr, + parent, + ) { Ok(fix) => { diagnostic.amend(fix); } diff --git a/crates/ruff/src/rules/flake8_comprehensions/rules/unnecessary_map.rs b/crates/ruff/src/rules/flake8_comprehensions/rules/unnecessary_map.rs index 456cb77b449f2..22c6e94876df5 100644 --- a/crates/ruff/src/rules/flake8_comprehensions/rules/unnecessary_map.rs +++ b/crates/ruff/src/rules/flake8_comprehensions/rules/unnecessary_map.rs @@ -110,6 +110,7 @@ pub fn unnecessary_map( checker.locator, checker.stylist, expr, + parent, "generator", ) { Ok(fix) => { @@ -141,6 +142,7 @@ pub fn unnecessary_map( checker.locator, checker.stylist, expr, + parent, id, ) { Ok(fix) => { @@ -173,6 +175,7 @@ pub fn unnecessary_map( checker.locator, checker.stylist, expr, + parent, id, ) { Ok(fix) => { diff --git a/crates/ruff/src/rules/flake8_comprehensions/snapshots/ruff__rules__flake8_comprehensions__tests__C401_C401.py.snap b/crates/ruff/src/rules/flake8_comprehensions/snapshots/ruff__rules__flake8_comprehensions__tests__C401_C401.py.snap index 857fd2c2f745d..2eb34a4d58aa2 100644 --- a/crates/ruff/src/rules/flake8_comprehensions/snapshots/ruff__rules__flake8_comprehensions__tests__C401_C401.py.snap +++ b/crates/ruff/src/rules/flake8_comprehensions/snapshots/ruff__rules__flake8_comprehensions__tests__C401_C401.py.snap @@ -1,5 +1,5 @@ --- -source: src/rules/flake8_comprehensions/mod.rs +source: crates/ruff/src/rules/flake8_comprehensions/mod.rs expression: diagnostics --- - kind: @@ -40,4 +40,58 @@ expression: diagnostics row: 4 column: 1 parent: ~ +- kind: + UnnecessaryGeneratorSet: ~ + location: + row: 5 + column: 7 + end_location: + row: 5 + column: 48 + fix: + content: + - " {a if a < 6 else 0 for a in range(3)} " + location: + row: 5 + column: 7 + end_location: + row: 5 + column: 48 + parent: ~ +- kind: + UnnecessaryGeneratorSet: ~ + location: + row: 6 + column: 16 + end_location: + row: 6 + column: 57 + fix: + content: + - "{a if a < 6 else 0 for a in range(3)}" + location: + row: 6 + column: 16 + end_location: + row: 6 + column: 57 + parent: ~ +- kind: + UnnecessaryGeneratorSet: ~ + location: + row: 7 + column: 15 + end_location: + row: 7 + column: 39 + fix: + content: + - " {a for a in range(3)} " + location: + row: 7 + column: 15 + end_location: + row: 7 + column: 39 + parent: ~ diff --git a/crates/ruff/src/rules/flake8_comprehensions/snapshots/ruff__rules__flake8_comprehensions__tests__C402_C402.py.snap b/crates/ruff/src/rules/flake8_comprehensions/snapshots/ruff__rules__flake8_comprehensions__tests__C402_C402.py.snap index 22424ba5df3ee..af2729e4967ef 100644 --- a/crates/ruff/src/rules/flake8_comprehensions/snapshots/ruff__rules__flake8_comprehensions__tests__C402_C402.py.snap +++ b/crates/ruff/src/rules/flake8_comprehensions/snapshots/ruff__rules__flake8_comprehensions__tests__C402_C402.py.snap @@ -1,5 +1,5 @@ --- -source: src/rules/flake8_comprehensions/mod.rs +source: crates/ruff/src/rules/flake8_comprehensions/mod.rs expression: diagnostics --- - kind: @@ -40,4 +40,40 @@ expression: diagnostics row: 4 column: 1 parent: ~ +- kind: + UnnecessaryGeneratorDict: ~ + location: + row: 6 + column: 7 + end_location: + row: 6 + column: 37 + fix: + content: + - " {x: x for x in range(3)} " + location: + row: 6 + column: 7 + end_location: + row: 6 + column: 37 + parent: ~ +- kind: + UnnecessaryGeneratorDict: ~ + location: + row: 7 + column: 15 + end_location: + row: 7 + column: 45 + fix: + content: + - " {x: x for x in range(3)} " + location: + row: 7 + column: 15 + end_location: + row: 7 + column: 45 + parent: ~ diff --git a/crates/ruff/src/rules/flake8_comprehensions/snapshots/ruff__rules__flake8_comprehensions__tests__C417_C417.py.snap b/crates/ruff/src/rules/flake8_comprehensions/snapshots/ruff__rules__flake8_comprehensions__tests__C417_C417.py.snap index 5ce90483c79c6..86a38f45b29a6 100644 --- a/crates/ruff/src/rules/flake8_comprehensions/snapshots/ruff__rules__flake8_comprehensions__tests__C417_C417.py.snap +++ b/crates/ruff/src/rules/flake8_comprehensions/snapshots/ruff__rules__flake8_comprehensions__tests__C417_C417.py.snap @@ -192,14 +192,52 @@ expression: diagnostics row: 12 column: 35 parent: ~ +- kind: + UnnecessaryMap: + obj_type: set + location: + row: 15 + column: 7 + end_location: + row: 15 + column: 43 + fix: + content: + - " {x % 2 == 0 for x in nums} " + location: + row: 15 + column: 7 + end_location: + row: 15 + column: 43 + parent: ~ +- kind: + UnnecessaryMap: + obj_type: dict + location: + row: 16 + column: 7 + end_location: + row: 16 + column: 43 + fix: + content: + - " {v: v**2 for v in nums} " + location: + row: 16 + column: 7 + end_location: + row: 16 + column: 43 + parent: ~ - kind: UnnecessaryMap: obj_type: generator location: - row: 17 + row: 21 column: 0 end_location: - row: 17 + row: 21 column: 24 fix: ~ parent: ~ diff --git a/docs/rules/unnecessary-generator-dict.md b/docs/rules/unnecessary-generator-dict.md new file mode 100644 index 0000000000000..e3d7d3d3a448c --- /dev/null +++ b/docs/rules/unnecessary-generator-dict.md @@ -0,0 +1,22 @@ +# unnecessary-generator-dict (C402) + +Derived from the **flake8-comprehensions** linter. + +Autofix is always available. + +## What it does +Checks for unnecessary generator that can be rewritten as `dict` comprehension. + +## Why is this bad? +It is unnecessary to use `dict` around a generator expression, since there are +equivalent comprehensions for these types. + +## Examples +```python +dict((x, f(x)) for x in foo) +``` + +Use instead: +```python +{x: f(x) for x in foo} +``` \ No newline at end of file diff --git a/docs/rules/unnecessary-generator-list.md b/docs/rules/unnecessary-generator-list.md new file mode 100644 index 0000000000000..cc0a0614ceebe --- /dev/null +++ b/docs/rules/unnecessary-generator-list.md @@ -0,0 +1,22 @@ +# unnecessary-generator-list (C400) + +Derived from the **flake8-comprehensions** linter. + +Autofix is always available. + +## What it does +Checks for unnecessary generator that can be rewritten as `list` comprehension. + +## Why is this bad? +It is unnecessary to use `list` around a generator expression, since there are +equivalent comprehensions for these types. + +## Examples +```python +list(f(x) for x in foo) +``` + +Use instead: +```python +[f(x) for x in foo] +``` \ No newline at end of file diff --git a/docs/rules/unnecessary-generator-set.md b/docs/rules/unnecessary-generator-set.md new file mode 100644 index 0000000000000..baf26da6bb581 --- /dev/null +++ b/docs/rules/unnecessary-generator-set.md @@ -0,0 +1,22 @@ +# unnecessary-generator-set (C401) + +Derived from the **flake8-comprehensions** linter. + +Autofix is always available. + +## What it does +Checks for unnecessary generator that can be rewritten as `set` comprehension. + +## Why is this bad? +It is unnecessary to use `set` around a generator expression, since there are +equivalent comprehensions for these types. + +## Examples +```python +set(f(x) for x in foo) +``` + +Use instead: +```python +{f(x) for x in foo} +``` \ No newline at end of file From ff68c82d474f852d873b7f9592ec3257cfa9577b Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sun, 12 Feb 2023 11:00:16 -0500 Subject: [PATCH 2/2] Tweak docs --- crates/ruff/src/rules/flake8_comprehensions/fixes.rs | 9 ++++++--- .../rules/unnecessary_generator_dict.rs | 9 +++++---- .../rules/unnecessary_generator_list.rs | 9 +++++---- .../rules/unnecessary_generator_set.rs | 9 +++++---- docs/rules/unnecessary-generator-dict.md | 8 +++++--- docs/rules/unnecessary-generator-list.md | 8 +++++--- docs/rules/unnecessary-generator-set.md | 8 +++++--- 7 files changed, 36 insertions(+), 24 deletions(-) diff --git a/crates/ruff/src/rules/flake8_comprehensions/fixes.rs b/crates/ruff/src/rules/flake8_comprehensions/fixes.rs index ae52d6fcc9720..b115986aac124 100644 --- a/crates/ruff/src/rules/flake8_comprehensions/fixes.rs +++ b/crates/ruff/src/rules/flake8_comprehensions/fixes.rs @@ -116,7 +116,8 @@ pub fn fix_unnecessary_generator_set( let mut content = state.to_string(); - // If parent is f-string then surround with spaces + // If the expression is embedded in an f-string, surround it with spaces to avoid + // syntax errors. if let Some(parent_element) = parent { if let &rustpython_parser::ast::ExprKind::FormattedValue { .. } = &parent_element.node { content = format!(" {content} "); @@ -189,7 +190,8 @@ pub fn fix_unnecessary_generator_dict( let mut content = state.to_string(); - // If parent is f-string then surround with spaces + // If the expression is embedded in an f-string, surround it with spaces to avoid + // syntax errors. if let Some(parent_element) = parent { if let &rustpython_parser::ast::ExprKind::FormattedValue { .. } = &parent_element.node { content = format!(" {content} "); @@ -1084,7 +1086,8 @@ pub fn fix_unnecessary_map( let mut content = state.to_string(); - // If parent is f-string then surround with spaces + // If the expression is embedded in an f-string, surround it with spaces to avoid + // syntax errors. if kind == "set" || kind == "dict" { if let Some(parent_element) = parent { if let &rustpython_parser::ast::ExprKind::FormattedValue { .. } = diff --git a/crates/ruff/src/rules/flake8_comprehensions/rules/unnecessary_generator_dict.rs b/crates/ruff/src/rules/flake8_comprehensions/rules/unnecessary_generator_dict.rs index 771ca456771e5..46322bac26ec4 100644 --- a/crates/ruff/src/rules/flake8_comprehensions/rules/unnecessary_generator_dict.rs +++ b/crates/ruff/src/rules/flake8_comprehensions/rules/unnecessary_generator_dict.rs @@ -11,11 +11,13 @@ use crate::violation::AlwaysAutofixableViolation; define_violation!( /// ## What it does - /// Checks for unnecessary generator that can be rewritten as `dict` comprehension. + /// Checks for unnecessary generators that can be rewritten as `dict` + /// comprehensions. /// /// ## Why is this bad? - /// It is unnecessary to use `dict` around a generator expression, since there are - /// equivalent comprehensions for these types. + /// It is unnecessary to use `dict` around a generator expression, since + /// there are equivalent comprehensions for these types. Using a + /// comprehension is clearer and more idiomatic. /// /// ## Examples /// ```python @@ -26,7 +28,6 @@ define_violation!( /// ```python /// {x: f(x) for x in foo} /// ``` - /// pub struct UnnecessaryGeneratorDict; ); impl AlwaysAutofixableViolation for UnnecessaryGeneratorDict { diff --git a/crates/ruff/src/rules/flake8_comprehensions/rules/unnecessary_generator_list.rs b/crates/ruff/src/rules/flake8_comprehensions/rules/unnecessary_generator_list.rs index 4950c064464c9..a9defc36165fc 100644 --- a/crates/ruff/src/rules/flake8_comprehensions/rules/unnecessary_generator_list.rs +++ b/crates/ruff/src/rules/flake8_comprehensions/rules/unnecessary_generator_list.rs @@ -11,11 +11,13 @@ use crate::violation::AlwaysAutofixableViolation; define_violation!( /// ## What it does - /// Checks for unnecessary generator that can be rewritten as `list` comprehension. + /// Checks for unnecessary generators that can be rewritten as `list` + /// comprehensions. /// /// ## Why is this bad? - /// It is unnecessary to use `list` around a generator expression, since there are - /// equivalent comprehensions for these types. + /// It is unnecessary to use `list` around a generator expression, since + /// there are equivalent comprehensions for these types. Using a + /// comprehension is clearer and more idiomatic. /// /// ## Examples /// ```python @@ -26,7 +28,6 @@ define_violation!( /// ```python /// [f(x) for x in foo] /// ``` - /// pub struct UnnecessaryGeneratorList; ); impl AlwaysAutofixableViolation for UnnecessaryGeneratorList { diff --git a/crates/ruff/src/rules/flake8_comprehensions/rules/unnecessary_generator_set.rs b/crates/ruff/src/rules/flake8_comprehensions/rules/unnecessary_generator_set.rs index aa3cfdb0fd11b..bb9236457f882 100644 --- a/crates/ruff/src/rules/flake8_comprehensions/rules/unnecessary_generator_set.rs +++ b/crates/ruff/src/rules/flake8_comprehensions/rules/unnecessary_generator_set.rs @@ -11,11 +11,13 @@ use crate::violation::AlwaysAutofixableViolation; define_violation!( /// ## What it does - /// Checks for unnecessary generator that can be rewritten as `set` comprehension. + /// Checks for unnecessary generators that can be rewritten as `set` + /// comprehensions. /// /// ## Why is this bad? - /// It is unnecessary to use `set` around a generator expression, since there are - /// equivalent comprehensions for these types. + /// It is unnecessary to use `set` around a generator expression, since + /// there are equivalent comprehensions for these types. Using a + /// comprehension is clearer and more idiomatic. /// /// ## Examples /// ```python @@ -26,7 +28,6 @@ define_violation!( /// ```python /// {f(x) for x in foo} /// ``` - /// pub struct UnnecessaryGeneratorSet; ); impl AlwaysAutofixableViolation for UnnecessaryGeneratorSet { diff --git a/docs/rules/unnecessary-generator-dict.md b/docs/rules/unnecessary-generator-dict.md index e3d7d3d3a448c..c8220429cddaa 100644 --- a/docs/rules/unnecessary-generator-dict.md +++ b/docs/rules/unnecessary-generator-dict.md @@ -5,11 +5,13 @@ Derived from the **flake8-comprehensions** linter. Autofix is always available. ## What it does -Checks for unnecessary generator that can be rewritten as `dict` comprehension. +Checks for unnecessary generators that can be rewritten as `dict` +comprehensions. ## Why is this bad? -It is unnecessary to use `dict` around a generator expression, since there are -equivalent comprehensions for these types. +It is unnecessary to use `dict` around a generator expression, since +there are equivalent comprehensions for these types. Using a +comprehension is clearer and more idiomatic. ## Examples ```python diff --git a/docs/rules/unnecessary-generator-list.md b/docs/rules/unnecessary-generator-list.md index cc0a0614ceebe..df595115eb915 100644 --- a/docs/rules/unnecessary-generator-list.md +++ b/docs/rules/unnecessary-generator-list.md @@ -5,11 +5,13 @@ Derived from the **flake8-comprehensions** linter. Autofix is always available. ## What it does -Checks for unnecessary generator that can be rewritten as `list` comprehension. +Checks for unnecessary generators that can be rewritten as `list` +comprehensions. ## Why is this bad? -It is unnecessary to use `list` around a generator expression, since there are -equivalent comprehensions for these types. +It is unnecessary to use `list` around a generator expression, since +there are equivalent comprehensions for these types. Using a +comprehension is clearer and more idiomatic. ## Examples ```python diff --git a/docs/rules/unnecessary-generator-set.md b/docs/rules/unnecessary-generator-set.md index baf26da6bb581..ea5bc2c593b7d 100644 --- a/docs/rules/unnecessary-generator-set.md +++ b/docs/rules/unnecessary-generator-set.md @@ -5,11 +5,13 @@ Derived from the **flake8-comprehensions** linter. Autofix is always available. ## What it does -Checks for unnecessary generator that can be rewritten as `set` comprehension. +Checks for unnecessary generators that can be rewritten as `set` +comprehensions. ## Why is this bad? -It is unnecessary to use `set` around a generator expression, since there are -equivalent comprehensions for these types. +It is unnecessary to use `set` around a generator expression, since +there are equivalent comprehensions for these types. Using a +comprehension is clearer and more idiomatic. ## Examples ```python