Merge pull request #2203 from clippered/float_cmp_const
Fix #1142 float constant comparison lint
This commit is contained in:
commit
c5fa86da04
4 changed files with 169 additions and 2 deletions
|
|
@ -367,6 +367,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
|
|||
arithmetic::INTEGER_ARITHMETIC,
|
||||
array_indexing::INDEXING_SLICING,
|
||||
assign_ops::ASSIGN_OPS,
|
||||
misc::FLOAT_CMP_CONST,
|
||||
]);
|
||||
|
||||
reg.register_lint_group("clippy_pedantic", vec.
|
||||
///
|
||||
/// **Known problems:** None.
|
||||
///
|
||||
/// **Example:**
|
||||
/// ```rust
|
||||
/// const ONE == 1.00f64
|
||||
/// x == ONE // where both are floats
|
||||
/// ```
|
||||
declare_restriction_lint! {
|
||||
pub FLOAT_CMP_CONST,
|
||||
"using `==` or `!=` on float constants instead of comparing difference with an epsilon"
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct Pass;
|
||||
|
||||
|
|
@ -214,7 +236,8 @@ impl LintPass for Pass {
|
|||
REDUNDANT_PATTERN,
|
||||
USED_UNDERSCORE_BINDING,
|
||||
SHORT_CIRCUIT_STATEMENT,
|
||||
ZERO_PTR
|
||||
ZERO_PTR,
|
||||
FLOAT_CMP_CONST
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -334,7 +357,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
|
|||
return;
|
||||
}
|
||||
}
|
||||
span_lint_and_then(cx, FLOAT_CMP, expr.span, "strict comparison of f32 or f64", |db| {
|
||||
let (lint, msg) = if is_named_constant(cx, left) || is_named_constant(cx, right) {
|
||||
(FLOAT_CMP_CONST, "strict comparison of f32 or f64 constant")
|
||||
} else {
|
||||
(FLOAT_CMP, "strict comparison of f32 or f64")
|
||||
};
|
||||
span_lint_and_then(cx, lint, expr.span, msg, |db| {
|
||||
let lhs = Sugg::hir(cx, left, "..");
|
||||
let rhs = Sugg::hir(cx, right, "..");
|
||||
|
||||
|
|
@ -423,6 +451,14 @@ fn check_nan(cx: &LateContext, path: &Path, expr: &Expr) {
|
|||
}
|
||||
}
|
||||
|
||||
fn is_named_constant<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) -> bool {
|
||||
if let Some((_, res)) = constant(cx, expr) {
|
||||
res
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
fn is_allowed<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) -> bool {
|
||||
let parent_item = cx.tcx.hir.get_parent(expr.id);
|
||||
let parent_def_id = cx.tcx.hir.local_def_id(parent_item);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue