mirror of https://github.com/astral-sh/ruff
Implement C413 (#421)
This commit is contained in:
parent
3e789136af
commit
bcddd9e97f
|
|
@ -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-builtins`](https://pypi.org/project/flake8-builtins/)
|
||||||
- [`flake8-super`](https://pypi.org/project/flake8-super/)
|
- [`flake8-super`](https://pypi.org/project/flake8-super/)
|
||||||
- [`flake8-print`](https://pypi.org/project/flake8-print/)
|
- [`flake8-print`](https://pypi.org/project/flake8-print/)
|
||||||
- [`flake8-comprehensions`](https://pypi.org/project/flake8-comprehensions/) (14/16)
|
- [`flake8-comprehensions`](https://pypi.org/project/flake8-comprehensions/) (15/16)
|
||||||
- [`flake8-bugbear`](https://pypi.org/project/flake8-bugbear/) (3/32)
|
- [`flake8-bugbear`](https://pypi.org/project/flake8-bugbear/) (3/32)
|
||||||
- [`flake8-docstrings`](https://pypi.org/project/flake8-docstrings/) (37/48)
|
- [`flake8-docstrings`](https://pypi.org/project/flake8-docstrings/) (37/48)
|
||||||
- [`pyupgrade`](https://pypi.org/project/pyupgrade/) (8/34)
|
- [`pyupgrade`](https://pypi.org/project/pyupgrade/) (8/34)
|
||||||
|
|
@ -294,6 +294,7 @@ The 🛠 emoji indicates that a rule is automatically fixable by the `--fix` com
|
||||||
| C409 | UnnecessaryLiteralWithinTupleCall | Unnecessary <list/tuple> literal passed to tuple() - remove the outer call to tuple() | | |
|
| C409 | UnnecessaryLiteralWithinTupleCall | Unnecessary <list/tuple> literal passed to tuple() - remove the outer call to tuple() | | |
|
||||||
| C410 | UnnecessaryLiteralWithinListCall | Unnecessary <list/tuple> literal passed to list() - rewrite as a list literal | | |
|
| C410 | UnnecessaryLiteralWithinListCall | Unnecessary <list/tuple> literal passed to list() - rewrite as a list literal | | |
|
||||||
| C411 | UnnecessaryListCall | Unnecessary list call - remove the outer call to list() | | |
|
| C411 | UnnecessaryListCall | Unnecessary list call - remove the outer call to list() | | |
|
||||||
|
| C413 | UnnecessaryCallAroundSorted | Unnecessary <list/reversed> call around sorted() | | |
|
||||||
| C414 | UnnecessaryDoubleCastOrProcess | Unnecessary <list/reversed/set/sorted/tuple> call within <list/set/sorted/tuple>(). | | |
|
| C414 | UnnecessaryDoubleCastOrProcess | Unnecessary <list/reversed/set/sorted/tuple> call within <list/set/sorted/tuple>(). | | |
|
||||||
| C415 | UnnecessarySubscriptReversal | Unnecessary subscript reversal of iterable within <reversed/set/sorted>() | | |
|
| C415 | UnnecessarySubscriptReversal | Unnecessary subscript reversal of iterable within <reversed/set/sorted>() | | |
|
||||||
| C416 | UnnecessaryComprehension | Unnecessary <list/set> comprehension - rewrite using <list/set>() | | |
|
| C416 | UnnecessaryComprehension | Unnecessary <list/set> comprehension - rewrite using <list/set>() | | |
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,4 @@
|
||||||
|
x = [2, 3, 1]
|
||||||
|
list(sorted(x))
|
||||||
|
reversed(sorted(x))
|
||||||
|
reversed(sorted(x, reverse=True))
|
||||||
|
|
@ -1020,6 +1020,26 @@ pub fn unnecessary_list_call(expr: &Expr, func: &Expr, args: &[Expr]) -> Option<
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn unnecessary_call_around_sorted(expr: &Expr, func: &Expr, args: &[Expr]) -> Option<Check> {
|
||||||
|
if let ExprKind::Name { id: outer, .. } = &func.node {
|
||||||
|
if outer == "list" || outer == "reversed" {
|
||||||
|
if let Some(arg) = args.first() {
|
||||||
|
if let ExprKind::Call { func, .. } = &arg.node {
|
||||||
|
if let ExprKind::Name { id: inner, .. } = &func.node {
|
||||||
|
if inner == "sorted" {
|
||||||
|
return Some(Check::new(
|
||||||
|
CheckKind::UnnecessaryCallAroundSorted(outer.to_string()),
|
||||||
|
Range::from_located(expr),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
pub fn unnecessary_double_cast_or_process(
|
pub fn unnecessary_double_cast_or_process(
|
||||||
expr: &Expr,
|
expr: &Expr,
|
||||||
func: &Expr,
|
func: &Expr,
|
||||||
|
|
|
||||||
|
|
@ -843,6 +843,13 @@ where
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if self.settings.enabled.contains(&CheckCode::C413) {
|
||||||
|
if let Some(check) = checkers::unnecessary_call_around_sorted(expr, func, args)
|
||||||
|
{
|
||||||
|
self.checks.push(check);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
if self.settings.enabled.contains(&CheckCode::C414) {
|
if self.settings.enabled.contains(&CheckCode::C414) {
|
||||||
if let Some(check) =
|
if let Some(check) =
|
||||||
checkers::unnecessary_double_cast_or_process(expr, func, args)
|
checkers::unnecessary_double_cast_or_process(expr, func, args)
|
||||||
|
|
|
||||||
|
|
@ -138,6 +138,7 @@ pub enum CheckCode {
|
||||||
C409,
|
C409,
|
||||||
C410,
|
C410,
|
||||||
C411,
|
C411,
|
||||||
|
C413,
|
||||||
C414,
|
C414,
|
||||||
C415,
|
C415,
|
||||||
C416,
|
C416,
|
||||||
|
|
@ -275,6 +276,7 @@ pub enum CheckKind {
|
||||||
UnnecessaryLiteralWithinTupleCall(String),
|
UnnecessaryLiteralWithinTupleCall(String),
|
||||||
UnnecessaryLiteralWithinListCall(String),
|
UnnecessaryLiteralWithinListCall(String),
|
||||||
UnnecessaryListCall,
|
UnnecessaryListCall,
|
||||||
|
UnnecessaryCallAroundSorted(String),
|
||||||
UnnecessaryDoubleCastOrProcess(String, String),
|
UnnecessaryDoubleCastOrProcess(String, String),
|
||||||
UnnecessarySubscriptReversal(String),
|
UnnecessarySubscriptReversal(String),
|
||||||
UnnecessaryComprehension(String),
|
UnnecessaryComprehension(String),
|
||||||
|
|
@ -419,6 +421,9 @@ impl CheckCode {
|
||||||
CheckKind::UnnecessaryLiteralWithinListCall("<list/tuple>".to_string())
|
CheckKind::UnnecessaryLiteralWithinListCall("<list/tuple>".to_string())
|
||||||
}
|
}
|
||||||
CheckCode::C411 => CheckKind::UnnecessaryListCall,
|
CheckCode::C411 => CheckKind::UnnecessaryListCall,
|
||||||
|
CheckCode::C413 => {
|
||||||
|
CheckKind::UnnecessaryCallAroundSorted("<list/reversed>".to_string())
|
||||||
|
}
|
||||||
CheckCode::C414 => CheckKind::UnnecessaryDoubleCastOrProcess(
|
CheckCode::C414 => CheckKind::UnnecessaryDoubleCastOrProcess(
|
||||||
"<list/reversed/set/sorted/tuple>".to_string(),
|
"<list/reversed/set/sorted/tuple>".to_string(),
|
||||||
"<list/set/sorted/tuple>".to_string(),
|
"<list/set/sorted/tuple>".to_string(),
|
||||||
|
|
@ -559,6 +564,7 @@ impl CheckKind {
|
||||||
CheckKind::UnnecessaryLiteralWithinTupleCall(..) => &CheckCode::C409,
|
CheckKind::UnnecessaryLiteralWithinTupleCall(..) => &CheckCode::C409,
|
||||||
CheckKind::UnnecessaryLiteralWithinListCall(..) => &CheckCode::C410,
|
CheckKind::UnnecessaryLiteralWithinListCall(..) => &CheckCode::C410,
|
||||||
CheckKind::UnnecessaryListCall => &CheckCode::C411,
|
CheckKind::UnnecessaryListCall => &CheckCode::C411,
|
||||||
|
CheckKind::UnnecessaryCallAroundSorted(_) => &CheckCode::C413,
|
||||||
CheckKind::UnnecessaryDoubleCastOrProcess(..) => &CheckCode::C414,
|
CheckKind::UnnecessaryDoubleCastOrProcess(..) => &CheckCode::C414,
|
||||||
CheckKind::UnnecessarySubscriptReversal(_) => &CheckCode::C415,
|
CheckKind::UnnecessarySubscriptReversal(_) => &CheckCode::C415,
|
||||||
CheckKind::UnnecessaryComprehension(..) => &CheckCode::C416,
|
CheckKind::UnnecessaryComprehension(..) => &CheckCode::C416,
|
||||||
|
|
@ -827,6 +833,9 @@ impl CheckKind {
|
||||||
CheckKind::UnnecessaryListCall => {
|
CheckKind::UnnecessaryListCall => {
|
||||||
"Unnecessary list call - remove the outer call to list()".to_string()
|
"Unnecessary list call - remove the outer call to list()".to_string()
|
||||||
}
|
}
|
||||||
|
CheckKind::UnnecessaryCallAroundSorted(func) => {
|
||||||
|
format!("Unnecessary {func} call around sorted()")
|
||||||
|
}
|
||||||
CheckKind::UnnecessaryDoubleCastOrProcess(inner, outer) => {
|
CheckKind::UnnecessaryDoubleCastOrProcess(inner, outer) => {
|
||||||
format!("Unnecessary {inner} call within {outer}().")
|
format!("Unnecessary {inner} call within {outer}().")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1019,6 +1019,18 @@ mod tests {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn c413() -> Result<()> {
|
||||||
|
let mut checks = check_path(
|
||||||
|
Path::new("./resources/test/fixtures/C413.py"),
|
||||||
|
&settings::Settings::for_rule(CheckCode::C413),
|
||||||
|
&fixer::Mode::Generate,
|
||||||
|
)?;
|
||||||
|
checks.sort_by_key(|check| check.location);
|
||||||
|
insta::assert_yaml_snapshot!(checks);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn c414() -> Result<()> {
|
fn c414() -> Result<()> {
|
||||||
let mut checks = check_path(
|
let mut checks = check_path(
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,32 @@
|
||||||
|
---
|
||||||
|
source: src/linter.rs
|
||||||
|
expression: checks
|
||||||
|
---
|
||||||
|
- kind:
|
||||||
|
UnnecessaryCallAroundSorted: list
|
||||||
|
location:
|
||||||
|
row: 2
|
||||||
|
column: 1
|
||||||
|
end_location:
|
||||||
|
row: 2
|
||||||
|
column: 16
|
||||||
|
fix: ~
|
||||||
|
- kind:
|
||||||
|
UnnecessaryCallAroundSorted: reversed
|
||||||
|
location:
|
||||||
|
row: 3
|
||||||
|
column: 1
|
||||||
|
end_location:
|
||||||
|
row: 3
|
||||||
|
column: 20
|
||||||
|
fix: ~
|
||||||
|
- kind:
|
||||||
|
UnnecessaryCallAroundSorted: reversed
|
||||||
|
location:
|
||||||
|
row: 4
|
||||||
|
column: 1
|
||||||
|
end_location:
|
||||||
|
row: 4
|
||||||
|
column: 34
|
||||||
|
fix: ~
|
||||||
|
|
||||||
Loading…
Reference in New Issue