diff --git a/crates/ruff_linter/src/rules/refurb/rules/for_loop_set_mutations.rs b/crates/ruff_linter/src/rules/refurb/rules/for_loop_set_mutations.rs index 4b9646c774fbed..b019c6bd3c6a50 100644 --- a/crates/ruff_linter/src/rules/refurb/rules/for_loop_set_mutations.rs +++ b/crates/ruff_linter/src/rules/refurb/rules/for_loop_set_mutations.rs @@ -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 @@ -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) } } diff --git a/crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB142_FURB142.py.snap b/crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB142_FURB142.py.snap index e385a739b28358..ddb9fe52b64b67 100644 --- a/crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB142_FURB142.py.snap +++ b/crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB142_FURB142.py.snap @@ -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 | @@ -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 | @@ -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 | @@ -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]: @@ -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 | @@ -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}: @@ -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 | @@ -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): @@ -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 | @@ -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): @@ -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 | @@ -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): @@ -139,7 +139,7 @@ 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 | @@ -147,7 +147,7 @@ FURB142.py:25:1: FURB142 [*] Use of set.add in a for loop 26 | | s.add(num) | |______________^ FURB142 | - = help: Replace with `update` + = help: Replace with `.update()` ℹ Safe fix 22 22 |