Skip to content

Ignore borrow_deref_ref warnings in code from procedural macros. #10761

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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