diff --git a/crates/ruff/resources/test/fixtures/pylint/nested_min_max.py b/crates/ruff/resources/test/fixtures/pylint/nested_min_max.py index 9ae58c1241d2a..e299bb53afced 100644 --- a/crates/ruff/resources/test/fixtures/pylint/nested_min_max.py +++ b/crates/ruff/resources/test/fixtures/pylint/nested_min_max.py @@ -19,3 +19,9 @@ 1, # This is a comment. min(2, 3), ) + +# Handle iterable expressions. +min(1, min(a)) +min(1, min(i for i in range(10))) +max(1, max(a)) +max(1, max(i for i in range(10))) diff --git a/crates/ruff/src/rules/pylint/rules/nested_min_max.rs b/crates/ruff/src/rules/pylint/rules/nested_min_max.rs index ab9988a6fdc63..a2630234cb8c0 100644 --- a/crates/ruff/src/rules/pylint/rules/nested_min_max.rs +++ b/crates/ruff/src/rules/pylint/rules/nested_min_max.rs @@ -72,6 +72,17 @@ fn collect_nested_args(context: &Context, min_max: MinMax, args: &[Expr]) -> Vec keywords, }) = arg.node() { + if args.len() == 1 { + let new_arg = Expr::new( + TextRange::default(), + ast::ExprStarred { + value: Box::new(args[0].clone()), + ctx: ast::ExprContext::Load, + }, + ); + new_args.push(new_arg); + continue; + } if MinMax::try_from_call(func, keywords, context) == Some(min_max) { inner(context, min_max, args, new_args); continue; diff --git a/crates/ruff/src/rules/pylint/snapshots/ruff__rules__pylint__tests__PLW3301_nested_min_max.py.snap b/crates/ruff/src/rules/pylint/snapshots/ruff__rules__pylint__tests__PLW3301_nested_min_max.py.snap index b4f1448cc10e8..d20aeb94437da 100644 --- a/crates/ruff/src/rules/pylint/snapshots/ruff__rules__pylint__tests__PLW3301_nested_min_max.py.snap +++ b/crates/ruff/src/rules/pylint/snapshots/ruff__rules__pylint__tests__PLW3301_nested_min_max.py.snap @@ -191,7 +191,83 @@ nested_min_max.py:18:1: PLW3301 Nested `min` calls can be flattened 21 | | min(2, 3), 22 | | ) | |_^ PLW3301 +23 | +24 | # Handle iterable expressions. | = help: Flatten nested `min` calls +nested_min_max.py:24:1: PLW3301 [*] Nested `min` calls can be flattened + | +24 | # Handle iterable expressions. +25 | min(1, min(a)) + | ^^^^^^^^^^^^^^ PLW3301 +26 | min(1, min(i for i in range(10))) +27 | max(1, max(a)) + | + = help: Flatten nested `min` calls + +ℹ Suggested fix +21 21 | ) +22 22 | +23 23 | # Handle iterable expressions. +24 |-min(1, min(a)) + 24 |+min(1, *a) +25 25 | min(1, min(i for i in range(10))) +26 26 | max(1, max(a)) +27 27 | max(1, max(i for i in range(10))) + +nested_min_max.py:25:1: PLW3301 [*] Nested `min` calls can be flattened + | +25 | # Handle iterable expressions. +26 | min(1, min(a)) +27 | min(1, min(i for i in range(10))) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PLW3301 +28 | max(1, max(a)) +29 | max(1, max(i for i in range(10))) + | + = help: Flatten nested `min` calls + +ℹ Suggested fix +22 22 | +23 23 | # Handle iterable expressions. +24 24 | min(1, min(a)) +25 |-min(1, min(i for i in range(10))) + 25 |+min(1, *(i for i in range(10))) +26 26 | max(1, max(a)) +27 27 | max(1, max(i for i in range(10))) + +nested_min_max.py:26:1: PLW3301 [*] Nested `max` calls can be flattened + | +26 | min(1, min(a)) +27 | min(1, min(i for i in range(10))) +28 | max(1, max(a)) + | ^^^^^^^^^^^^^^ PLW3301 +29 | max(1, max(i for i in range(10))) + | + = help: Flatten nested `max` calls + +ℹ Suggested fix +23 23 | # Handle iterable expressions. +24 24 | min(1, min(a)) +25 25 | min(1, min(i for i in range(10))) +26 |-max(1, max(a)) + 26 |+max(1, *a) +27 27 | max(1, max(i for i in range(10))) + +nested_min_max.py:27:1: PLW3301 [*] Nested `max` calls can be flattened + | +27 | min(1, min(i for i in range(10))) +28 | max(1, max(a)) +29 | max(1, max(i for i in range(10))) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PLW3301 + | + = help: Flatten nested `max` calls + +ℹ Suggested fix +24 24 | min(1, min(a)) +25 25 | min(1, min(i for i in range(10))) +26 26 | max(1, max(a)) +27 |-max(1, max(i for i in range(10))) + 27 |+max(1, *(i for i in range(10))) +