X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=clippy_lints%2Fsrc%2Fmodulo_arithmetic.rs;h=1414fdc1b114d6fcccc2a93a9e1ae2eb97b3e5ac;hb=6c2748211565773c297560f2edcd762565f1933a;hp=238863e8c67be6eacd6d82ad6bbb592b06bdc215;hpb=779b6aeaa62d7d832bb791c7d92ed11d43f4873a;p=rust.git diff --git a/clippy_lints/src/modulo_arithmetic.rs b/clippy_lints/src/modulo_arithmetic.rs index 238863e8c67..1414fdc1b11 100644 --- a/clippy_lints/src/modulo_arithmetic.rs +++ b/clippy_lints/src/modulo_arithmetic.rs @@ -1,14 +1,15 @@ -use crate::consts::{constant, Constant}; -use crate::utils::{sext, span_lint_and_then}; +use clippy_utils::consts::{constant, Constant}; +use clippy_utils::diagnostics::span_lint_and_then; +use clippy_utils::sext; use if_chain::if_chain; -use rustc::ty::{self}; -use rustc_hir::*; +use rustc_hir::{BinOpKind, Expr, ExprKind}; use rustc_lint::{LateContext, LateLintPass}; +use rustc_middle::ty; use rustc_session::{declare_lint_pass, declare_tool_lint}; use std::fmt::Display; declare_clippy_lint! { - /// **What it does:** Checks for modulo arithemtic. + /// **What it does:** Checks for modulo arithmetic. /// /// **Why is this bad?** The results of modulo (%) operation might differ /// depending on the language, when negative numbers are involved. @@ -36,9 +37,9 @@ struct OperandInfo { is_integral: bool, } -fn analyze_operand(operand: &Expr<'_>, cx: &LateContext<'_, '_>, expr: &Expr<'_>) -> Option { - match constant(cx, cx.tables, operand) { - Some((Constant::Int(v), _)) => match cx.tables.expr_ty(expr).kind { +fn analyze_operand(operand: &Expr<'_>, cx: &LateContext<'_>, expr: &Expr<'_>) -> Option { + match constant(cx, cx.typeck_results(), operand) { + Some((Constant::Int(v), _)) => match *cx.typeck_results().expr_ty(expr).kind() { ty::Int(ity) => { let value = sext(cx.tcx, v, ity); return Some(OperandInfo { @@ -79,8 +80,8 @@ fn might_have_negative_value(t: &ty::TyS<'_>) -> bool { t.is_signed() || t.is_floating_point() } -fn check_const_operands<'a, 'tcx>( - cx: &LateContext<'a, 'tcx>, +fn check_const_operands<'tcx>( + cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, lhs_operand: &OperandInfo, rhs_operand: &OperandInfo, @@ -95,36 +96,36 @@ fn check_const_operands<'a, 'tcx>( lhs_operand.string_representation.as_ref().unwrap(), rhs_operand.string_representation.as_ref().unwrap() ), - |db| { - db.note("double check for expected result especially when interoperating with different languages"); + |diag| { + diag.note("double check for expected result especially when interoperating with different languages"); if lhs_operand.is_integral { - db.note("or consider using `rem_euclid` or similar function"); + diag.note("or consider using `rem_euclid` or similar function"); } }, ); } } -fn check_non_const_operands<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>, operand: &Expr<'_>) { - let operand_type = cx.tables.expr_ty(operand); +fn check_non_const_operands<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, operand: &Expr<'_>) { + let operand_type = cx.typeck_results().expr_ty(operand); if might_have_negative_value(operand_type) { span_lint_and_then( cx, MODULO_ARITHMETIC, expr.span, "you are using modulo operator on types that might have different signs", - |db| { - db.note("double check for expected result especially when interoperating with different languages"); + |diag| { + diag.note("double check for expected result especially when interoperating with different languages"); if operand_type.is_integral() { - db.note("or consider using `rem_euclid` or similar function"); + diag.note("or consider using `rem_euclid` or similar function"); } }, ); } } -impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ModuloArithmetic { - fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) { +impl<'tcx> LateLintPass<'tcx> for ModuloArithmetic { + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { match &expr.kind { ExprKind::Binary(op, lhs, rhs) | ExprKind::AssignOp(op, lhs, rhs) => { if let BinOpKind::Rem = op.node {