diff --git a/README.md b/README.md index d266951edc..a63e5f064b 100644 --- a/README.md +++ b/README.md @@ -215,7 +215,7 @@ ruff also implements some of the most popular Flake8 plugins natively, including - [`flake8-builtins`](https://pypi.org/project/flake8-builtins/) - [`flake8-super`](https://pypi.org/project/flake8-super/) - [`flake8-print`](https://pypi.org/project/flake8-print/) -- [`flake8-comprehensions`](https://pypi.org/project/flake8-comprehensions/) (13/16) +- [`flake8-comprehensions`](https://pypi.org/project/flake8-comprehensions/) (14/16) - [`flake8-bugbear`](https://pypi.org/project/flake8-bugbear/) (3/32) - [`flake8-docstrings`](https://pypi.org/project/flake8-docstrings/) (37/48) - [`pyupgrade`](https://pypi.org/project/pyupgrade/) (8/34) @@ -293,6 +293,7 @@ The 🛠 emoji indicates that a rule is automatically fixable by the `--fix` com | C408 | UnnecessaryCollectionCall | Unnecessary call - rewrite as a literal | | | | C409 | UnnecessaryLiteralWithinTupleCall | Unnecessary literal passed to tuple() - remove the outer call to tuple() | | | | C410 | UnnecessaryLiteralWithinListCall | Unnecessary literal passed to list() - rewrite as a list literal | | | +| C411 | UnnecessaryListCall | Unnecessary list call - remove the outer call to list() | | | | C414 | UnnecessaryDoubleCastOrProcess | Unnecessary call within (). | | | | C415 | UnnecessarySubscriptReversal | Unnecessary subscript reversal of iterable within () | | | | C416 | UnnecessaryComprehension | Unnecessary comprehension - rewrite using () | | | diff --git a/resources/test/fixtures/C411.py b/resources/test/fixtures/C411.py new file mode 100644 index 0000000000..8cb272217b --- /dev/null +++ b/resources/test/fixtures/C411.py @@ -0,0 +1,2 @@ +x = [1, 2, 3] +list([i for i in x]) diff --git a/src/ast/checkers.rs b/src/ast/checkers.rs index 644284afba..fd753d3177 100644 --- a/src/ast/checkers.rs +++ b/src/ast/checkers.rs @@ -1004,6 +1004,22 @@ pub fn unnecessary_literal_within_list_call( None } +pub fn unnecessary_list_call(expr: &Expr, func: &Expr, args: &[Expr]) -> Option { + if let ExprKind::Name { id, .. } = &func.node { + if id == "list" { + if let Some(arg) = args.first() { + if let ExprKind::ListComp { .. } = &arg.node { + return Some(Check::new( + CheckKind::UnnecessaryListCall, + Range::from_located(expr), + )); + } + } + } + } + None +} + pub fn unnecessary_double_cast_or_process( expr: &Expr, func: &Expr, diff --git a/src/check_ast.rs b/src/check_ast.rs index 359caf9884..5f5bff98fb 100644 --- a/src/check_ast.rs +++ b/src/check_ast.rs @@ -837,6 +837,12 @@ where }; } + if self.settings.enabled.contains(&CheckCode::C411) { + if let Some(check) = checkers::unnecessary_list_call(expr, func, args) { + self.checks.push(check); + }; + } + if self.settings.enabled.contains(&CheckCode::C414) { if let Some(check) = checkers::unnecessary_double_cast_or_process(expr, func, args) diff --git a/src/checks.rs b/src/checks.rs index ba74f840c3..4775f3ea10 100644 --- a/src/checks.rs +++ b/src/checks.rs @@ -137,6 +137,7 @@ pub enum CheckCode { C408, C409, C410, + C411, C414, C415, C416, @@ -273,6 +274,7 @@ pub enum CheckKind { UnnecessaryCollectionCall(String), UnnecessaryLiteralWithinTupleCall(String), UnnecessaryLiteralWithinListCall(String), + UnnecessaryListCall, UnnecessaryDoubleCastOrProcess(String, String), UnnecessarySubscriptReversal(String), UnnecessaryComprehension(String), @@ -416,6 +418,7 @@ impl CheckCode { CheckCode::C410 => { CheckKind::UnnecessaryLiteralWithinListCall("".to_string()) } + CheckCode::C411 => CheckKind::UnnecessaryListCall, CheckCode::C414 => CheckKind::UnnecessaryDoubleCastOrProcess( "".to_string(), "".to_string(), @@ -555,6 +558,7 @@ impl CheckKind { CheckKind::UnnecessaryCollectionCall(_) => &CheckCode::C408, CheckKind::UnnecessaryLiteralWithinTupleCall(..) => &CheckCode::C409, CheckKind::UnnecessaryLiteralWithinListCall(..) => &CheckCode::C410, + CheckKind::UnnecessaryListCall => &CheckCode::C411, CheckKind::UnnecessaryDoubleCastOrProcess(..) => &CheckCode::C414, CheckKind::UnnecessarySubscriptReversal(_) => &CheckCode::C415, CheckKind::UnnecessaryComprehension(..) => &CheckCode::C416, @@ -820,6 +824,9 @@ impl CheckKind { ) } } + CheckKind::UnnecessaryListCall => { + "Unnecessary list call - remove the outer call to list()".to_string() + } CheckKind::UnnecessaryDoubleCastOrProcess(inner, outer) => { format!("Unnecessary {inner} call within {outer}().") } diff --git a/src/linter.rs b/src/linter.rs index dd470657b6..4c4f1ec6c0 100644 --- a/src/linter.rs +++ b/src/linter.rs @@ -1007,6 +1007,18 @@ mod tests { Ok(()) } + #[test] + fn c411() -> Result<()> { + let mut checks = check_path( + Path::new("./resources/test/fixtures/C411.py"), + &settings::Settings::for_rule(CheckCode::C411), + &fixer::Mode::Generate, + )?; + checks.sort_by_key(|check| check.location); + insta::assert_yaml_snapshot!(checks); + Ok(()) + } + #[test] fn c414() -> Result<()> { let mut checks = check_path( diff --git a/src/snapshots/ruff__linter__tests__c411.snap b/src/snapshots/ruff__linter__tests__c411.snap new file mode 100644 index 0000000000..60d9652faf --- /dev/null +++ b/src/snapshots/ruff__linter__tests__c411.snap @@ -0,0 +1,13 @@ +--- +source: src/linter.rs +expression: checks +--- +- kind: UnnecessaryListCall + location: + row: 2 + column: 1 + end_location: + row: 2 + column: 21 + fix: ~ +