Skip to content

Commit

Permalink
Suppress the triggering of some lints in derived structures
Browse files Browse the repository at this point in the history
  • Loading branch information
c410-f3r committed Jan 19, 2023
1 parent 97fb130 commit 8ae2a01
Show file tree
Hide file tree
Showing 10 changed files with 237 additions and 177 deletions.
7 changes: 7 additions & 0 deletions clippy_lints/src/operators/arithmetic_side_effects.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::ARITHMETIC_SIDE_EFFECTS;
use clippy_utils::is_from_proc_macro;
use clippy_utils::{
consts::{constant, constant_simple},
diagnostics::span_lint,
Expand Down Expand Up @@ -120,6 +121,9 @@ impl ArithmeticSideEffects {
lhs: &hir::Expr<'tcx>,
rhs: &hir::Expr<'tcx>,
) {
if is_from_proc_macro(cx, expr) {
return;
}
if constant_simple(cx, cx.typeck_results(), expr).is_some() {
return;
}
Expand Down Expand Up @@ -171,6 +175,9 @@ impl ArithmeticSideEffects {
un_expr: &hir::Expr<'tcx>,
un_op: hir::UnOp,
) {
if is_from_proc_macro(cx, expr) {
return;
}
let hir::UnOp::Neg = un_op else { return; };
if constant(cx, cx.typeck_results(), un_expr).is_some() {
return;
Expand Down
13 changes: 8 additions & 5 deletions clippy_lints/src/operators/numeric_arithmetic.rs
Original file line number Diff line number Diff line change
@@ -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 All @@ -15,7 +15,10 @@ pub struct Context {
const_span: Option<Span>,
}
impl Context {
fn skip_expr(&mut self, e: &hir::Expr<'_>) -> bool {
fn skip_expr<'tcx>(&mut self, cx: &LateContext<'tcx>, e: &'tcx hir::Expr<'_>) -> bool {
if is_from_proc_macro(cx, e) {
return true;
}
self.expr_id.is_some() || self.const_span.map_or(false, |span| span.contains(e.span))
}

Expand All @@ -27,7 +30,7 @@ impl Context {
l: &'tcx hir::Expr<'_>,
r: &'tcx hir::Expr<'_>,
) {
if self.skip_expr(expr) {
if self.skip_expr(cx, expr) {
return;
}
match op {
Expand Down Expand Up @@ -73,7 +76,7 @@ impl Context {
}

pub fn check_negate<'tcx>(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, arg: &'tcx hir::Expr<'_>) {
if self.skip_expr(expr) {
if self.skip_expr(cx, expr) {
return;
}
let ty = cx.typeck_results().expr_ty(arg);
Expand Down
4 changes: 4 additions & 0 deletions clippy_lints/src/shadow.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use clippy_utils::any_parent_is_automatically_derived;
use clippy_utils::diagnostics::span_lint_and_note;
use clippy_utils::source::snippet;
use clippy_utils::visitors::is_local_used;
Expand Down Expand Up @@ -170,6 +171,9 @@ fn is_shadow(cx: &LateContext<'_>, owner: LocalDefId, first: ItemLocalId, second
}

fn lint_shadow(cx: &LateContext<'_>, pat: &Pat<'_>, shadowed: HirId, span: Span) {
if any_parent_is_automatically_derived(cx.tcx, pat.hir_id) {
return;
}
let (lint, msg) = match find_init(cx, pat.hir_id) {
Some(expr) if is_self_shadow(cx, pat, expr, shadowed) => {
let msg = format!(
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/arithmetic_side_effects.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// aux-build:proc_macro_derive.rs

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

extern crate proc_macro_derive;

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

#[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
Loading

0 comments on commit 8ae2a01

Please sign in to comment.