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

Fix float_cmp false positive when comparing signum #4275

Merged
merged 2 commits into from
Jul 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions clippy_lints/src/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MiscLints {
if is_allowed(cx, left) || is_allowed(cx, right) {
return;
}

// Allow comparing the results of signum()
if is_signum(cx, left) && is_signum(cx, right) {
return;
}

if let Some(name) = get_item_name(cx, expr) {
let name = name.as_str();
if name == "eq"
Expand Down Expand Up @@ -493,6 +499,25 @@ fn is_allowed<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) -> bool {
}
}

// Return true if `expr` is the result of `signum()` invoked on a float value.
fn is_signum(cx: &LateContext<'_, '_>, expr: &Expr) -> bool {
// The negation of a signum is still a signum
Copy link
Member

Choose a reason for hiding this comment

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

Nice, good catch!

Lint idea (for another PR):

// for
f1.signum() == -f2.signum()
// suggest
f1.signum() != f2.signum()

Same goes for -sig==+sig and -sig==-sig and the != variants.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! I'm not sure that's the best way to simplify f1.signum() == -f2.signum(), since if both f1 and f2 are 0, then also f1.signum() == f2.signum() == 0.

Copy link
Member

Choose a reason for hiding this comment

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

Oh your right, in this case we can't really lint this. Never mind then :D

if let ExprKind::Unary(UnNeg, ref child_expr) = expr.node {
return is_signum(cx, &child_expr);
}

if_chain! {
if let ExprKind::MethodCall(ref method_name, _, ref expressions) = expr.node;
if sym!(signum) == method_name.ident.name;
// Check that the receiver of the signum() is a float (expressions[0] is the receiver of
// the method call)
then {
return is_float(cx, &expressions[0]);
}
}
false
}

fn is_float(cx: &LateContext<'_, '_>, expr: &Expr) -> bool {
matches!(walk_ptrs_ty(cx.tables.expr_ty(expr)).sty, ty::Float(_))
}
Expand Down
19 changes: 19 additions & 0 deletions tests/ui/float_cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,23 @@ fn main() {
let b: *const f32 = xs.as_ptr();

assert_eq!(a, b); // no errors

// no errors - comparing signums is ok
let x32 = 3.21f32;
1.23f32.signum() == x32.signum();
1.23f32.signum() == -(x32.signum());
1.23f32.signum() == 3.21f32.signum();

1.23f32.signum() != x32.signum();
1.23f32.signum() != -(x32.signum());
1.23f32.signum() != 3.21f32.signum();

let x64 = 3.21f64;
1.23f64.signum() == x64.signum();
1.23f64.signum() == -(x64.signum());
1.23f64.signum() == 3.21f64.signum();

1.23f64.signum() != x64.signum();
1.23f64.signum() != -(x64.signum());
1.23f64.signum() != 3.21f64.signum();
}