Auto merge of #10761 - lochetti:fix_9871, r=Manishearth

Ignore `borrow_deref_ref` warnings in code from procedural macros.

Don't linting `borrow_deref_ref` if code was generated by procedural macro.

This PR fixes https://github.com/rust-lang/rust-clippy/issues/8971

changelog: [`borrow_deref_ref`] avoiding warnings in macro-generated code
This commit is contained in:
bors 2023-05-08 16:29:02 +00:00
commit 90ce1a2e7c
4 changed files with 37 additions and 5 deletions

View File

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

View File

@ -1,7 +1,11 @@
//@run-rustfix //@run-rustfix
//@aux-build: proc_macros.rs
#![allow(dead_code, unused_variables)] #![allow(dead_code, unused_variables)]
extern crate proc_macros;
use proc_macros::with_span;
fn main() {} fn main() {}
mod should_lint { 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)` // this mod explains why we should not lint `& &* (&T)`
mod false_negative { mod false_negative {
fn foo() { fn foo() {

View File

@ -1,7 +1,11 @@
//@run-rustfix //@run-rustfix
//@aux-build: proc_macros.rs
#![allow(dead_code, unused_variables)] #![allow(dead_code, unused_variables)]
extern crate proc_macros;
use proc_macros::with_span;
fn main() {} fn main() {}
mod should_lint { 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)` // this mod explains why we should not lint `& &* (&T)`
mod false_negative { mod false_negative {
fn foo() { fn foo() {

View File

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