Skip to content

Commit cd0009e

Browse files
committed
Suppress the triggering of some lints in derived structures
1 parent e903af5 commit cd0009e

9 files changed

+290
-186
lines changed

clippy_lints/src/operators/arithmetic_side_effects.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::ARITHMETIC_SIDE_EFFECTS;
2+
use clippy_utils::is_from_proc_macro;
23
use clippy_utils::{
34
consts::{constant, constant_simple, Constant},
45
diagnostics::span_lint,
@@ -206,8 +207,9 @@ impl ArithmeticSideEffects {
206207
self.issue_lint(cx, expr);
207208
}
208209

209-
fn should_skip_expr(&mut self, cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> bool {
210+
fn should_skip_expr<'tcx>(&mut self, cx: &LateContext<'tcx>, expr: &hir::Expr<'tcx>) -> bool {
210211
is_lint_allowed(cx, ARITHMETIC_SIDE_EFFECTS, expr.hir_id)
212+
|| is_from_proc_macro(cx, expr)
211213
|| self.expr_span.is_some()
212214
|| self.const_span.map_or(false, |sp| sp.contains(expr.span))
213215
}

clippy_lints/src/operators/numeric_arithmetic.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1+
use super::{FLOAT_ARITHMETIC, INTEGER_ARITHMETIC};
12
use clippy_utils::consts::constant_simple;
23
use clippy_utils::diagnostics::span_lint;
4+
use clippy_utils::is_from_proc_macro;
35
use clippy_utils::is_integer_literal;
46
use rustc_hir as hir;
57
use rustc_lint::LateContext;
68
use rustc_span::source_map::Span;
79

8-
use super::{FLOAT_ARITHMETIC, INTEGER_ARITHMETIC};
9-
1010
#[derive(Default)]
1111
pub struct Context {
1212
expr_id: Option<hir::HirId>,
@@ -19,6 +19,10 @@ impl Context {
1919
self.expr_id.is_some() || self.const_span.map_or(false, |span| span.contains(e.span))
2020
}
2121

22+
fn skip_expr_involving_integers<'tcx>(cx: &LateContext<'tcx>, e: &hir::Expr<'tcx>) -> bool {
23+
is_from_proc_macro(cx, e)
24+
}
25+
2226
pub fn check_binary<'tcx>(
2327
&mut self,
2428
cx: &LateContext<'tcx>,
@@ -47,6 +51,9 @@ impl Context {
4751

4852
let (l_ty, r_ty) = (cx.typeck_results().expr_ty(l), cx.typeck_results().expr_ty(r));
4953
if l_ty.peel_refs().is_integral() && r_ty.peel_refs().is_integral() {
54+
if Self::skip_expr_involving_integers(cx, expr) {
55+
return;
56+
}
5057
match op {
5158
hir::BinOpKind::Div | hir::BinOpKind::Rem => match &r.kind {
5259
hir::ExprKind::Lit(_lit) => (),
@@ -79,6 +86,9 @@ impl Context {
7986
let ty = cx.typeck_results().expr_ty(arg);
8087
if constant_simple(cx, cx.typeck_results(), expr).is_none() {
8188
if ty.is_integral() {
89+
if Self::skip_expr_involving_integers(cx, expr) {
90+
return;
91+
}
8292
span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
8393
self.expr_id = Some(expr.hir_id);
8494
} else if ty.is_floating_point() {

tests/ui/arithmetic_side_effects.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// aux-build:proc_macro_derive.rs
2+
13
#![allow(
24
clippy::assign_op_pattern,
35
clippy::erasing_op,
@@ -11,6 +13,8 @@
1113
#![feature(const_mut_refs, inline_const, saturating_int_impl)]
1214
#![warn(clippy::arithmetic_side_effects)]
1315

16+
extern crate proc_macro_derive;
17+
1418
use core::num::{Saturating, Wrapping};
1519

1620
const ONE: i32 = 1;
@@ -19,6 +23,9 @@ const ZERO: i32 = 0;
1923
#[derive(Clone, Copy)]
2024
pub struct Custom;
2125

26+
#[derive(proc_macro_derive::ShadowDerive)]
27+
pub struct Nothing;
28+
2229
macro_rules! impl_arith {
2330
( $( $_trait:ident, $lhs:ty, $rhs:ty, $method:ident; )* ) => {
2431
$(

0 commit comments

Comments
 (0)