Skip to content

Suggest hypot for x.mul_add(x, y * y).sqrt() - #17600

Open
Gri-ffin wants to merge 3 commits into
rust-lang:masterfrom
Gri-ffin:cleanup-hypot
Open

Suggest hypot for x.mul_add(x, y * y).sqrt()#17600
Gri-ffin wants to merge 3 commits into
rust-lang:masterfrom
Gri-ffin:cleanup-hypot

Conversation

@Gri-ffin

@Gri-ffin Gri-ffin commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

changelog: [imprecise_flops]: also suggest hypot for the form x.mul_add(x, y * y).sqrt()

@rustbot rustbot added the S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. label Aug 21, 2026
@rustbot

rustbot commented Aug 21, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request. A reviewer will take a look after it receives 2 community reviews.

In the meantime, we would highly appreciate if you could try to review any of PRs waiting on community reviews.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Aug 21, 2026
@pthariensflame

Copy link
Copy Markdown

These are not equivalent mathematically; they differ by the presence/absence of a square root at the end.

@CommanderStorm CommanderStorm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Community review:
If you can clear up my sqrt confusion regarding why this is linting on this despite it not having any check for it, this LGTM.

I also would like to see a bit more testing since I think this lint might be hiding some things that are wrong, but my intuition might also be wrong here 😉

View changes since this review

Comment thread tests/ui/floating_point_hypot.rs
Comment thread clippy_lints/src/floating_point_arithmetic/hypot.rs
Comment thread tests/ui/floating_point_hypot.rs

@CommanderStorm CommanderStorm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Community review:
LGTM, sorry for my confusion in the previous review. Please make sure to add the .sqrt() in the (also) misleading PR description and title 😉

View changes since this review

Comment thread tests/ui/floating_point_hypot.rs

@hashcatHitman hashcatHitman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

community review:

I think we could be a little more confident that we have the right mul_add, but otherwise looks good to me.

View changes since this review

Comment on lines +69 to +79
if let ExprKind::MethodCall(PathSegment { ident: method, .. }, self_arg, [arg1, arg2], _) = receiver.kind
&& method.name == sym::mul_add
&& eq_expr_value(cx, ctxt, self_arg, arg1)
&& let ExprKind::Binary(
Spanned {
node: BinOpKind::Mul, ..
},
mul_lhs,
mul_rhs,
) = arg2.kind
&& eq_expr_value(cx, ctxt, mul_lhs, mul_rhs)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not actually sure if we typically expect more effort than this when checking that we're dealing with the right method, but from what I've found this seems to be pretty close to our usual "best effort".

That said, maybe it'd be nice to double check that all operands involved are the same type, and that the type in question is a floating point? Just for a little extra peace of mind. I think you can find some inspiration in clippy_lints/src/floating_point_arithmetic/expm1.rs.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes why not, but I believe testing that self_arg here is a floating point would be enough, as rustc would lock in place its specific mul_add implementation (f64 or f32... for both operands in each case).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I took a swing at breaking it as of 9acfaae and you seem to be right. Thanks!

@hashcatHitman hashcatHitman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

community review:

Looks good to me, thanks!

View changes since this review

@rustbot rustbot removed the S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. label Aug 23, 2026
@rustbot

rustbot commented Aug 23, 2026

Copy link
Copy Markdown
Collaborator

r? @Manishearth

rustbot has assigned @Manishearth for the project review.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: 9 candidates
  • 9 candidates expanded to 9 candidates
  • Random selection from Jarcho, Manishearth, blyxyas, dswij, llogiq

@Gri-ffin Gri-ffin changed the title Suggest hypot for x.mul_add(x, y * y) Suggest hypot for x.mul_add(x, y * y).sqrt() Aug 23, 2026
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

Development

Successfully merging this pull request may close these issues.

6 participants