forked from astral-sh/ruff
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement SIM118 (key in dict) of flake8-simplify
Refs: astral-sh#998
- Loading branch information
Showing
11 changed files
with
278 additions
and
1 deletion.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
key in dict.keys() # SIM118 | ||
|
||
foo["bar"] in dict.keys() # SIM118 | ||
|
||
foo() in dict.keys() # SIM118 | ||
|
||
for key in dict.keys(): # SIM118 | ||
pass | ||
|
||
for key in list(dict.keys()): | ||
if some_property(key): | ||
del dict[key] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
pub mod plugins; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::convert::AsRef; | ||
use std::path::Path; | ||
|
||
use anyhow::Result; | ||
use test_case::test_case; | ||
|
||
use crate::checks::CheckCode; | ||
use crate::linter::test_path; | ||
use crate::settings; | ||
|
||
#[test_case(CheckCode::SIM118, Path::new("SIM118.py"); "SIM118")] | ||
fn checks(check_code: CheckCode, path: &Path) -> Result<()> { | ||
let snapshot = format!("{}_{}", check_code.as_ref(), path.to_string_lossy()); | ||
let mut checks = test_path( | ||
Path::new("./resources/test/fixtures/flake8_simplify") | ||
.join(path) | ||
.as_path(), | ||
&settings::Settings::for_rule(check_code), | ||
true, | ||
)?; | ||
checks.sort_by_key(|check| check.location); | ||
insta::assert_yaml_snapshot!(snapshot, checks); | ||
Ok(()) | ||
} | ||
} |
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,74 @@ | ||
use rustpython_ast::{Cmpop, Expr, ExprKind}; | ||
|
||
use crate::ast::types::Range; | ||
use crate::autofix::Fix; | ||
use crate::check_ast::Checker; | ||
use crate::checks::{Check, CheckCode, CheckKind}; | ||
|
||
/// SIM118 | ||
fn key_in_dict(checker: &mut Checker, left: &Expr, right: &Expr, range: Range) { | ||
let ExprKind::Call { | ||
func, | ||
args, | ||
keywords, | ||
} = &right.node else { | ||
return; | ||
}; | ||
if !(args.is_empty() && keywords.is_empty()) { | ||
return; | ||
} | ||
|
||
let ExprKind::Attribute { attr, value, .. } = &func.node else { | ||
return; | ||
}; | ||
if attr != "keys" { | ||
return; | ||
} | ||
|
||
let mut check = Check::new( | ||
CheckKind::KeyInDict(left.to_string(), value.to_string()), | ||
range, | ||
); | ||
if checker.patch(&CheckCode::SIM118) { | ||
let content = right.to_string().replace(".keys()", ""); | ||
check.amend(Fix::replacement( | ||
content, | ||
right.location, | ||
right.end_location.unwrap(), | ||
)); | ||
} | ||
checker.add_check(check); | ||
} | ||
|
||
/// SIM118 in a for loop | ||
pub fn key_in_dict_for(checker: &mut Checker, target: &Expr, iter: &Expr) { | ||
key_in_dict( | ||
checker, | ||
target, | ||
iter, | ||
Range { | ||
location: target.location, | ||
end_location: iter.end_location.unwrap(), | ||
}, | ||
); | ||
} | ||
|
||
/// SIM118 in a comparison | ||
pub fn key_in_dict_compare( | ||
checker: &mut Checker, | ||
expr: &Expr, | ||
left: &Expr, | ||
ops: &[Cmpop], | ||
comparators: &[Expr], | ||
) { | ||
if !matches!(ops[..], [Cmpop::In]) { | ||
return; | ||
} | ||
|
||
if comparators.len() != 1 { | ||
return; | ||
} | ||
let right = comparators.first().unwrap(); | ||
|
||
key_in_dict(checker, left, right, Range::from_located(expr)); | ||
} |
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,3 @@ | ||
pub use key_in_dict::{key_in_dict_compare, key_in_dict_for}; | ||
|
||
mod key_in_dict; |
Oops, something went wrong.