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

Detect usage of custom floating-point abs implementation #5224

Closed
krishna-veerareddy opened this issue Feb 25, 2020 · 3 comments · Fixed by #5246
Closed

Detect usage of custom floating-point abs implementation #5224

krishna-veerareddy opened this issue Feb 25, 2020 · 3 comments · Fixed by #5246
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy L-complexity Lint: Belongs in the complexity lint group

Comments

@krishna-veerareddy
Copy link
Contributor

krishna-veerareddy commented Feb 25, 2020

Detect usage of

if a < 0.0 { // or a.is_sign_negative()
    -a
} else {
    a
}

and

if a > 0.0 { // or a.is_sign_positive()
    a
} else {
    -a
}

and suggest usage of abs instead

a.abs()

Using abs is more efficient, handles more cases and is more succinct than using the if/else block so this lint could go under the suboptimal_flops lint. If this seems like an obvious thing and isn't worth the check then I'd be happy to close it.

@flip1995
Copy link
Member

This should also lint the opposite case:

if a < 0.0 { // or a.is_sign_negative()
    a
} else {
    -a
}

and suggest -a.abs()

@flip1995 flip1995 added L-complexity Lint: Belongs in the complexity lint group good-first-issue These issues are a good way to get started with Clippy A-lint Area: New lints labels Feb 25, 2020
@JarredAllen
Copy link
Contributor

I can work on this. This seems like a good point to begin contributing.

@flip1995
Copy link
Member

Thanks! If you have questions, just ask here or open a WIP PR. A good read for starting writing lints is this documentation: https://github.com/rust-lang/rust-clippy/blob/master/doc/adding_lints.md

bors added a commit that referenced this issue Mar 4, 2020
Detect usage of custom floating-point abs implementation

Implements #5224
@bors bors closed this as completed in 8dc3fde Mar 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy L-complexity Lint: Belongs in the complexity lint group
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants