[pylint] Fix PL1730: min/max auto-fix and suggestion - #15930
Merged
MichaReiser merged 5 commits intoFeb 5, 2025
Conversation
Contributor
|
MichaReiser
reviewed
Feb 4, 2025
Co-authored-by: Micha Reiser <micha@reiser.io>
Contributor
Author
|
@MichaReiser I implemented your suggestion ;-) Let me know if you like it... |
MichaReiser
reviewed
Feb 5, 2025
MichaReiser
left a comment
Member
There was a problem hiding this comment.
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, | ||
| }; | ||
|
|
Member
There was a problem hiding this comment.
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)
};
Contributor
Author
There was a problem hiding this comment.
Yes... I did the changes ;-)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The PR addresses the issue #15887
For two objects
aandb, we ensure that the auto-fix and the suggestion is of the forma = min(a, b)(ora = max(a, b)). This is because we want to be consistent with the python implementation of the methods:minandmax. See the above issue for more details.