-
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
Implement FURB136 #8664
Implement FURB136 #8664
Conversation
This implementation diverges from Refurb's original implementation by retaining the order of equal values. For example, Refurb suggest that the following expressions: highest_score1 = score1 if score1 > score2 else score2 highest_score2 = score1 if score1 >= score2 else score2 should be to rewritten as: highest_score1 = max(score1, score2) highest_score2 = max(score1, score2) whereas this implementation provides more correct alternatives: highest_score1 = max(score2, score1) highest_score2 = max(score1, score2)
|
code | total | + violation | - violation | + fix | - fix |
---|---|---|---|---|---|
FURB136 | 1 | 1 | 0 | 0 | 0 |
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, this looks great! I may make some small tweaks to better conform to some of our idioms but shouldn't require any major changes.
@@ -1301,6 +1301,9 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) { | |||
if checker.enabled(Rule::IfExprWithTwistedArms) { | |||
flake8_simplify::rules::twisted_arms_in_ifexpr(checker, expr, test, body, orelse); | |||
} | |||
if checker.enabled(Rule::IfExprMinMax) { | |||
refurb::rules::if_expr_min_max(checker, expr, test, body, orelse); |
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.
Can you instead pass in the ast::ExprIfExp
here? That's what we prefer for new rules. So e.g. on line 1284, you'd change to:
Expr::IfExp(if_exp @ ast::ExprIfExp {
test,
body,
orelse,
range: _,
}) => {
...
}
And then here, you'd pass in if_exp
instead of the destructured fields. That way, callers can't pass in the "wrong" values.
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.
Good point!
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!
Summary
Implements FURB136 that checks for
if
expressions that can be replaced withmin()
ormax()
calls. See issue #1348 for more information.This implementation diverges from Refurb's original implementation by retaining the order of equal values. For example, Refurb suggest that the following expressions:
should be to rewritten as:
whereas this implementation provides more correct alternatives:
Test Plan
Unit test checks all eight possibilities.