Skip to content

Commit feccf1b

Browse files
committed
Suppress the triggering of some lints in derived structures
1 parent 97fb130 commit feccf1b

10 files changed

+237
-177
lines changed

clippy_lints/src/operators/arithmetic_side_effects.rs

Lines changed: 7 additions & 0 deletions
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},
45
diagnostics::span_lint,
@@ -120,6 +121,9 @@ impl ArithmeticSideEffects {
120121
lhs: &hir::Expr<'tcx>,
121122
rhs: &hir::Expr<'tcx>,
122123
) {
124+
if is_from_proc_macro(cx, expr) {
125+
return;
126+
}
123127
if constant_simple(cx, cx.typeck_results(), expr).is_some() {
124128
return;
125129
}
@@ -171,6 +175,9 @@ impl ArithmeticSideEffects {
171175
un_expr: &hir::Expr<'tcx>,
172176
un_op: hir::UnOp,
173177
) {
178+
if is_from_proc_macro(cx, expr) {
179+
return;
180+
}
174181
let hir::UnOp::Neg = un_op else { return; };
175182
if constant(cx, cx.typeck_results(), un_expr).is_some() {
176183
return;

clippy_lints/src/operators/numeric_arithmetic.rs

Lines changed: 8 additions & 5 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>,
@@ -15,7 +15,10 @@ pub struct Context {
1515
const_span: Option<Span>,
1616
}
1717
impl Context {
18-
fn skip_expr(&mut self, e: &hir::Expr<'_>) -> bool {
18+
fn skip_expr<'tcx>(&mut self, cx: &LateContext<'tcx>, e: &'tcx hir::Expr<'_>) -> bool {
19+
if is_from_proc_macro(cx, e) {
20+
return true;
21+
}
1922
self.expr_id.is_some() || self.const_span.map_or(false, |span| span.contains(e.span))
2023
}
2124

@@ -27,7 +30,7 @@ impl Context {
2730
l: &'tcx hir::Expr<'_>,
2831
r: &'tcx hir::Expr<'_>,
2932
) {
30-
if self.skip_expr(expr) {
33+
if self.skip_expr(cx, expr) {
3134
return;
3235
}
3336
match op {
@@ -73,7 +76,7 @@ impl Context {
7376
}
7477

7578
pub fn check_negate<'tcx>(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, arg: &'tcx hir::Expr<'_>) {
76-
if self.skip_expr(expr) {
79+
if self.skip_expr(cx, expr) {
7780
return;
7881
}
7982
let ty = cx.typeck_results().expr_ty(arg);

clippy_lints/src/shadow.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use clippy_utils::any_parent_is_automatically_derived;
12
use clippy_utils::diagnostics::span_lint_and_note;
23
use clippy_utils::source::snippet;
34
use clippy_utils::visitors::is_local_used;
@@ -170,6 +171,9 @@ fn is_shadow(cx: &LateContext<'_>, owner: LocalDefId, first: ItemLocalId, second
170171
}
171172

172173
fn lint_shadow(cx: &LateContext<'_>, pat: &Pat<'_>, shadowed: HirId, span: Span) {
174+
if any_parent_is_automatically_derived(cx.tcx, pat.hir_id) {
175+
return;
176+
}
173177
let (lint, msg) = match find_init(cx, pat.hir_id) {
174178
Some(expr) if is_self_shadow(cx, pat, expr, shadowed) => {
175179
let msg = format!(

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,11 +13,16 @@
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
#[derive(Clone, Copy)]
1721
pub struct Custom;
1822

23+
#[derive(proc_macro_derive::ShadowDerive)]
24+
pub struct Nothing;
25+
1926
macro_rules! impl_arith {
2027
( $( $_trait:ident, $lhs:ty, $rhs:ty, $method:ident; )* ) => {
2128
$(

0 commit comments

Comments
 (0)