-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aa6846c
commit b07a1cb
Showing
8 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
crates/ruff_linter/resources/test/fixtures/pylint/set_membership.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# PLR6201 | ||
1 in [1, 2, 3] | ||
|
||
# PLR6201 | ||
1 in (1, 2, 3) | ||
|
||
# PLR6201 | ||
def fruit_is_dangerous_for_cat(fruit: str) -> bool: | ||
return fruit in ["cherry", "grapes"] | ||
|
||
# Ok | ||
fruits = ["cherry", "grapes"] | ||
"cherry" in fruits |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
crates/ruff_linter/src/rules/pylint/rules/set_membership.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::{self as ast, CmpOp, Expr}; | ||
use ruff_text_size::{Ranged, TextRange}; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for operations that checks a list or tuple for an element. | ||
/// | ||
/// ## Why is this bad? | ||
/// Membership tests are more efficient when performed on a | ||
/// lookup-optimized datatype like `set`. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// 1 in [1, 2, 3] | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// 1 in {1, 2, 3} | ||
/// ``` | ||
/// ## References | ||
/// - [Python 3.2 release notes](https://docs.python.org/3/whatsnew/3.2.html#optimizations) | ||
#[violation] | ||
pub struct SetMembership; | ||
|
||
impl AlwaysFixableViolation for SetMembership { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Use a `set` when checking for element membership") | ||
} | ||
|
||
fn fix_title(&self) -> String { | ||
format!("Use a `set` when checking for element membership") | ||
} | ||
} | ||
|
||
/// PLR6201 | ||
pub(crate) fn set_membership(checker: &mut Checker, compare: &ast::ExprCompare) { | ||
let [op] = compare.ops.as_slice() else { | ||
return; | ||
}; | ||
|
||
if !matches!(op, CmpOp::In | CmpOp::NotIn) { | ||
return; | ||
} | ||
|
||
let [right] = compare.comparators.as_slice() else { | ||
return; | ||
}; | ||
|
||
let (Expr::List(ast::ExprList { | ||
elts: right_elements, | ||
.. | ||
}) | ||
| Expr::Tuple(ast::ExprTuple { | ||
elts: right_elements, | ||
.. | ||
})) = right | ||
else { | ||
return; | ||
}; | ||
|
||
let mut diagnostic = Diagnostic::new(SetMembership, right.range()); | ||
|
||
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement( | ||
checker.generator().expr(&Expr::Set(ast::ExprSet { | ||
elts: right_elements.clone(), | ||
range: TextRange::default(), | ||
})), | ||
right.range(), | ||
))); | ||
|
||
checker.diagnostics.push(diagnostic); | ||
} |
63 changes: 63 additions & 0 deletions
63
.../rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLR6201_set_membership.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pylint/mod.rs | ||
--- | ||
set_membership.py:2:6: PLR6201 [*] Use a `set` when checking for element membership | ||
| | ||
1 | # PLR6201 | ||
2 | 1 in [1, 2, 3] | ||
| ^^^^^^^^^ PLR6201 | ||
3 | | ||
4 | # PLR6201 | ||
| | ||
= help: Use a `set` when checking for element membership | ||
|
||
ℹ Fix | ||
1 1 | # PLR6201 | ||
2 |-1 in [1, 2, 3] | ||
2 |+1 in {1, 2, 3} | ||
3 3 | | ||
4 4 | # PLR6201 | ||
5 5 | 1 in (1, 2, 3) | ||
|
||
set_membership.py:5:6: PLR6201 [*] Use a `set` when checking for element membership | ||
| | ||
4 | # PLR6201 | ||
5 | 1 in (1, 2, 3) | ||
| ^^^^^^^^^ PLR6201 | ||
6 | | ||
7 | # PLR6201 | ||
| | ||
= help: Use a `set` when checking for element membership | ||
|
||
ℹ Fix | ||
2 2 | 1 in [1, 2, 3] | ||
3 3 | | ||
4 4 | # PLR6201 | ||
5 |-1 in (1, 2, 3) | ||
5 |+1 in {1, 2, 3} | ||
6 6 | | ||
7 7 | # PLR6201 | ||
8 8 | def fruit_is_dangerous_for_cat(fruit: str) -> bool: | ||
|
||
set_membership.py:9:21: PLR6201 [*] Use a `set` when checking for element membership | ||
| | ||
7 | # PLR6201 | ||
8 | def fruit_is_dangerous_for_cat(fruit: str) -> bool: | ||
9 | return fruit in ["cherry", "grapes"] | ||
| ^^^^^^^^^^^^^^^^^^^^ PLR6201 | ||
10 | | ||
11 | # Ok | ||
| | ||
= help: Use a `set` when checking for element membership | ||
|
||
ℹ Fix | ||
6 6 | | ||
7 7 | # PLR6201 | ||
8 8 | def fruit_is_dangerous_for_cat(fruit: str) -> bool: | ||
9 |- return fruit in ["cherry", "grapes"] | ||
9 |+ return fruit in {"cherry", "grapes"} | ||
10 10 | | ||
11 11 | # Ok | ||
12 12 | fruits = ["cherry", "grapes"] | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.