Skip to content

Commit c063940

Browse files
dylwil3ntBre
authored andcommitted
[ruff] Stabilize checking in presence of slices for collection-literal-concatenation (RUF005) (#18500)
1 parent 8aea383 commit c063940

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"))]
@@ -486,7 +487,6 @@ mod tests {
486487
#[test_case(Rule::ClassWithMixedTypeVars, Path::new("RUF053.py"))]
487488
#[test_case(Rule::IndentedFormFeed, Path::new("RUF054.py"))]
488489
#[test_case(Rule::ImplicitClassVarInDataclass, Path::new("RUF045.py"))]
489-
#[test_case(Rule::CollectionLiteralConcatenation, Path::new("RUF005_slices.py"))]
490490
fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> {
491491
let snapshot = format!(
492492
"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
@@ -97,7 +96,7 @@ enum Type {
9796
}
9897

9998
/// Recursively merge all the tuples and lists in the expression.
100-
fn concatenate_expressions(expr: &Expr, should_support_slices: bool) -> Option<(Expr, Type)> {
99+
fn concatenate_expressions(expr: &Expr) -> Option<(Expr, Type)> {
101100
let Expr::BinOp(ast::ExprBinOp {
102101
left,
103102
op: Operator::Add,
@@ -110,22 +109,18 @@ fn concatenate_expressions(expr: &Expr, should_support_slices: bool) -> Option<(
110109
};
111110

112111
let new_left = match left.as_ref() {
113-
Expr::BinOp(ast::ExprBinOp { .. }) => {
114-
match concatenate_expressions(left, should_support_slices) {
115-
Some((new_left, _)) => new_left,
116-
None => *left.clone(),
117-
}
118-
}
112+
Expr::BinOp(ast::ExprBinOp { .. }) => match concatenate_expressions(left) {
113+
Some((new_left, _)) => new_left,
114+
None => *left.clone(),
115+
},
119116
_ => *left.clone(),
120117
};
121118

122119
let new_right = match right.as_ref() {
123-
Expr::BinOp(ast::ExprBinOp { .. }) => {
124-
match concatenate_expressions(right, should_support_slices) {
125-
Some((new_right, _)) => new_right,
126-
None => *right.clone(),
127-
}
128-
}
120+
Expr::BinOp(ast::ExprBinOp { .. }) => match concatenate_expressions(right) {
121+
Some((new_right, _)) => new_right,
122+
None => *right.clone(),
123+
},
129124
_ => *right.clone(),
130125
};
131126

@@ -153,9 +148,7 @@ fn concatenate_expressions(expr: &Expr, should_support_slices: bool) -> Option<(
153148
make_splat_elts(splat_element, other_elements, splat_at_left)
154149
}
155150
// Subscripts are also considered safe-ish to splat if the indexer is a slice.
156-
Expr::Subscript(ast::ExprSubscript { slice, .. })
157-
if should_support_slices && matches!(&**slice, Expr::Slice(_)) =>
158-
{
151+
Expr::Subscript(ast::ExprSubscript { slice, .. }) if matches!(&**slice, Expr::Slice(_)) => {
159152
make_splat_elts(splat_element, other_elements, splat_at_left)
160153
}
161154
// If the splat element is itself a list/tuple, insert them in the other list/tuple.
@@ -202,10 +195,7 @@ pub(crate) fn collection_literal_concatenation(checker: &Checker, expr: &Expr) {
202195
return;
203196
}
204197

205-
let should_support_slices =
206-
is_support_slices_in_literal_concatenation_enabled(checker.settings);
207-
208-
let Some((new_expr, type_)) = concatenate_expressions(expr, should_support_slices) else {
198+
let Some((new_expr, type_)) = concatenate_expressions(expr) else {
209199
return;
210200
};
211201

File renamed without changes.

0 commit comments

Comments
 (0)