-
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
Showing
11 changed files
with
278 additions
and
73 deletions.
There are no files selected for viewing
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
10 changes: 10 additions & 0 deletions
10
crates/ruff_linter/resources/test/fixtures/pylint/literal_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,10 @@ | ||
# Errors | ||
1 in [1, 2, 3] | ||
1 in (1, 2, 3) | ||
1 in ( | ||
1, 2, 3 | ||
) | ||
|
||
# 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
65 changes: 65 additions & 0 deletions
65
crates/ruff_linter/src/rules/pylint/rules/literal_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,65 @@ | ||
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; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for membership tests on `list` and `tuple` literals. | ||
/// | ||
/// ## Why is this bad? | ||
/// When testing for membership in a static sequence, prefer a `set` literal | ||
/// over a `list` or `tuple`, as Python optimizes `set` membership tests. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// 1 in [1, 2, 3] | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// 1 in {1, 2, 3} | ||
/// ``` | ||
/// ## References | ||
/// - [What’s New In Python 3.2](https://docs.python.org/3/whatsnew/3.2.html#optimizations) | ||
#[violation] | ||
pub struct LiteralMembership; | ||
|
||
impl AlwaysFixableViolation for LiteralMembership { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Use a `set` literal when testing for membership") | ||
} | ||
|
||
fn fix_title(&self) -> String { | ||
format!("Convert to `set`") | ||
} | ||
} | ||
|
||
/// PLR6201 | ||
pub(crate) fn literal_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; | ||
}; | ||
|
||
if !matches!(right, Expr::List(_) | Expr::Tuple(_)) { | ||
return; | ||
} | ||
|
||
let mut diagnostic = Diagnostic::new(LiteralMembership, right.range()); | ||
|
||
let literal = checker.locator().slice(right); | ||
let set = format!("{{{}}}", &literal[1..literal.len() - 1]); | ||
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(set, right.range()))); | ||
|
||
checker.diagnostics.push(diagnostic); | ||
} |
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
Oops, something went wrong.