Skip to content

Commit 973dba5

Browse files
committed
Gate RUF005 slice support as preview feature
1 parent a362bcb commit 973dba5

File tree

6 files changed

+214
-174
lines changed

6 files changed

+214
-174
lines changed

crates/ruff_linter/resources/test/fixtures/ruff/RUF005.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ def yay(self):
4747
astonishment = ("we all feel",) + Fun.words
4848

4949
chain = ["a", "b", "c"] + eggs + list(("yes", "no", "pants") + zoob)
50-
slicing1 = foo[:1] + [7, 8, 9]
51-
slicing2 = [7, 8, 9] + bar[1:]
52-
slicing3 = foo[:1] + [7, 8, 9] + bar[1:]
53-
indexing = foo[0] + [7, 8, 9] + bar[1] # Not changed; looks a little suspect for concatenation.
5450

5551
baz = () + zoob
5652

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
foo = [4, 5, 6]
2+
bar = [1, 2, 3] + foo
3+
slicing1 = foo[:1] + [7, 8, 9]
4+
slicing2 = [7, 8, 9] + bar[1:]
5+
slicing3 = foo[:1] + [7, 8, 9] + bar[1:]
6+
indexing = foo[0] + [7, 8, 9] + bar[1] # Not changed; looks a little suspect for concatenation.

crates/ruff_linter/src/rules/ruff/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,19 @@ mod tests {
109109
Ok(())
110110
}
111111

112+
#[test]
113+
fn preview_ruf005_slices() -> Result<()> {
114+
let diagnostics = test_path(
115+
Path::new("ruff/RUF005_slices.py"),
116+
&LinterSettings {
117+
preview: PreviewMode::Enabled,
118+
..LinterSettings::for_rules(vec![Rule::CollectionLiteralConcatenation])
119+
},
120+
)?;
121+
assert_messages!(diagnostics);
122+
Ok(())
123+
}
124+
112125
#[test]
113126
fn prefer_parentheses_getitem_tuple() -> Result<()> {
114127
let diagnostics = test_path(

crates/ruff_linter/src/rules/ruff/rules/collection_literal_concatenation.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ enum Type {
8989
}
9090

9191
/// Recursively merge all the tuples and lists in the expression.
92-
fn concatenate_expressions(expr: &Expr) -> Option<(Expr, Type)> {
92+
fn concatenate_expressions(expr: &Expr, should_support_slices: bool) -> Option<(Expr, Type)> {
9393
let Expr::BinOp(ast::ExprBinOp {
9494
left,
9595
op: Operator::Add,
@@ -101,18 +101,22 @@ fn concatenate_expressions(expr: &Expr) -> Option<(Expr, Type)> {
101101
};
102102

103103
let new_left = match left.as_ref() {
104-
Expr::BinOp(ast::ExprBinOp { .. }) => match concatenate_expressions(left) {
105-
Some((new_left, _)) => new_left,
106-
None => *left.clone(),
107-
},
104+
Expr::BinOp(ast::ExprBinOp { .. }) => {
105+
match concatenate_expressions(left, should_support_slices) {
106+
Some((new_left, _)) => new_left,
107+
None => *left.clone(),
108+
}
109+
}
108110
_ => *left.clone(),
109111
};
110112

111113
let new_right = match right.as_ref() {
112-
Expr::BinOp(ast::ExprBinOp { .. }) => match concatenate_expressions(right) {
113-
Some((new_right, _)) => new_right,
114-
None => *right.clone(),
115-
},
114+
Expr::BinOp(ast::ExprBinOp { .. }) => {
115+
match concatenate_expressions(right, should_support_slices) {
116+
Some((new_right, _)) => new_right,
117+
None => *right.clone(),
118+
}
119+
}
116120
_ => *right.clone(),
117121
};
118122

@@ -140,7 +144,9 @@ fn concatenate_expressions(expr: &Expr) -> Option<(Expr, Type)> {
140144
make_splat_elts(splat_element, other_elements, splat_at_left)
141145
}
142146
// Subscripts are also considered safe-ish to splat if the indexer is a slice.
143-
Expr::Subscript(ast::ExprSubscript { slice, .. }) if matches!(&**slice, Expr::Slice(_)) => {
147+
Expr::Subscript(ast::ExprSubscript { slice, .. })
148+
if should_support_slices && matches!(&**slice, Expr::Slice(_)) =>
149+
{
144150
make_splat_elts(splat_element, other_elements, splat_at_left)
145151
}
146152
// If the splat element is itself a list/tuple, insert them in the other list/tuple.
@@ -185,7 +191,9 @@ pub(crate) fn collection_literal_concatenation(checker: &Checker, expr: &Expr) {
185191
return;
186192
}
187193

188-
let Some((new_expr, type_)) = concatenate_expressions(expr) else {
194+
let should_support_slices = checker.settings.preview.is_enabled();
195+
196+
let Some((new_expr, type_)) = concatenate_expressions(expr, should_support_slices) else {
189197
return;
190198
};
191199

0 commit comments

Comments
 (0)