Skip to content

Commit

Permalink
Small touch-ups
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood committed Mar 26, 2024
1 parent 05a2132 commit b160eee
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ use ruff_python_semantic::analyze::typing;
use crate::checkers::ast::Checker;

/// ## What it does
/// Checks for updating list with iterable object by calling `add`/`discard` on each element
/// separately in the `for` loop.
/// Checks for code that updates a set with the contents of an iterable by
/// using a `for` loop to call `.add()` or `.discard()` on each element
/// separately.
///
/// ## Why is this bad?
/// When adding/removing a batch of elements to/from a set, it's more readable to call
/// an appropriate method, instead of add/remove elements one-by-one.
/// When adding or removing a batch of elements to or from a set, it's more
/// idiomatic to use a single method call rather than adding or removing
/// elements one by one.
///
/// ## Example
/// ```python
Expand Down Expand Up @@ -43,11 +45,11 @@ pub struct ForLoopSetMutations {
impl AlwaysFixableViolation for ForLoopSetMutations {
#[derive_message_formats]
fn message(&self) -> String {
format!("Use of set.{} in a for loop", self.method_name)
format!("Use of `set.{}()` in a for loop", self.method_name)
}

fn fix_title(&self) -> String {
format!("Replace with `{}`", self.batch_method_name)
format!("Replace with `.{}()`", self.batch_method_name)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
source: crates/ruff_linter/src/rules/refurb/mod.rs
---
FURB142.py:5:1: FURB142 [*] Use of set.add in a for loop
FURB142.py:5:1: FURB142 [*] Use of `set.add()` in a for loop
|
3 | s = set()
4 |
Expand All @@ -11,7 +11,7 @@ FURB142.py:5:1: FURB142 [*] Use of set.add in a for loop
7 |
8 | for x in {1, 2, 3}:
|
= help: Replace with `update`
= help: Replace with `.update()`

Safe fix
2 2 |
Expand All @@ -24,7 +24,7 @@ FURB142.py:5:1: FURB142 [*] Use of set.add in a for loop
8 7 | for x in {1, 2, 3}:
9 8 | s.add(x)

FURB142.py:8:1: FURB142 [*] Use of set.add in a for loop
FURB142.py:8:1: FURB142 [*] Use of `set.add()` in a for loop
|
6 | s.add(x)
7 |
Expand All @@ -34,7 +34,7 @@ FURB142.py:8:1: FURB142 [*] Use of set.add in a for loop
10 |
11 | for x in (1, 2, 3):
|
= help: Replace with `update`
= help: Replace with `.update()`

Safe fix
5 5 | for x in [1, 2, 3]:
Expand All @@ -47,7 +47,7 @@ FURB142.py:8:1: FURB142 [*] Use of set.add in a for loop
11 10 | for x in (1, 2, 3):
12 11 | s.add(x)

FURB142.py:11:1: FURB142 [*] Use of set.add in a for loop
FURB142.py:11:1: FURB142 [*] Use of `set.add()` in a for loop
|
9 | s.add(x)
10 |
Expand All @@ -57,7 +57,7 @@ FURB142.py:11:1: FURB142 [*] Use of set.add in a for loop
13 |
14 | for x in (1, 2, 3):
|
= help: Replace with `update`
= help: Replace with `.update()`

Safe fix
8 8 | for x in {1, 2, 3}:
Expand All @@ -70,7 +70,7 @@ FURB142.py:11:1: FURB142 [*] Use of set.add in a for loop
14 13 | for x in (1, 2, 3):
15 14 | s.discard(x)

FURB142.py:14:1: FURB142 [*] Use of set.discard in a for loop
FURB142.py:14:1: FURB142 [*] Use of `set.discard()` in a for loop
|
12 | s.add(x)
13 |
Expand All @@ -80,7 +80,7 @@ FURB142.py:14:1: FURB142 [*] Use of set.discard in a for loop
16 |
17 | for x in (1, 2, 3):
|
= help: Replace with `difference_update`
= help: Replace with `.difference_update()`

Safe fix
11 11 | for x in (1, 2, 3):
Expand All @@ -93,7 +93,7 @@ FURB142.py:14:1: FURB142 [*] Use of set.discard in a for loop
17 16 | for x in (1, 2, 3):
18 17 | s.add(x + 1)

FURB142.py:17:1: FURB142 [*] Use of set.add in a for loop
FURB142.py:17:1: FURB142 [*] Use of `set.add()` in a for loop
|
15 | s.discard(x)
16 |
Expand All @@ -103,7 +103,7 @@ FURB142.py:17:1: FURB142 [*] Use of set.add in a for loop
19 |
20 | for x, y in ((1, 2), (3, 4)):
|
= help: Replace with `update`
= help: Replace with `.update()`

Safe fix
14 14 | for x in (1, 2, 3):
Expand All @@ -116,7 +116,7 @@ FURB142.py:17:1: FURB142 [*] Use of set.add in a for loop
20 19 | for x, y in ((1, 2), (3, 4)):
21 20 | s.add((x, y))

FURB142.py:20:1: FURB142 [*] Use of set.add in a for loop
FURB142.py:20:1: FURB142 [*] Use of `set.add()` in a for loop
|
18 | s.add(x + 1)
19 |
Expand All @@ -126,7 +126,7 @@ FURB142.py:20:1: FURB142 [*] Use of set.add in a for loop
22 |
23 | num = 123
|
= help: Replace with `update`
= help: Replace with `.update()`

Safe fix
17 17 | for x in (1, 2, 3):
Expand All @@ -139,15 +139,15 @@ FURB142.py:20:1: FURB142 [*] Use of set.add in a for loop
23 22 | num = 123
24 23 |

FURB142.py:25:1: FURB142 [*] Use of set.add in a for loop
FURB142.py:25:1: FURB142 [*] Use of `set.add()` in a for loop
|
23 | num = 123
24 |
25 | / for x in (1, 2, 3):
26 | | s.add(num)
| |______________^ FURB142
|
= help: Replace with `update`
= help: Replace with `.update()`

Safe fix
22 22 |
Expand Down

0 comments on commit b160eee

Please sign in to comment.