-
Notifications
You must be signed in to change notification settings - Fork 1.8k
add manual_sign_check
lint
#15860
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
base: master
Are you sure you want to change the base?
add manual_sign_check
lint
#15860
Conversation
rustbot has assigned @samueltardieu. Use |
Lintcheck changes for a235c82
This comment will be updated if you push new changes |
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.
Looking at the lintcheck result shows some inconsistencies:
345 | Value::F32(value) => Value::F32(if value < 0. { -value } else { value }),
| ^^^^^^^^^^ help: consider using `is_sign_negative` for clarity and performance: `value.is_sign_negative()`
Not equivalent: if value
is -0.0
, the initial test will evaluate to false
while .is_sign_negative()
will evaluate to true
166 | if n >= 0.0 {
| ^^^^^^^^ help: consider using `is_sign_negative` for clarity and performance: `!n.is_sign_negative()`
Why not n.is_sign_positive()
?
361 | if fragments[j - 1].penalty_width() > 0.0 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `is_sign_positive` for clarity and performance: `fragments[j - 1].penalty_width().is_sign_positive()`
Not equivalent: if the expression evaluates to 0.0
, the comparaison will evaluate to false
while is_sign_positive()
will evaluate to true
You should add more tests, including with edge cases. I suggest that you use a const fn
that evaluates expressions such as:
const fn check_properties(x: f32) {
assert!((x >= 0.0) == (x.is_positive());
assert!((x <= -0.0) == (x.is_negative());
// … include other tests here, with conditions reversed, etc.
}
fn main() {
const {
check_properties(0.0);
check_properties(-0.0);
check_properties(1.0);
check_properties(-1.0);
check_properties(f32::INFINITY);
check_properties(f32::NEG_INFINITY);
check_properties(f32::NAN);
check_properties(f32::MIN);
check_properties(f32::MAX);
}
}
to make sure that expressions are really equivalent, both in the test file and the fixed file, as it will not compile if expressions are not equivalent.
This is a caveat that is noted in the documentation both for the lint and the method itself. I'm not sure there's any way around this, and working with negative zero seems like an edge case anyways? |
Also thank you for the feedback—this is my first time contributing to this repo and to rust in general and I appreciate the helpfulness :) |
In what is admittedly somewhat of an inconsistency, this is kind of bringing -0.0 into consideration— |
In this case, it cannot be in the |
changelog: [
manual_sign_check
]: add lintLints on things like
f < 0
and0 <= f
for floats, recommending the use off*::is_positive
/f*::is_negative
instead. Added to pedantic group.