Skip to content

Commit 56f507b

Browse files
committed
Extract floating_point_operand_info() function
1 parent c6d3cde commit 56f507b

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

clippy_lints/src/modulo_arithmetic.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc::hir::*;
66
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
77
use rustc::ty::{self};
88
use rustc_session::declare_tool_lint;
9+
use std::fmt::Display;
910

1011
declare_clippy_lint! {
1112
/// **What it does:** Checks for modulo arithemtic.
@@ -57,24 +58,24 @@ fn analyze_operand(operand: &Expr, cx: &LateContext<'_, '_>, expr: &Expr) -> Opt
5758
_ => {},
5859
},
5960
Some((Constant::F32(f), _)) => {
60-
return Some(OperandInfo {
61-
string_representation: Some(format!("{:.3}", f)),
62-
is_negative: f < 0.0,
63-
is_integral: false,
64-
});
61+
return Some(floating_point_operand_info(&f));
6562
},
6663
Some((Constant::F64(f), _)) => {
67-
return Some(OperandInfo {
68-
string_representation: Some(format!("{:.3}", f)),
69-
is_negative: f < 0.0,
70-
is_integral: false,
71-
});
64+
return Some(floating_point_operand_info(&f));
7265
},
7366
_ => {},
7467
}
7568
None
7669
}
7770

71+
fn floating_point_operand_info<T: Display + PartialOrd + From<f32>>(f: &T) -> OperandInfo {
72+
OperandInfo {
73+
string_representation: Some(format!("{:.3}", *f)),
74+
is_negative: *f < 0.0.into(),
75+
is_integral: false,
76+
}
77+
}
78+
7879
fn might_have_negative_value(t: &ty::TyS<'_>) -> bool {
7980
t.is_signed() || t.is_floating_point()
8081
}

0 commit comments

Comments
 (0)