Skip to content
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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

float_cmp changes #11948

wants to merge 6 commits into from

Conversation

Jarcho
Copy link
Contributor

@Jarcho Jarcho commented Dec 10, 2023

fixes #2834
fixes #6816

changelog: float_cmp_const: Deprecate in favor of a config option on float_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)).

@rustbot
Copy link
Collaborator

rustbot commented Dec 10, 2023

r? @giraffate

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Dec 10, 2023
@bors
Copy link
Collaborator

bors commented Dec 29, 2023

☔ The latest upstream changes (presumably #10283) made this pull request unmergeable. Please resolve the merge conflicts.

@Jarcho
Copy link
Contributor Author

Jarcho commented Feb 14, 2024

r? clippy

@bors
Copy link
Collaborator

bors commented Feb 19, 2024

☔ The latest upstream changes (presumably #12306) made this pull request unmergeable. Please resolve the merge conflicts.

@xFrednet
Copy link
Member

xFrednet commented Apr 1, 2024

Hey @Jarcho woudl you mind rebasing this PR?

Hey @GuillaumeGomez, if you have the time, could you give this PR a review?

r? xFrednet

@rustbot rustbot assigned xFrednet and unassigned matthiaskrgr Apr 1, 2024
#![deny(clippy::float_cmp)]

fn main() {
{
Copy link
Member

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.

Copy link
Contributor Author

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.

Comment on lines 26 to 29
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)
Copy link
Member

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:

Suggested change
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)

?

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Member

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);
}
{
Copy link
Member

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 {
Copy link
Member

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"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No side effects.

Copy link
Member

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.

Copy link
Contributor Author

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() {
Copy link
Member

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") {
Copy link
Member

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).

Copy link
Contributor Author

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.

Copy link
Member

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.

@Jarcho Jarcho force-pushed the float_cmp branch 2 times, most recently from f768816 to b6623ac Compare June 7, 2024 21:47
@bors
Copy link
Collaborator

bors commented Jun 27, 2024

☔ The latest upstream changes (presumably #12999) made this pull request unmergeable. Please resolve the merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
7 participants