Implement C417 (#426)

This commit is contained in:
Harutaka Kawamura
2022-10-15 01:34:00 +09:00
committed by GitHub
parent 952a0eb4e3
commit b64040cbb2
7 changed files with 161 additions and 1 deletions

View File

@@ -1175,6 +1175,64 @@ pub fn unnecessary_comprehension(
None
}
pub fn unnecessary_map(expr: &Expr, func: &Expr, args: &[Expr]) -> Option<Check> {
if let ExprKind::Name { id, .. } = &func.node {
if id == "map" {
if args.len() == 2 {
if let ExprKind::Lambda { .. } = &args[0].node {
return Some(Check::new(
CheckKind::UnnecessaryMap("generator".to_string()),
Range::from_located(expr),
));
}
}
} else if id == "list" || id == "set" {
if let Some(arg) = args.first() {
if let ExprKind::Call { func, args, .. } = &arg.node {
if let ExprKind::Name { id: f, .. } = &func.node {
if f == "map" {
if let Some(arg) = args.first() {
if let ExprKind::Lambda { .. } = &arg.node {
return Some(Check::new(
CheckKind::UnnecessaryMap(id.to_string()),
Range::from_located(expr),
));
}
}
}
}
}
}
} else if id == "dict" {
if args.len() == 1 {
if let ExprKind::Call { func, args, .. } = &args[0].node {
if let ExprKind::Name { id: f, .. } = &func.node {
if f == "map" {
if let Some(arg) = args.first() {
if let ExprKind::Lambda { body, .. } = &arg.node {
match &body.node {
ExprKind::Tuple { elts, .. }
| ExprKind::List { elts, .. }
if elts.len() == 2 =>
{
return Some(Check::new(
CheckKind::UnnecessaryMap(id.to_string()),
Range::from_located(expr),
))
}
_ => {}
}
}
}
}
}
}
}
}
}
None
}
// flake8-super
/// Check that `super()` has no args
pub fn super_args(

View File

@@ -865,6 +865,12 @@ where
};
}
if self.settings.enabled.contains(&CheckCode::C417) {
if let Some(check) = checkers::unnecessary_map(expr, func, args) {
self.checks.push(check);
};
}
// pyupgrade
if self.settings.enabled.contains(&CheckCode::U002)
&& self.settings.target_version >= PythonVersion::Py310

View File

@@ -142,6 +142,7 @@ pub enum CheckCode {
C414,
C415,
C416,
C417,
// flake8-print
T201,
T203,
@@ -284,6 +285,7 @@ pub enum CheckKind {
UnnecessaryDoubleCastOrProcess(String, String),
UnnecessarySubscriptReversal(String),
UnnecessaryComprehension(String),
UnnecessaryMap(String),
// flake8-print
PrintFound,
PPrintFound,
@@ -440,6 +442,7 @@ impl CheckCode {
CheckKind::UnnecessarySubscriptReversal("<reversed/set/sorted>".to_string())
}
CheckCode::C416 => CheckKind::UnnecessaryComprehension("<list/set>".to_string()),
CheckCode::C417 => CheckKind::UnnecessaryMap("<list/set/dict>".to_string()),
// flake8-print
CheckCode::T201 => CheckKind::PrintFound,
CheckCode::T203 => CheckKind::PPrintFound,
@@ -582,6 +585,7 @@ impl CheckKind {
CheckKind::UnnecessaryDoubleCastOrProcess(..) => &CheckCode::C414,
CheckKind::UnnecessarySubscriptReversal(_) => &CheckCode::C415,
CheckKind::UnnecessaryComprehension(..) => &CheckCode::C416,
CheckKind::UnnecessaryMap(_) => &CheckCode::C417,
// flake8-print
CheckKind::PrintFound => &CheckCode::T201,
CheckKind::PPrintFound => &CheckCode::T203,
@@ -863,6 +867,13 @@ impl CheckKind {
CheckKind::UnnecessaryComprehension(obj_type) => {
format!(" Unnecessary {obj_type} comprehension - rewrite using {obj_type}()")
}
CheckKind::UnnecessaryMap(obj_type) => {
if obj_type == "generator" {
"Unnecessary map usage - rewrite using a generator expression".to_string()
} else {
format!("Unnecessary map usage - rewrite using a {obj_type} comprehension")
}
}
// flake8-print
CheckKind::PrintFound => "`print` found".to_string(),
CheckKind::PPrintFound => "`pprint` found".to_string(),

View File

@@ -259,6 +259,7 @@ mod tests {
#[test_case(CheckCode::C414, Path::new("C414.py"); "C414")]
#[test_case(CheckCode::C415, Path::new("C415.py"); "C415")]
#[test_case(CheckCode::C416, Path::new("C416.py"); "C416")]
#[test_case(CheckCode::C417, Path::new("C417.py"); "C417")]
#[test_case(CheckCode::D100, Path::new("D.py"); "D100")]
#[test_case(CheckCode::D101, Path::new("D.py"); "D101")]
#[test_case(CheckCode::D102, Path::new("D.py"); "D102")]

View File

@@ -0,0 +1,77 @@
---
source: src/linter.rs
expression: checks
---
- kind:
UnnecessaryMap: generator
location:
row: 2
column: 1
end_location:
row: 2
column: 27
fix: ~
- kind:
UnnecessaryMap: generator
location:
row: 3
column: 1
end_location:
row: 3
column: 28
fix: ~
- kind:
UnnecessaryMap: list
location:
row: 4
column: 1
end_location:
row: 4
column: 33
fix: ~
- kind:
UnnecessaryMap: generator
location:
row: 4
column: 6
end_location:
row: 4
column: 32
fix: ~
- kind:
UnnecessaryMap: set
location:
row: 5
column: 1
end_location:
row: 5
column: 37
fix: ~
- kind:
UnnecessaryMap: generator
location:
row: 5
column: 5
end_location:
row: 5
column: 36
fix: ~
- kind:
UnnecessaryMap: dict
location:
row: 6
column: 1
end_location:
row: 6
column: 37
fix: ~
- kind:
UnnecessaryMap: generator
location:
row: 6
column: 6
end_location:
row: 6
column: 36
fix: ~