Skip to content

Commit 971affe

Browse files
dylwil3ntBre
authored andcommitted
[ruff] Stabilize checking in presence of slices for collection-literal-concatenation (RUF005) (#18500)
1 parent 0d9f732 commit 971affe

File tree

4 files changed

+12
-29
lines changed

4 files changed

+12
-29
lines changed

crates/ruff_linter/src/preview.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,6 @@ pub(crate) const fn is_unicode_to_unicode_confusables_enabled(settings: &LinterS
104104
settings.preview.is_enabled()
105105
}
106106

107-
// https://github.com/astral-sh/ruff/pull/17078
108-
pub(crate) const fn is_support_slices_in_literal_concatenation_enabled(
109-
settings: &LinterSettings,
110-
) -> bool {
111-
settings.preview.is_enabled()
112-
}
113-
114107
// https://github.com/astral-sh/ruff/pull/11370
115108
pub(crate) const fn is_undefined_export_in_dunder_init_enabled(settings: &LinterSettings) -> bool {
116109
settings.preview.is_enabled()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ mod tests {
2424
use crate::{assert_messages, settings};
2525

2626
#[test_case(Rule::CollectionLiteralConcatenation, Path::new("RUF005.py"))]
27+
#[test_case(Rule::CollectionLiteralConcatenation, Path::new("RUF005_slices.py"))]
2728
#[test_case(Rule::AsyncioDanglingTask, Path::new("RUF006.py"))]
2829
#[test_case(Rule::ZipInsteadOfPairwise, Path::new("RUF007.py"))]
2930
#[test_case(Rule::MutableDataclassDefault, Path::new("RUF008.py"))]
@@ -483,7 +484,6 @@ mod tests {
483484
#[test_case(Rule::ClassWithMixedTypeVars, Path::new("RUF053.py"))]
484485
#[test_case(Rule::IndentedFormFeed, Path::new("RUF054.py"))]
485486
#[test_case(Rule::ImplicitClassVarInDataclass, Path::new("RUF045.py"))]
486-
#[test_case(Rule::CollectionLiteralConcatenation, Path::new("RUF005_slices.py"))]
487487
fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> {
488488
let snapshot = format!(
489489
"preview__{}_{}",

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

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use ruff_text_size::{Ranged, TextRange};
44

55
use crate::checkers::ast::Checker;
66
use crate::fix::snippet::SourceCodeSnippet;
7-
use crate::preview::is_support_slices_in_literal_concatenation_enabled;
87
use crate::{Edit, Fix, FixAvailability, Violation};
98

109
/// ## What it does
@@ -96,7 +95,7 @@ enum Type {
9695
}
9796

9897
/// Recursively merge all the tuples and lists in the expression.
99-
fn concatenate_expressions(expr: &Expr, should_support_slices: bool) -> Option<(Expr, Type)> {
98+
fn concatenate_expressions(expr: &Expr) -> Option<(Expr, Type)> {
10099
let Expr::BinOp(ast::ExprBinOp {
101100
left,
102101
op: Operator::Add,
@@ -108,22 +107,18 @@ fn concatenate_expressions(expr: &Expr, should_support_slices: bool) -> Option<(
108107
};
109108

110109
let new_left = match left.as_ref() {
111-
Expr::BinOp(ast::ExprBinOp { .. }) => {
112-
match concatenate_expressions(left, should_support_slices) {
113-
Some((new_left, _)) => new_left,
114-
None => *left.clone(),
115-
}
116-
}
110+
Expr::BinOp(ast::ExprBinOp { .. }) => match concatenate_expressions(left) {
111+
Some((new_left, _)) => new_left,
112+
None => *left.clone(),
113+
},
117114
_ => *left.clone(),
118115
};
119116

120117
let new_right = match right.as_ref() {
121-
Expr::BinOp(ast::ExprBinOp { .. }) => {
122-
match concatenate_expressions(right, should_support_slices) {
123-
Some((new_right, _)) => new_right,
124-
None => *right.clone(),
125-
}
126-
}
118+
Expr::BinOp(ast::ExprBinOp { .. }) => match concatenate_expressions(right) {
119+
Some((new_right, _)) => new_right,
120+
None => *right.clone(),
121+
},
127122
_ => *right.clone(),
128123
};
129124

@@ -151,9 +146,7 @@ fn concatenate_expressions(expr: &Expr, should_support_slices: bool) -> Option<(
151146
make_splat_elts(splat_element, other_elements, splat_at_left)
152147
}
153148
// Subscripts are also considered safe-ish to splat if the indexer is a slice.
154-
Expr::Subscript(ast::ExprSubscript { slice, .. })
155-
if should_support_slices && matches!(&**slice, Expr::Slice(_)) =>
156-
{
149+
Expr::Subscript(ast::ExprSubscript { slice, .. }) if matches!(&**slice, Expr::Slice(_)) => {
157150
make_splat_elts(splat_element, other_elements, splat_at_left)
158151
}
159152
// If the splat element is itself a list/tuple, insert them in the other list/tuple.
@@ -198,10 +191,7 @@ pub(crate) fn collection_literal_concatenation(checker: &Checker, expr: &Expr) {
198191
return;
199192
}
200193

201-
let should_support_slices =
202-
is_support_slices_in_literal_concatenation_enabled(checker.settings);
203-
204-
let Some((new_expr, type_)) = concatenate_expressions(expr, should_support_slices) else {
194+
let Some((new_expr, type_)) = concatenate_expressions(expr) else {
205195
return;
206196
};
207197

0 commit comments

Comments
 (0)