Suppress the triggering of some lints in derived structures

This commit is contained in:
Caio 2023-04-09 08:16:20 -03:00
parent e903af506f
commit cd0009eb30
9 changed files with 349 additions and 245 deletions

View file

@ -1,4 +1,5 @@
use super::ARITHMETIC_SIDE_EFFECTS;
use clippy_utils::is_from_proc_macro;
use clippy_utils::{
consts::{constant, constant_simple, Constant},
diagnostics::span_lint,
@ -206,8 +207,9 @@ impl ArithmeticSideEffects {
self.issue_lint(cx, expr);
}
fn should_skip_expr(&mut self, cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> bool {
fn should_skip_expr<'tcx>(&mut self, cx: &LateContext<'tcx>, expr: &hir::Expr<'tcx>) -> bool {
is_lint_allowed(cx, ARITHMETIC_SIDE_EFFECTS, expr.hir_id)
|| is_from_proc_macro(cx, expr)
|| self.expr_span.is_some()
|| self.const_span.map_or(false, |sp| sp.contains(expr.span))
}

View file

@ -1,12 +1,12 @@
use super::{FLOAT_ARITHMETIC, INTEGER_ARITHMETIC};
use clippy_utils::consts::constant_simple;
use clippy_utils::diagnostics::span_lint;
use clippy_utils::is_from_proc_macro;
use clippy_utils::is_integer_literal;
use rustc_hir as hir;
use rustc_lint::LateContext;
use rustc_span::source_map::Span;
use super::{FLOAT_ARITHMETIC, INTEGER_ARITHMETIC};
#[derive(Default)]
pub struct Context {
expr_id: Option<hir::HirId>,
@ -19,6 +19,10 @@ impl Context {
self.expr_id.is_some() || self.const_span.map_or(false, |span| span.contains(e.span))
}
fn skip_expr_involving_integers<'tcx>(cx: &LateContext<'tcx>, e: &hir::Expr<'tcx>) -> bool {
is_from_proc_macro(cx, e)
}
pub fn check_binary<'tcx>(
&mut self,
cx: &LateContext<'tcx>,
@ -47,6 +51,9 @@ impl Context {
let (l_ty, r_ty) = (cx.typeck_results().expr_ty(l), cx.typeck_results().expr_ty(r));
if l_ty.peel_refs().is_integral() && r_ty.peel_refs().is_integral() {
if Self::skip_expr_involving_integers(cx, expr) {
return;
}
match op {
hir::BinOpKind::Div | hir::BinOpKind::Rem => match &r.kind {
hir::ExprKind::Lit(_lit) => (),
@ -79,6 +86,9 @@ impl Context {
let ty = cx.typeck_results().expr_ty(arg);
if constant_simple(cx, cx.typeck_results(), expr).is_none() {
if ty.is_integral() {
if Self::skip_expr_involving_integers(cx, expr) {
return;
}
span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
self.expr_id = Some(expr.hir_id);
} else if ty.is_floating_point() {