-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[pylint] Fix PL1730: min/max auto-fix and suggestion #15930
[pylint] Fix PL1730: min/max auto-fix and suggestion #15930
Conversation
|
Co-authored-by: Micha Reiser <[email protected]>
@MichaReiser I implemented your suggestion ;-) Let me know if you like it... |
There was a problem hiding this comment.
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.
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, | ||
}; | ||
|
There was a problem hiding this comment.
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)
};
There was a problem hiding this comment.
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 ;-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, thank you
Summary
The PR addresses the issue #15887
For two objects
a
andb
, 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:min
andmax
. See the above issue for more details.