-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
float_cmp
changes
#11948
base: master
Are you sure you want to change the base?
float_cmp
changes
#11948
Conversation
r? @giraffate (rustbot has picked a reviewer for you, use r? to override) |
☔ The latest upstream changes (presumably #10283) made this pull request unmergeable. Please resolve the merge conflicts. |
147961e
to
e0a1fc6
Compare
r? clippy |
☔ The latest upstream changes (presumably #12306) made this pull request unmergeable. Please resolve the merge conflicts. |
Hey @Jarcho woudl you mind rebasing this PR? Hey @GuillaumeGomez, if you have the time, could you give this PR a review? r? xFrednet |
#![deny(clippy::float_cmp)] | ||
|
||
fn main() { | ||
{ |
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.
Could you move the const C
outside of the main
function and also move both f
functions out too please? Having nested levels like this make the code reading a bit more complicated than it could be.
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.
Nested blocks both move the code for each test closer together (as in, everything needed for each test is all in one spot). and prevents name collisions between tests.
if matches!(op, BinOpKind::Eq | BinOpKind::Ne) | ||
&& let left = peel_hir_expr_while(left, peel_expr) | ||
&& let right = peel_hir_expr_while(right, peel_expr) | ||
&& is_float(cx, 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.
What do you think about:
if matches!(op, BinOpKind::Eq | BinOpKind::Ne) | |
&& let left = peel_hir_expr_while(left, peel_expr) | |
&& let right = peel_hir_expr_while(right, peel_expr) | |
&& is_float(cx, left) | |
if !matches(op, BinOpKind::Eq | BinOpKind::Ne) { | |
return; | |
} | |
let left = peel_hir_expr_while(left, peel_expr); | |
let right = peel_hir_expr_while(right, peel_expr); | |
if is_float(cx, 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.
Let chains with infallible bindings are used all over Clippy.
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 agree with @Jarcho, this is the normal way and readable, IMO.
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.
@Jarcho, if you rebase I can review this PR sometime this week. If you say it's ready
@@ -15,4 +15,9 @@ fn main() { | |||
} | |||
let _ = f(1.0) == f(2.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.
No need to put it into a nested block. However, a comment explaining that this is checking that the lint is not emitted for literal comparisons would be nice. 👍
@@ -105,6 +113,78 @@ fn is_allowed(val: &Constant<'_>) -> bool { | |||
} | |||
} | |||
|
|||
// This is a best effort guess and may have false positives and negatives. | |||
fn is_pure_expr<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> bool { |
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.
What are you trying to check here exactly? What is a "pure expression"?
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.
No side effects.
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.
Then please add a comment explaining that on the function.
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.
Functional purity isn't a weird concept. It's also mentioned casually in comments a few times in other places.
|
||
#![deny(clippy::float_cmp)] | ||
|
||
fn main() { |
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 you should add checks with closures as well.
@@ -64,7 +69,7 @@ pub(crate) fn check<'tcx>( | |||
|
|||
if let Some(name) = get_item_name(cx, expr) { | |||
let name = name.as_str(); | |||
if name == "eq" || name == "ne" || name == "is_nan" || name.starts_with("eq_") || name.ends_with("_eq") { | |||
if name == "eq" || name == "ne" || name.starts_with("eq_") || name.ends_with("_eq") { |
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.
The name.starts_with("eq_") || name.ends_with("_eq")
part is worrying me. Is it supposed to be for PartialEq
methods? If so, please check the full name and that it is from PartialEq
(same for eq
and ne
).
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.
That isn't even added in this PR.
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.
Indeed but the worry stays. Is there already a test for it by any chance? If so just resolve this conversation please.
f768816
to
b6623ac
Compare
☔ The latest upstream changes (presumably #12999) made this pull request unmergeable. Please resolve the merge conflicts. |
fixes #2834
fixes #6816
changelog:
float_cmp_const
: Deprecate in favor of a config option onfloat_cmp
.changelog:
float_cmp
: Don't lint self comparisons.changelog:
float_cmp
: Don't lint literal comparisons.changelog:
float_cmp
: Add a config option to ignore comparisons to named constants.changelog:
float_cmp
: Add a config option to ignore const-evaluatable comparisons.changelog:
float_cmp
: Add a config option to ignore comparisons to the modification of an operand (e.g.x == f(x)
).