-
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 new potentially unsafe "unpack instead of concatenating" check
- Loading branch information
Showing
10 changed files
with
176 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,5 @@ | ||
foo = [4, 5, 6] | ||
bar = [1, 2, 3] + foo | ||
quux = (7, 8, 9) + bar | ||
spam = quux + (10, 11, 12) | ||
eggs = spam + [13, 14, 15] |
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 |
---|---|---|
|
@@ -1622,6 +1622,9 @@ | |
"SIM4", | ||
"SIM40", | ||
"SIM401", | ||
"SIM7", | ||
"SIM70", | ||
"SIM701", | ||
"T", | ||
"T1", | ||
"T10", | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
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; | ||
use rustpython_ast::{Expr, ExprContext, ExprKind, Operator}; | ||
|
||
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 | ||
} | ||
|
||
/// SIM701 (not in flake8-simplify) | ||
/// NB: this could be unsafe if the non-literal expression has overridden __add__ | ||
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; | ||
} | ||
let new_expr = match (&left.node, &right.node) { | ||
(ExprKind::List { elts: l_elts, ctx }, _) => create_expr(ExprKind::List { | ||
elts: make_splat_elts(right, l_elts, false), | ||
ctx: ctx.clone(), | ||
}), | ||
(ExprKind::Tuple { elts: l_elts, ctx }, _) => create_expr(ExprKind::Tuple { | ||
elts: make_splat_elts(right, l_elts, false), | ||
ctx: ctx.clone(), | ||
}), | ||
(_, ExprKind::List { elts: r_elts, ctx }) => create_expr(ExprKind::List { | ||
elts: make_splat_elts(left, r_elts, true), | ||
ctx: ctx.clone(), | ||
}), | ||
(_, ExprKind::Tuple { elts: r_elts, ctx }) => create_expr(ExprKind::Tuple { | ||
elts: make_splat_elts(left, r_elts, true), | ||
ctx: ctx.clone(), | ||
}), | ||
_ => return, | ||
}; | ||
|
||
let new_expr_string = unparse_expr(&new_expr, checker.stylist); | ||
|
||
let mut diagnostic = Diagnostic::new( | ||
violations::UnpackInsteadOfConcatenatingToCollectionLiteral(new_expr_string.clone()), | ||
Range::from_located(expr), | ||
); | ||
if checker.patch(diagnostic.kind.code()) { | ||
diagnostic.amend(Fix::replacement( | ||
new_expr_string, | ||
expr.location, | ||
expr.end_location.unwrap(), | ||
)); | ||
} | ||
checker.diagnostics.push(diagnostic); | ||
} |
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
73 changes: 73 additions & 0 deletions
73
...ules/flake8_simplify/snapshots/ruff__rules__flake8_simplify__tests__SIM701_SIM701.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,73 @@ | ||
--- | ||
source: src/rules/flake8_simplify/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
UnpackInsteadOfConcatenatingToCollectionLiteral: "[1, 2, 3, *foo]" | ||
location: | ||
row: 2 | ||
column: 6 | ||
end_location: | ||
row: 2 | ||
column: 21 | ||
fix: | ||
content: "[1, 2, 3, *foo]" | ||
location: | ||
row: 2 | ||
column: 6 | ||
end_location: | ||
row: 2 | ||
column: 21 | ||
parent: ~ | ||
- kind: | ||
UnpackInsteadOfConcatenatingToCollectionLiteral: "7, 8, 9, *bar" | ||
location: | ||
row: 3 | ||
column: 7 | ||
end_location: | ||
row: 3 | ||
column: 22 | ||
fix: | ||
content: "7, 8, 9, *bar" | ||
location: | ||
row: 3 | ||
column: 7 | ||
end_location: | ||
row: 3 | ||
column: 22 | ||
parent: ~ | ||
- kind: | ||
UnpackInsteadOfConcatenatingToCollectionLiteral: "*quux, 10, 11, 12" | ||
location: | ||
row: 4 | ||
column: 7 | ||
end_location: | ||
row: 4 | ||
column: 26 | ||
fix: | ||
content: "*quux, 10, 11, 12" | ||
location: | ||
row: 4 | ||
column: 7 | ||
end_location: | ||
row: 4 | ||
column: 26 | ||
parent: ~ | ||
- kind: | ||
UnpackInsteadOfConcatenatingToCollectionLiteral: "[*spam, 13, 14, 15]" | ||
location: | ||
row: 5 | ||
column: 7 | ||
end_location: | ||
row: 5 | ||
column: 26 | ||
fix: | ||
content: "[*spam, 13, 14, 15]" | ||
location: | ||
row: 5 | ||
column: 7 | ||
end_location: | ||
row: 5 | ||
column: 26 | ||
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