1 use crate::utils::span_help_and_lint;
2 use if_chain::if_chain;
3 use rustc::declare_lint_pass;
5 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
6 use rustc_session::declare_tool_lint;
9 /// **What it does:** Checks for division of integers
11 /// **Why is this bad?** When outside of some very specific algorithms,
12 /// integer division is very often a mistake because it discards the
15 /// **Known problems:** None.
21 /// println!("{}", x);
26 "integer division may cause loss of precision"
29 declare_lint_pass!(IntegerDivision => [INTEGER_DIVISION]);
31 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IntegerDivision {
32 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
33 if is_integer_division(cx, expr) {
39 "division of integers may cause loss of precision. consider using floats.",
45 fn is_integer_division<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) -> bool {
47 if let hir::ExprKind::Binary(binop, left, right) = &expr.kind;
48 if let hir::BinOpKind::Div = &binop.node;
50 let (left_ty, right_ty) = (cx.tables.expr_ty(left), cx.tables.expr_ty(right));
51 return left_ty.is_integral() && right_ty.is_integral();