Skip to content

Commit a3c047e

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

9 files changed

+290
-189
lines changed

clippy_lints/src/operators/arithmetic_side_effects.rs

+7
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,
@@ -122,6 +123,9 @@ impl ArithmeticSideEffects {
122123
lhs: &hir::Expr<'tcx>,
123124
rhs: &hir::Expr<'tcx>,
124125
) {
126+
if is_from_proc_macro(cx, expr) {
127+
return;
128+
}
125129
if constant_simple(cx, cx.typeck_results(), expr).is_some() {
126130
return;
127131
}
@@ -191,6 +195,9 @@ impl ArithmeticSideEffects {
191195
un_expr: &hir::Expr<'tcx>,
192196
un_op: hir::UnOp,
193197
) {
198+
if is_from_proc_macro(cx, expr) {
199+
return;
200+
}
194201
let hir::UnOp::Neg = un_op else { return; };
195202
if constant(cx, cx.typeck_results(), un_expr).is_some() {
196203
return;

clippy_lints/src/operators/numeric_arithmetic.rs

+8-6
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>,
@@ -15,8 +15,10 @@ pub struct Context {
1515
const_span: Option<Span>,
1616
}
1717
impl Context {
18-
fn skip_expr(&mut self, e: &hir::Expr<'_>) -> bool {
19-
self.expr_id.is_some() || self.const_span.map_or(false, |span| span.contains(e.span))
18+
fn skip_expr<'tcx>(&mut self, cx: &LateContext<'tcx>, e: &'tcx hir::Expr<'_>) -> bool {
19+
self.expr_id.is_some()
20+
|| self.const_span.map_or(false, |span| span.contains(e.span))
21+
|| is_from_proc_macro(cx, e)
2022
}
2123

2224
pub fn check_binary<'tcx>(
@@ -27,7 +29,7 @@ impl Context {
2729
l: &'tcx hir::Expr<'_>,
2830
r: &'tcx hir::Expr<'_>,
2931
) {
30-
if self.skip_expr(expr) {
32+
if self.skip_expr(cx, expr) {
3133
return;
3234
}
3335
match op {
@@ -73,7 +75,7 @@ impl Context {
7375
}
7476

7577
pub fn check_negate<'tcx>(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, arg: &'tcx hir::Expr<'_>) {
76-
if self.skip_expr(expr) {
78+
if self.skip_expr(cx, expr) {
7779
return;
7880
}
7981
let ty = cx.typeck_results().expr_ty(arg);

tests/ui/arithmetic_side_effects.rs

+7
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)