Skip to content

Commit f1a552c

Browse files
committed
Auto merge of rust-lang#10203 - c410-f3r:macro-lint, r=giraffate
Suppress the triggering of some lints in derived structures Fixes rust-lang#10185 Fixes rust-lang#10417 For `integer_arithmetic`, `arithmetic_side_effects` and `shadow_reuse`. * ~~Not sure how to test these use-cases so feel free to point any method or any related PR.~~ --- changelog: FP: [`integer_arithmetic`], [`arithmetic_side_effects`]: No longer lint inside proc macros [rust-lang#10203](rust-lang/rust-clippy#10203) <!-- changelog_checked -->
2 parents 0c44586 + d639062 commit f1a552c

9 files changed

+286
-186
lines changed

clippy_lints/src/operators/arithmetic_side_effects.rs

+3-1
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

+8-2
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>,
@@ -47,6 +47,9 @@ impl Context {
4747

4848
let (l_ty, r_ty) = (cx.typeck_results().expr_ty(l), cx.typeck_results().expr_ty(r));
4949
if l_ty.peel_refs().is_integral() && r_ty.peel_refs().is_integral() {
50+
if is_from_proc_macro(cx, expr) {
51+
return;
52+
}
5053
match op {
5154
hir::BinOpKind::Div | hir::BinOpKind::Rem => match &r.kind {
5255
hir::ExprKind::Lit(_lit) => (),
@@ -79,6 +82,9 @@ impl Context {
7982
let ty = cx.typeck_results().expr_ty(arg);
8083
if constant_simple(cx, cx.typeck_results(), expr).is_none() {
8184
if ty.is_integral() {
85+
if is_from_proc_macro(cx, expr) {
86+
return;
87+
}
8288
span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
8389
self.expr_id = Some(expr.hir_id);
8490
} else if ty.is_floating_point() {

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)