Skip to content

Commit

Permalink
Fix PLW3301 false positive single argument nested min/max (#4683)
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanPlasse authored May 27, 2023
1 parent f069eb9 commit 901060f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
11 changes: 11 additions & 0 deletions crates/ruff/resources/test/fixtures/pylint/nested_min_max.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,14 @@
min(1, min(i for i in range(10)))
max(1, max(a))
max(1, max(i for i in range(10)))

tuples_list = [
(1, 2),
(2, 3),
(3, 4),
(4, 5),
(5, 6),
]

min(min(tuples_list))
max(max(tuples_list))
6 changes: 6 additions & 0 deletions crates/ruff/src/rules/pylint/rules/nested_min_max.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ pub(crate) fn nested_min_max(
return;
};

if args.len() == 1
&& matches!(&args[0], Expr::Call(ast::ExprCall { args, .. }) if args.len() == 1)
{
return;
}

if args.iter().any(|arg| {
let Expr::Call(ast::ExprCall { func, keywords, ..} )= arg else {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ nested_min_max.py:25:1: PLW3301 [*] Nested `min` calls can be flattened
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)))
28 28 |

nested_min_max.py:26:1: PLW3301 [*] Nested `max` calls can be flattened
|
Expand All @@ -253,13 +254,17 @@ nested_min_max.py:26:1: PLW3301 [*] Nested `max` calls can be flattened
26 |-max(1, max(a))
26 |+max(1, *a)
27 27 | max(1, max(i for i in range(10)))
28 28 |
29 29 | tuples_list = [

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
30 |
31 | tuples_list = [
|
= help: Flatten nested `max` calls

Expand All @@ -269,5 +274,8 @@ nested_min_max.py:27:1: PLW3301 [*] Nested `max` calls can be flattened
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)))
28 28 |
29 29 | tuples_list = [
30 30 | (1, 2),


0 comments on commit 901060f

Please sign in to comment.