Ignore borrow_deref_ref warnings in code from procedural macros.

This commit is contained in:
Renato Lochetti 2023-05-07 12:35:17 +01:00
parent 371120bdbf
commit 394b4c1906
No known key found for this signature in database
GPG key ID: FDE4EF56C479D308
4 changed files with 37 additions and 5 deletions

View file

@ -1,5 +1,6 @@
use crate::reference::DEREF_ADDROF;
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::is_from_proc_macro;
use clippy_utils::source::snippet_opt;
use clippy_utils::ty::implements_trait;
use clippy_utils::{get_parent_expr, is_lint_allowed};
@ -47,8 +48,8 @@ declare_clippy_lint! {
declare_lint_pass!(BorrowDerefRef => [BORROW_DEREF_REF]);
impl LateLintPass<'_> for BorrowDerefRef {
fn check_expr(&mut self, cx: &LateContext<'_>, e: &rustc_hir::Expr<'_>) {
impl<'tcx> LateLintPass<'tcx> for BorrowDerefRef {
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &rustc_hir::Expr<'tcx>) {
if_chain! {
if !e.span.from_expansion();
if let ExprKind::AddrOf(_, Mutability::Not, addrof_target) = e.kind;
@ -58,6 +59,7 @@ impl LateLintPass<'_> for BorrowDerefRef {
if !matches!(deref_target.kind, ExprKind::Unary(UnOp::Deref, ..) );
let ref_ty = cx.typeck_results().expr_ty(deref_target);
if let ty::Ref(_, inner_ty, Mutability::Not) = ref_ty.kind();
if !is_from_proc_macro(cx, e);
then{
if let Some(parent_expr) = get_parent_expr(cx, e){

View file

@ -1,7 +1,11 @@
//@run-rustfix
//@aux-build: proc_macros.rs
#![allow(dead_code, unused_variables)]
extern crate proc_macros;
use proc_macros::with_span;
fn main() {}
mod should_lint {
@ -47,6 +51,17 @@ mod should_not_lint2 {
}
}
with_span!(
span
fn just_returning(x: &u32) -> &u32 {
x
}
fn dont_lint_proc_macro() {
let a = &mut &*just_returning(&12);
}
);
// this mod explains why we should not lint `& &* (&T)`
mod false_negative {
fn foo() {

View file

@ -1,7 +1,11 @@
//@run-rustfix
//@aux-build: proc_macros.rs
#![allow(dead_code, unused_variables)]
extern crate proc_macros;
use proc_macros::with_span;
fn main() {}
mod should_lint {
@ -47,6 +51,17 @@ mod should_not_lint2 {
}
}
with_span!(
span
fn just_returning(x: &u32) -> &u32 {
x
}
fn dont_lint_proc_macro() {
let a = &mut &*just_returning(&12);
}
);
// this mod explains why we should not lint `& &* (&T)`
mod false_negative {
fn foo() {

View file

@ -1,5 +1,5 @@
error: deref on an immutable reference
--> $DIR/borrow_deref_ref.rs:10:17
--> $DIR/borrow_deref_ref.rs:14:17
|
LL | let b = &*a;
| ^^^ help: if you would like to reborrow, try removing `&*`: `a`
@ -7,13 +7,13 @@ LL | let b = &*a;
= note: `-D clippy::borrow-deref-ref` implied by `-D warnings`
error: deref on an immutable reference
--> $DIR/borrow_deref_ref.rs:12:22
--> $DIR/borrow_deref_ref.rs:16:22
|
LL | let b = &mut &*bar(&12);
| ^^^^^^^^^^ help: if you would like to reborrow, try removing `&*`: `bar(&12)`
error: deref on an immutable reference
--> $DIR/borrow_deref_ref.rs:55:23
--> $DIR/borrow_deref_ref.rs:70:23
|
LL | let addr_y = &&*x as *const _ as usize; // assert ok
| ^^^ help: if you would like to reborrow, try removing `&*`: `x`