Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change iteration-over-set to flag set literals only #4907

Merged
merged 3 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions crates/ruff/resources/test/fixtures/pylint/iteration_over_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
for item in {"apples", "lemons", "water"}: # flags in-line set literals
print(f"I like {item}.")

for item in set(("apples", "lemons", "water")): # flags set() calls
print(f"I like {item}.")

for number in {i for i in range(10)}: # flags set comprehensions
print(number)

numbers_list = [i for i in {1, 2, 3}] # flags sets in list comprehensions

numbers_set = {i for i in {1, 2, 3}} # flags sets in set comprehensions
Expand Down Expand Up @@ -36,3 +30,9 @@
numbers_dict = {str(i): i for i in [1, 2, 3]} # lists in dict comprehensions are fine

numbers_gen = (i for i in (1, 2, 3)) # tuples in generator expressions are fine

for item in set(("apples", "lemons", "water")): # set constructor is fine
print(f"I like {item}.")

for number in {i for i in range(10)}: # set comprehensions are fine
print(number)
20 changes: 2 additions & 18 deletions crates/ruff/src/rules/pylint/rules/iteration_over_set.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rustpython_parser::ast::{Expr, ExprName, Ranged};
use rustpython_parser::ast::{Expr, Ranged};

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
Expand Down Expand Up @@ -38,23 +38,7 @@ impl Violation for IterationOverSet {

/// PLC0208
pub(crate) fn iteration_over_set(checker: &mut Checker, expr: &Expr) {
let is_set = match expr {
// Ex) `for i in {1, 2, 3}`
Expr::Set(_) => true,
// Ex)` for i in {n for n in range(1, 4)}`
Expr::SetComp(_) => true,
// Ex) `for i in set(1, 2, 3)`
Expr::Call(call) => {
if let Expr::Name(ExprName { id, .. }) = call.func.as_ref() {
id.as_str() == "set" && checker.semantic_model().is_builtin("set")
} else {
false
}
}
_ => false,
};

if is_set {
if let Expr::Set(_) = expr {
tjkuson marked this conversation as resolved.
Show resolved Hide resolved
checker
.diagnostics
.push(Diagnostic::new(IterationOverSet, expr.range()));
Expand Down