Skip to content

[pylint] Fix PL1730: min/max auto-fix and suggestion - #15930

Merged
MichaReiser merged 5 commits into
astral-sh:mainfrom
VascoSch92:fix-PLR1730-autofix-and-suggestion
Feb 5, 2025
Merged

[pylint] Fix PL1730: min/max auto-fix and suggestion#15930
MichaReiser merged 5 commits into
astral-sh:mainfrom
VascoSch92:fix-PLR1730-autofix-and-suggestion

Conversation

@VascoSch92

Copy link
Copy Markdown
Contributor

Summary

The PR addresses the issue #15887

For two objects a and b, we ensure that the auto-fix and the suggestion is of the form a = min(a, b) (or a = max(a, b)). This is because we want to be consistent with the python implementation of the methods: min and max. See the above issue for more details.

@github-actions

github-actions Bot commented Feb 4, 2025

Copy link
Copy Markdown
Contributor

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@MichaReiser MichaReiser added the fixes Related to suggested fixes for violations label Feb 4, 2025
@MichaReiser MichaReiser linked an issue Feb 4, 2025 that may be closed by this pull request
Comment thread crates/ruff_linter/src/rules/pylint/rules/if_stmt_min_max.rs Outdated
Comment thread crates/ruff_linter/src/rules/pylint/rules/if_stmt_min_max.rs Outdated
@VascoSch92

Copy link
Copy Markdown
Contributor Author

@MichaReiser I implemented your suggestion ;-) Let me know if you like it...

@MichaReiser MichaReiser left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I think we can simplify this further.

Comment on lines 129 to 151
let (min_max, arg1, arg2) = match (
left_is_target,
right_is_target,
left_is_value,
right_is_value,
) {
(true, false, false, true) => match op {
CmpOp::Lt => (MinMax::Max, true),
CmpOp::LtE => (MinMax::Max, false),
CmpOp::Gt => (MinMax::Min, true),
CmpOp::GtE => (MinMax::Min, false),
CmpOp::Lt => (MinMax::Max, left.as_ref(), right),
CmpOp::LtE => (MinMax::Max, left.as_ref(), right),
CmpOp::Gt => (MinMax::Min, left.as_ref(), right),
CmpOp::GtE => (MinMax::Min, left.as_ref(), right),
_ => return,
},
(false, true, true, false) => match op {
CmpOp::Lt => (MinMax::Min, true),
CmpOp::LtE => (MinMax::Min, false),
CmpOp::Gt => (MinMax::Max, true),
CmpOp::GtE => (MinMax::Max, false),
CmpOp::Lt => (MinMax::Min, right, left.as_ref()),
CmpOp::LtE => (MinMax::Min, right, left.as_ref()),
CmpOp::Gt => (MinMax::Max, right, left.as_ref()),
CmpOp::GtE => (MinMax::Max, right, left.as_ref()),
_ => return,
},
_ => return,
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can simplify this to (and also reduce the diff size):

    let min_max = match (
        left_is_target,
        right_is_target,
        left_is_value,
        right_is_value,
    ) {
        (true, false, false, true) => match op {
            CmpOp::Lt | CmpOp::LtE => MinMax::Max,
            CmpOp::Gt | CmpOp::GtE => MinMax::Min,
            _ => return,
        },
        (false, true, true, false) => match op {
            CmpOp::Lt | CmpOp::LtE => MinMax::Min,
            CmpOp::Gt | CmpOp::GtE => MinMax::Max,
            _ => return,
        },
        _ => return,
    };

		// Determine whether to use `min()` or `max()`, and make sure that the first
    // arg of the `min()` or `max()` method is equal to the target of the comparison.
    // This is to be consistent with the Python implementation of the methods `min()` and `max()`.
    let (arg1, arg2) = if left_is_target {
        (&**left, right)
    } else {
        (right, &**left)
    };

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes... I did the changes ;-)

@MichaReiser MichaReiser left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thank you

@MichaReiser MichaReiser added the bug Something isn't working label Feb 5, 2025
@MichaReiser
MichaReiser enabled auto-merge (squash) February 5, 2025 09:27
@MichaReiser
MichaReiser merged commit 827a076 into astral-sh:main Feb 5, 2025
@VascoSch92
VascoSch92 deleted the fix-PLR1730-autofix-and-suggestion branch February 5, 2025 09:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working fixes Related to suggested fixes for violations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PLR1730 autofixes max/mins in the wrong order

2 participants