-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Detect usage of custom floating-point abs implementation #5246
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ce0dc9b
Created floating point abs lint and test, but not yet run
JarredAllen 5a21661
Some bugfixing
JarredAllen 028cddb
Finished checking for cases of absolute values
JarredAllen bfa2691
Run cargo dev fmt
JarredAllen ee73972
Changed test output to reflect cargo fmt
JarredAllen 91a1cd5
Merge branch 'master' of github.com:rust-lang/rust-clippy
JarredAllen 0a6d299
Fixed compile error from merging
JarredAllen d887503
Refactor suggested by krishna-veerareddy
JarredAllen 0d584f3
Fix one last test issue
JarredAllen f8e949f
Recommended changes from flip1995
JarredAllen fe342f3
Ran cargo dev fmt
JarredAllen c3e96d1
Update test case answers to match cargo dev fmt
JarredAllen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// run-rustfix | ||
#![warn(clippy::suboptimal_flops)] | ||
|
||
struct A { | ||
a: f64, | ||
b: f64, | ||
} | ||
|
||
fn fake_abs1(num: f64) -> f64 { | ||
num.abs() | ||
} | ||
|
||
fn fake_abs2(num: f64) -> f64 { | ||
num.abs() | ||
} | ||
|
||
fn fake_abs3(a: A) -> f64 { | ||
a.a.abs() | ||
} | ||
|
||
fn fake_abs4(num: f64) -> f64 { | ||
num.abs() | ||
} | ||
|
||
fn fake_abs5(a: A) -> f64 { | ||
a.a.abs() | ||
} | ||
|
||
fn fake_nabs1(num: f64) -> f64 { | ||
-num.abs() | ||
} | ||
|
||
fn fake_nabs2(num: f64) -> f64 { | ||
-num.abs() | ||
} | ||
|
||
fn fake_nabs3(a: A) -> A { | ||
A { | ||
a: -a.a.abs(), | ||
b: a.b, | ||
} | ||
} | ||
|
||
fn not_fake_abs1(num: f64) -> f64 { | ||
if num > 0.0 { | ||
num | ||
} else { | ||
-num - 1f64 | ||
} | ||
} | ||
|
||
fn not_fake_abs2(num: f64) -> f64 { | ||
if num > 0.0 { | ||
num + 1.0 | ||
} else { | ||
-(num + 1.0) | ||
} | ||
} | ||
|
||
fn not_fake_abs3(num1: f64, num2: f64) -> f64 { | ||
if num1 > 0.0 { | ||
num2 | ||
} else { | ||
-num2 | ||
} | ||
} | ||
|
||
fn not_fake_abs4(a: A) -> f64 { | ||
if a.a > 0.0 { | ||
a.b | ||
} else { | ||
-a.b | ||
} | ||
} | ||
|
||
fn not_fake_abs5(a: A) -> f64 { | ||
if a.a > 0.0 { | ||
a.a | ||
} else { | ||
-a.b | ||
} | ||
} | ||
|
||
fn main() { | ||
fake_abs1(5.0); | ||
fake_abs2(5.0); | ||
fake_abs3(A { a: 5.0, b: 5.0 }); | ||
fake_abs4(5.0); | ||
fake_abs5(A { a: 5.0, b: 5.0 }); | ||
fake_nabs1(5.0); | ||
fake_nabs2(5.0); | ||
fake_nabs3(A { a: 5.0, b: 5.0 }); | ||
not_fake_abs1(5.0); | ||
not_fake_abs2(5.0); | ||
not_fake_abs3(5.0, 5.0); | ||
not_fake_abs4(A { a: 5.0, b: 5.0 }); | ||
not_fake_abs5(A { a: 5.0, b: 5.0 }); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// run-rustfix | ||
#![warn(clippy::suboptimal_flops)] | ||
JarredAllen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
struct A { | ||
a: f64, | ||
b: f64, | ||
} | ||
|
||
fn fake_abs1(num: f64) -> f64 { | ||
if num >= 0.0 { | ||
num | ||
} else { | ||
-num | ||
} | ||
} | ||
|
||
fn fake_abs2(num: f64) -> f64 { | ||
if 0.0 < num { | ||
num | ||
} else { | ||
-num | ||
} | ||
} | ||
|
||
fn fake_abs3(a: A) -> f64 { | ||
if a.a > 0.0 { | ||
a.a | ||
} else { | ||
-a.a | ||
} | ||
} | ||
|
||
fn fake_abs4(num: f64) -> f64 { | ||
if 0.0 >= num { | ||
-num | ||
} else { | ||
num | ||
} | ||
} | ||
|
||
fn fake_abs5(a: A) -> f64 { | ||
if a.a < 0.0 { | ||
-a.a | ||
} else { | ||
a.a | ||
} | ||
} | ||
|
||
fn fake_nabs1(num: f64) -> f64 { | ||
if num < 0.0 { | ||
num | ||
} else { | ||
-num | ||
} | ||
} | ||
|
||
fn fake_nabs2(num: f64) -> f64 { | ||
if 0.0 >= num { | ||
num | ||
} else { | ||
-num | ||
} | ||
} | ||
|
||
fn fake_nabs3(a: A) -> A { | ||
A { | ||
a: if a.a >= 0.0 { -a.a } else { a.a }, | ||
b: a.b, | ||
} | ||
} | ||
|
||
fn not_fake_abs1(num: f64) -> f64 { | ||
if num > 0.0 { | ||
num | ||
} else { | ||
-num - 1f64 | ||
} | ||
} | ||
|
||
fn not_fake_abs2(num: f64) -> f64 { | ||
if num > 0.0 { | ||
num + 1.0 | ||
} else { | ||
-(num + 1.0) | ||
} | ||
} | ||
|
||
fn not_fake_abs3(num1: f64, num2: f64) -> f64 { | ||
if num1 > 0.0 { | ||
num2 | ||
} else { | ||
-num2 | ||
} | ||
} | ||
|
||
fn not_fake_abs4(a: A) -> f64 { | ||
if a.a > 0.0 { | ||
a.b | ||
} else { | ||
-a.b | ||
} | ||
} | ||
|
||
fn not_fake_abs5(a: A) -> f64 { | ||
if a.a > 0.0 { | ||
a.a | ||
} else { | ||
-a.b | ||
} | ||
} | ||
|
||
fn main() { | ||
fake_abs1(5.0); | ||
fake_abs2(5.0); | ||
fake_abs3(A { a: 5.0, b: 5.0 }); | ||
fake_abs4(5.0); | ||
fake_abs5(A { a: 5.0, b: 5.0 }); | ||
fake_nabs1(5.0); | ||
fake_nabs2(5.0); | ||
fake_nabs3(A { a: 5.0, b: 5.0 }); | ||
not_fake_abs1(5.0); | ||
not_fake_abs2(5.0); | ||
not_fake_abs3(5.0, 5.0); | ||
not_fake_abs4(A { a: 5.0, b: 5.0 }); | ||
not_fake_abs5(A { a: 5.0, b: 5.0 }); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.