-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RUF005 "unpack instead of concatenating" check
* Guard against complex splattees * Wrap tuples in parens
- Loading branch information
Showing
10 changed files
with
318 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class Fun: | ||
words = ("how", "fun!") | ||
|
||
def yay(self): | ||
return self.words | ||
|
||
yay = Fun().yay | ||
|
||
foo = [4, 5, 6] | ||
bar = [1, 2, 3] + foo | ||
zoob = tuple(bar) | ||
quux = (7, 8, 9) + zoob | ||
spam = quux + (10, 11, 12) | ||
spom = list(spam) | ||
eggs = spom + [13, 14, 15] | ||
elatement = ("we all say", ) + yay() | ||
excitement = ("we all think", ) + Fun().yay() | ||
astonishment = ("we all feel", ) + Fun.words | ||
|
||
chain = ['a', 'b', 'c'] + eggs + list(('yes', 'no', 'pants') + zoob) | ||
|
||
baz = () + zoob |
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 |
---|---|---|
|
@@ -1631,6 +1631,7 @@ | |
"RUF002", | ||
"RUF003", | ||
"RUF004", | ||
"RUF005", | ||
"RUF1", | ||
"RUF10", | ||
"RUF100", | ||
|
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
95 changes: 95 additions & 0 deletions
95
src/rules/ruff/rules/unpack_instead_of_concatenating_to_collection_literal.rs
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,95 @@ | ||
use rustpython_ast::{Expr, ExprContext, ExprKind, Operator}; | ||
|
||
use crate::ast::helpers::{create_expr, unparse_expr}; | ||
use crate::ast::types::Range; | ||
use crate::checkers::ast::Checker; | ||
use crate::fix::Fix; | ||
use crate::registry::Diagnostic; | ||
use crate::violations; | ||
|
||
fn make_splat_elts( | ||
splat_element: &Expr, | ||
other_elements: &[Expr], | ||
splat_at_left: bool, | ||
) -> Vec<Expr> { | ||
let mut new_elts = other_elements.to_owned(); | ||
let splat = create_expr(ExprKind::Starred { | ||
value: Box::from(splat_element.clone()), | ||
ctx: ExprContext::Load, | ||
}); | ||
if splat_at_left { | ||
new_elts.insert(0, splat); | ||
} else { | ||
new_elts.push(splat); | ||
} | ||
new_elts | ||
} | ||
|
||
#[derive(Debug)] | ||
enum Kind { | ||
List, | ||
Tuple, | ||
} | ||
|
||
/// RUF005 | ||
/// This suggestion could be unsafe if the non-literal expression in the | ||
/// expression has overridden the `__add__` (or `__radd__`) magic methods. | ||
pub fn unpack_instead_of_concatenating_to_collection_literal(checker: &mut Checker, expr: &Expr) { | ||
let ExprKind::BinOp { op, left, right } = &expr.node else { | ||
return; | ||
}; | ||
if !matches!(op, Operator::Add) { | ||
return; | ||
} | ||
|
||
// Figure out which way the splat is, and what the kind of the collection is. | ||
let (kind, splat_element, other_elements, splat_at_left, ctx) = match (&left.node, &right.node) | ||
{ | ||
(ExprKind::List { elts: l_elts, ctx }, _) => (Kind::List, right, l_elts, false, ctx), | ||
(ExprKind::Tuple { elts: l_elts, ctx }, _) => (Kind::Tuple, right, l_elts, false, ctx), | ||
(_, ExprKind::List { elts: r_elts, ctx }) => (Kind::List, left, r_elts, true, ctx), | ||
(_, ExprKind::Tuple { elts: r_elts, ctx }) => (Kind::Tuple, left, r_elts, true, ctx), | ||
_ => return, | ||
}; | ||
|
||
// We'll be a bit conservative here; only calls, names and attribute accesses | ||
// will be considered as splat elements. | ||
if !matches!( | ||
splat_element.node, | ||
ExprKind::Call { .. } | ExprKind::Name { .. } | ExprKind::Attribute { .. } | ||
) { | ||
return; | ||
} | ||
|
||
let new_expr = match kind { | ||
Kind::List => create_expr(ExprKind::List { | ||
elts: make_splat_elts(splat_element, other_elements, splat_at_left), | ||
ctx: ctx.clone(), | ||
}), | ||
Kind::Tuple => create_expr(ExprKind::Tuple { | ||
elts: make_splat_elts(splat_element, other_elements, splat_at_left), | ||
ctx: ctx.clone(), | ||
}), | ||
}; | ||
|
||
let mut new_expr_string = unparse_expr(&new_expr, checker.stylist); | ||
|
||
new_expr_string = match kind { | ||
// Wrap the new expression in parentheses if it was a tuple | ||
Kind::Tuple => format!("({new_expr_string})"), | ||
Kind::List => new_expr_string, | ||
}; | ||
|
||
let mut diagnostic = Diagnostic::new( | ||
violations::UnpackInsteadOfConcatenatingToCollectionLiteral(new_expr_string.clone()), | ||
Range::from_located(expr), | ||
); | ||
if checker.patch(diagnostic.kind.rule()) { | ||
diagnostic.amend(Fix::replacement( | ||
new_expr_string, | ||
expr.location, | ||
expr.end_location.unwrap(), | ||
)); | ||
} | ||
checker.diagnostics.push(diagnostic); | ||
} |
175 changes: 175 additions & 0 deletions
175
src/rules/ruff/snapshots/ruff__rules__ruff__tests__RUF005_RUF005.py.snap
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,175 @@ | ||
--- | ||
source: src/rules/ruff/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
UnpackInsteadOfConcatenatingToCollectionLiteral: "[1, 2, 3, *foo]" | ||
location: | ||
row: 10 | ||
column: 6 | ||
end_location: | ||
row: 10 | ||
column: 21 | ||
fix: | ||
content: "[1, 2, 3, *foo]" | ||
location: | ||
row: 10 | ||
column: 6 | ||
end_location: | ||
row: 10 | ||
column: 21 | ||
parent: ~ | ||
- kind: | ||
UnpackInsteadOfConcatenatingToCollectionLiteral: "(7, 8, 9, *zoob)" | ||
location: | ||
row: 12 | ||
column: 7 | ||
end_location: | ||
row: 12 | ||
column: 23 | ||
fix: | ||
content: "(7, 8, 9, *zoob)" | ||
location: | ||
row: 12 | ||
column: 7 | ||
end_location: | ||
row: 12 | ||
column: 23 | ||
parent: ~ | ||
- kind: | ||
UnpackInsteadOfConcatenatingToCollectionLiteral: "(*quux, 10, 11, 12)" | ||
location: | ||
row: 13 | ||
column: 7 | ||
end_location: | ||
row: 13 | ||
column: 26 | ||
fix: | ||
content: "(*quux, 10, 11, 12)" | ||
location: | ||
row: 13 | ||
column: 7 | ||
end_location: | ||
row: 13 | ||
column: 26 | ||
parent: ~ | ||
- kind: | ||
UnpackInsteadOfConcatenatingToCollectionLiteral: "[*spom, 13, 14, 15]" | ||
location: | ||
row: 15 | ||
column: 7 | ||
end_location: | ||
row: 15 | ||
column: 26 | ||
fix: | ||
content: "[*spom, 13, 14, 15]" | ||
location: | ||
row: 15 | ||
column: 7 | ||
end_location: | ||
row: 15 | ||
column: 26 | ||
parent: ~ | ||
- kind: | ||
UnpackInsteadOfConcatenatingToCollectionLiteral: "(\"we all say\", *yay())" | ||
location: | ||
row: 16 | ||
column: 12 | ||
end_location: | ||
row: 16 | ||
column: 36 | ||
fix: | ||
content: "(\"we all say\", *yay())" | ||
location: | ||
row: 16 | ||
column: 12 | ||
end_location: | ||
row: 16 | ||
column: 36 | ||
parent: ~ | ||
- kind: | ||
UnpackInsteadOfConcatenatingToCollectionLiteral: "(\"we all think\", *Fun().yay())" | ||
location: | ||
row: 17 | ||
column: 13 | ||
end_location: | ||
row: 17 | ||
column: 45 | ||
fix: | ||
content: "(\"we all think\", *Fun().yay())" | ||
location: | ||
row: 17 | ||
column: 13 | ||
end_location: | ||
row: 17 | ||
column: 45 | ||
parent: ~ | ||
- kind: | ||
UnpackInsteadOfConcatenatingToCollectionLiteral: "(\"we all feel\", *Fun.words)" | ||
location: | ||
row: 18 | ||
column: 15 | ||
end_location: | ||
row: 18 | ||
column: 44 | ||
fix: | ||
content: "(\"we all feel\", *Fun.words)" | ||
location: | ||
row: 18 | ||
column: 15 | ||
end_location: | ||
row: 18 | ||
column: 44 | ||
parent: ~ | ||
- kind: | ||
UnpackInsteadOfConcatenatingToCollectionLiteral: "[\"a\", \"b\", \"c\", *eggs]" | ||
location: | ||
row: 20 | ||
column: 8 | ||
end_location: | ||
row: 20 | ||
column: 30 | ||
fix: | ||
content: "[\"a\", \"b\", \"c\", *eggs]" | ||
location: | ||
row: 20 | ||
column: 8 | ||
end_location: | ||
row: 20 | ||
column: 30 | ||
parent: ~ | ||
- kind: | ||
UnpackInsteadOfConcatenatingToCollectionLiteral: "(\"yes\", \"no\", \"pants\", *zoob)" | ||
location: | ||
row: 20 | ||
column: 38 | ||
end_location: | ||
row: 20 | ||
column: 67 | ||
fix: | ||
content: "(\"yes\", \"no\", \"pants\", *zoob)" | ||
location: | ||
row: 20 | ||
column: 38 | ||
end_location: | ||
row: 20 | ||
column: 67 | ||
parent: ~ | ||
- kind: | ||
UnpackInsteadOfConcatenatingToCollectionLiteral: "(*zoob,)" | ||
location: | ||
row: 22 | ||
column: 6 | ||
end_location: | ||
row: 22 | ||
column: 15 | ||
fix: | ||
content: "(*zoob,)" | ||
location: | ||
row: 22 | ||
column: 6 | ||
end_location: | ||
row: 22 | ||
column: 15 | ||
parent: ~ | ||
|
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