Skip to content

Commit

Permalink
Auto merge of rust-lang#10203 - c410-f3r:macro-lint, r=giraffate
Browse files Browse the repository at this point in the history
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 -->
  • Loading branch information
bors committed Apr 19, 2023
2 parents 0c44586 + d639062 commit f1a552c
Show file tree
Hide file tree
Showing 9 changed files with 286 additions and 186 deletions.
4 changes: 3 additions & 1 deletion clippy_lints/src/operators/arithmetic_side_effects.rs
@@ -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,
Expand Down Expand Up @@ -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))
}
Expand Down
10 changes: 8 additions & 2 deletions clippy_lints/src/operators/numeric_arithmetic.rs
@@ -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>,
Expand Down Expand Up @@ -47,6 +47,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 is_from_proc_macro(cx, expr) {
return;
}
match op {
hir::BinOpKind::Div | hir::BinOpKind::Rem => match &r.kind {
hir::ExprKind::Lit(_lit) => (),
Expand Down Expand Up @@ -79,6 +82,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 is_from_proc_macro(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() {
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/arithmetic_side_effects.rs
@@ -1,3 +1,5 @@
// aux-build:proc_macro_derive.rs

#![allow(
clippy::assign_op_pattern,
clippy::erasing_op,
Expand All @@ -11,6 +13,8 @@
#![feature(const_mut_refs, inline_const, saturating_int_impl)]
#![warn(clippy::arithmetic_side_effects)]

extern crate proc_macro_derive;

use core::num::{Saturating, Wrapping};

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

#[derive(proc_macro_derive::ShadowDerive)]
pub struct Nothing;

macro_rules! impl_arith {
( $( $_trait:ident, $lhs:ty, $rhs:ty, $method:ident; )* ) => {
$(
Expand Down

0 comments on commit f1a552c

Please sign in to comment.