Skip to content

Commit

Permalink
Auto merge of rust-lang#10761 - lochetti:fix_9871, r=Manishearth
Browse files Browse the repository at this point in the history
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 rust-lang/rust-clippy#8971

changelog: [`borrow_deref_ref`] avoiding warnings in macro-generated code
  • Loading branch information
bors committed May 8, 2023
2 parents e66488a + 394b4c1 commit 90ce1a2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
6 changes: 4 additions & 2 deletions clippy_lints/src/borrow_deref_ref.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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;
Expand All @@ -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){
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/borrow_deref_ref.fixed
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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() {
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/borrow_deref_ref.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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() {
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/borrow_deref_ref.stderr
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
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`
|
= 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`
Expand Down

0 comments on commit 90ce1a2

Please sign in to comment.