Skip to content

Commit

Permalink
Implement C404
Browse files Browse the repository at this point in the history
  • Loading branch information
harupy committed Oct 7, 2022
1 parent 4eac7a0 commit 007382c
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ The 🛠 emoji indicates that a rule is automatically fixable by the `--fix` com
| A002 | BuiltinArgumentShadowing | Argument `...` is shadowing a python builtin | | |
| A003 | BuiltinAttributeShadowing | Class attribute `...` is shadowing a python builtin | | |
| C403 | UnnecessaryListComprehensionSet | Unnecessary list comprehension - rewrite as a set comprehension | | |
| C404 | UnnecessaryListComprehensionDict | Unnecessary list comprehension - rewrite as a dict comprehension | | |
| SPR001 | SuperCallWithParameters | Use `super()` instead of `super(__class__, self)` | | 🛠 |
| T201 | PrintFound | `print` found | | 🛠 |
| T203 | PPrintFound | `pprint` found | | 🛠 |
Expand Down
1 change: 1 addition & 0 deletions resources/test/fixtures/C404.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
d = dict([(i, i) for i in range(3)])
26 changes: 20 additions & 6 deletions src/ast/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,12 +695,26 @@ pub fn is_super_call_with_arguments(func: &Expr, args: &Vec<Expr>) -> bool {
// flakes8-comprehensions
pub fn unnecessary_list_comprehension(expr: &Expr, func: &Expr, args: &Vec<Expr>) -> Option<Check> {
if let ExprKind::Name { id, .. } = &func.node {
if id == "set" && args.len() == 1 {
if let ExprKind::ListComp { .. } = &args[0].node {
return Some(Check::new(
CheckKind::UnnecessaryListComprehensionSet,
Range::from_located(expr),
));
if (id == "set" || id == "dict") && args.len() == 1 {
if let ExprKind::ListComp { elt, .. } = &args[0].node {
if id == "set" {
return Some(Check::new(
CheckKind::UnnecessaryListComprehensionSet,
Range::from_located(expr),
));
}

if id == "dict" {
match &elt.node {
ExprKind::Tuple { elts, .. } if elts.len() == 2 => {
return Some(Check::new(
CheckKind::UnnecessaryListComprehensionDict,
Range::from_located(expr),
));
}
_ => {}
}
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/check_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,9 @@ where
}

// flake8-comprehensions
if self.settings.enabled.contains(&CheckCode::C403) {
if self.settings.enabled.contains(&CheckCode::C403)
|| self.settings.enabled.contains(&CheckCode::C404)
{
if let Some(check) = checks::unnecessary_list_comprehension(expr, func, args) {
self.checks.push(check);
};
Expand Down
13 changes: 12 additions & 1 deletion src/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub const DEFAULT_CHECK_CODES: [CheckCode; 42] = [
CheckCode::F901,
];

pub const ALL_CHECK_CODES: [CheckCode; 53] = [
pub const ALL_CHECK_CODES: [CheckCode; 54] = [
// pycodestyle
CheckCode::E402,
CheckCode::E501,
Expand Down Expand Up @@ -104,6 +104,7 @@ pub const ALL_CHECK_CODES: [CheckCode; 53] = [
CheckCode::A003,
// flake8-comprehensions
CheckCode::C403,
CheckCode::C404,
// flake8-super
CheckCode::SPR001,
// flake8-print
Expand Down Expand Up @@ -170,6 +171,7 @@ pub enum CheckCode {
A003,
// flake8-comprehensions
C403,
C404,
// flake8-super
SPR001,
// flake8-print
Expand Down Expand Up @@ -239,6 +241,7 @@ impl FromStr for CheckCode {
"A003" => Ok(CheckCode::A003),
// flake8-comprehensions
"C403" => Ok(CheckCode::C403),
"C404" => Ok(CheckCode::C404),
// flake8-super
"SPR001" => Ok(CheckCode::SPR001),
// flake8-print
Expand Down Expand Up @@ -309,6 +312,7 @@ impl CheckCode {
CheckCode::A003 => "A003",
// flake8-comprehensions
CheckCode::C403 => "C403",
CheckCode::C404 => "C404",
// flake8-super
CheckCode::SPR001 => "SPR001",
// flake8-print
Expand Down Expand Up @@ -388,6 +392,7 @@ impl CheckCode {
CheckCode::A003 => CheckKind::BuiltinAttributeShadowing("...".to_string()),
// flake8-comprehensions
CheckCode::C403 => CheckKind::UnnecessaryListComprehensionSet,
CheckCode::C404 => CheckKind::UnnecessaryListComprehensionDict,
// flake8-super
CheckCode::SPR001 => CheckKind::SuperCallWithParameters,
// flake8-print
Expand Down Expand Up @@ -471,6 +476,7 @@ pub enum CheckKind {
BuiltinAttributeShadowing(String),
// flakes8-comprehensions
UnnecessaryListComprehensionSet,
UnnecessaryListComprehensionDict,
// flake8-super
SuperCallWithParameters,
// flake8-print
Expand Down Expand Up @@ -530,6 +536,7 @@ impl CheckKind {
CheckKind::BuiltinAttributeShadowing(_) => "BuiltinAttributeShadowing",
// flake8-comprehensions
CheckKind::UnnecessaryListComprehensionSet => "UnnecessaryListComprehensionSet",
CheckKind::UnnecessaryListComprehensionDict => "UnnecessaryListComprehensionDict",
// flake8-super
CheckKind::SuperCallWithParameters => "SuperCallWithParameters",
// flake8-print
Expand Down Expand Up @@ -596,6 +603,7 @@ impl CheckKind {
CheckKind::BuiltinAttributeShadowing(_) => &CheckCode::A003,
// flake8-comprehensions
CheckKind::UnnecessaryListComprehensionSet => &CheckCode::C403,
CheckKind::UnnecessaryListComprehensionDict => &CheckCode::C404,
// flake8-super
CheckKind::SuperCallWithParameters => &CheckCode::SPR001,
// flake8-print
Expand Down Expand Up @@ -753,6 +761,9 @@ impl CheckKind {
CheckKind::UnnecessaryListComprehensionSet => {
"Unnecessary list comprehension - rewrite as a set comprehension".to_string()
}
CheckKind::UnnecessaryListComprehensionDict => {
"Unnecessary list comprehension - rewrite as a dict comprehension".to_string()
}
// flake8-super
CheckKind::SuperCallWithParameters => {
"Use `super()` instead of `super(__class__, self)`".to_string()
Expand Down
12 changes: 12 additions & 0 deletions src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,18 @@ mod tests {
Ok(())
}

#[test]
fn c404() -> Result<()> {
let mut checks = check_path(
Path::new("./resources/test/fixtures/C404.py"),
&settings::Settings::for_rule(CheckCode::C404),
&fixer::Mode::Generate,
)?;
checks.sort_by_key(|check| check.location);
insta::assert_yaml_snapshot!(checks);
Ok(())
}

#[test]
fn spr001() -> Result<()> {
let mut checks = check_path(
Expand Down
13 changes: 13 additions & 0 deletions src/snapshots/ruff__linter__tests__c404.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
source: src/linter.rs
expression: checks
---
- kind: UnnecessaryListComprehensionDict
location:
row: 1
column: 5
end_location:
row: 1
column: 37
fix: ~

0 comments on commit 007382c

Please # to comment.