From 901060fa96f76ba72530006b8d63b4a04b1d65e3 Mon Sep 17 00:00:00 2001 From: Jonathan Plasse <13716151+JonathanPlasse@users.noreply.github.com> Date: Sat, 27 May 2023 21:34:55 +0200 Subject: [PATCH] Fix PLW3301 false positive single argument nested min/max (#4683) --- .../resources/test/fixtures/pylint/nested_min_max.py | 11 +++++++++++ crates/ruff/src/rules/pylint/rules/nested_min_max.rs | 6 ++++++ ...les__pylint__tests__PLW3301_nested_min_max.py.snap | 8 ++++++++ 3 files changed, 25 insertions(+) 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 e299bb53afced..2c66580305c6e 100644 --- a/crates/ruff/resources/test/fixtures/pylint/nested_min_max.py +++ b/crates/ruff/resources/test/fixtures/pylint/nested_min_max.py @@ -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)) 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 a35f588b43a44..b4b3418cf9c39 100644 --- a/crates/ruff/src/rules/pylint/rules/nested_min_max.rs +++ b/crates/ruff/src/rules/pylint/rules/nested_min_max.rs @@ -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; 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 d20aeb94437da..60e730fd94ccc 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 @@ -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 | @@ -253,6 +254,8 @@ 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 | @@ -260,6 +263,8 @@ nested_min_max.py:27:1: PLW3301 [*] Nested `max` calls can be flattened 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 @@ -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),