]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/operators/modulo_one.rs
Rollup merge of #103305 - c410-f3r:moar-errors, r=petrochenkov
[rust.git] / src / tools / clippy / clippy_lints / src / operators / modulo_one.rs
1 use clippy_utils::diagnostics::span_lint;
2 use clippy_utils::{is_integer_const, unsext};
3 use rustc_hir::{BinOpKind, Expr};
4 use rustc_lint::LateContext;
5 use rustc_middle::ty;
6
7 use super::MODULO_ONE;
8
9 pub(crate) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, op: BinOpKind, right: &Expr<'_>) {
10     if op == BinOpKind::Rem {
11         if is_integer_const(cx, right, 1) {
12             span_lint(cx, MODULO_ONE, expr.span, "any number modulo 1 will be 0");
13         }
14
15         if let ty::Int(ity) = cx.typeck_results().expr_ty(right).kind() {
16             if is_integer_const(cx, right, unsext(cx.tcx, -1, *ity)) {
17                 span_lint(
18                     cx,
19                     MODULO_ONE,
20                     expr.span,
21                     "any number modulo -1 will panic/overflow or result in 0",
22                 );
23             }
24         };
25     }
26 }