mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-17 22:08:28 +00:00
[pylint
] Fix PLW3301
auto-fix with generators (#4412)
This commit is contained in:
parent
0a68636de3
commit
a0258f2205
3 changed files with 93 additions and 0 deletions
|
@ -19,3 +19,9 @@ min(
|
|||
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)))
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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)))
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue