Skip to content

Commit 394b4c1

Browse files
committed
Ignore borrow_deref_ref warnings in code from procedural macros.
1 parent 371120b commit 394b4c1

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

Diff for: clippy_lints/src/borrow_deref_ref.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::reference::DEREF_ADDROF;
22
use clippy_utils::diagnostics::span_lint_and_then;
3+
use clippy_utils::is_from_proc_macro;
34
use clippy_utils::source::snippet_opt;
45
use clippy_utils::ty::implements_trait;
56
use clippy_utils::{get_parent_expr, is_lint_allowed};
@@ -47,8 +48,8 @@ declare_clippy_lint! {
4748

4849
declare_lint_pass!(BorrowDerefRef => [BORROW_DEREF_REF]);
4950

50-
impl LateLintPass<'_> for BorrowDerefRef {
51-
fn check_expr(&mut self, cx: &LateContext<'_>, e: &rustc_hir::Expr<'_>) {
51+
impl<'tcx> LateLintPass<'tcx> for BorrowDerefRef {
52+
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &rustc_hir::Expr<'tcx>) {
5253
if_chain! {
5354
if !e.span.from_expansion();
5455
if let ExprKind::AddrOf(_, Mutability::Not, addrof_target) = e.kind;
@@ -58,6 +59,7 @@ impl LateLintPass<'_> for BorrowDerefRef {
5859
if !matches!(deref_target.kind, ExprKind::Unary(UnOp::Deref, ..) );
5960
let ref_ty = cx.typeck_results().expr_ty(deref_target);
6061
if let ty::Ref(_, inner_ty, Mutability::Not) = ref_ty.kind();
62+
if !is_from_proc_macro(cx, e);
6163
then{
6264

6365
if let Some(parent_expr) = get_parent_expr(cx, e){

Diff for: tests/ui/borrow_deref_ref.fixed

+15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
//@run-rustfix
2+
//@aux-build: proc_macros.rs
23

34
#![allow(dead_code, unused_variables)]
45

6+
extern crate proc_macros;
7+
use proc_macros::with_span;
8+
59
fn main() {}
610

711
mod should_lint {
@@ -47,6 +51,17 @@ mod should_not_lint2 {
4751
}
4852
}
4953

54+
with_span!(
55+
span
56+
57+
fn just_returning(x: &u32) -> &u32 {
58+
x
59+
}
60+
61+
fn dont_lint_proc_macro() {
62+
let a = &mut &*just_returning(&12);
63+
}
64+
);
5065
// this mod explains why we should not lint `& &* (&T)`
5166
mod false_negative {
5267
fn foo() {

Diff for: tests/ui/borrow_deref_ref.rs

+15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
//@run-rustfix
2+
//@aux-build: proc_macros.rs
23

34
#![allow(dead_code, unused_variables)]
45

6+
extern crate proc_macros;
7+
use proc_macros::with_span;
8+
59
fn main() {}
610

711
mod should_lint {
@@ -47,6 +51,17 @@ mod should_not_lint2 {
4751
}
4852
}
4953

54+
with_span!(
55+
span
56+
57+
fn just_returning(x: &u32) -> &u32 {
58+
x
59+
}
60+
61+
fn dont_lint_proc_macro() {
62+
let a = &mut &*just_returning(&12);
63+
}
64+
);
5065
// this mod explains why we should not lint `& &* (&T)`
5166
mod false_negative {
5267
fn foo() {

Diff for: tests/ui/borrow_deref_ref.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
error: deref on an immutable reference
2-
--> $DIR/borrow_deref_ref.rs:10:17
2+
--> $DIR/borrow_deref_ref.rs:14:17
33
|
44
LL | let b = &*a;
55
| ^^^ help: if you would like to reborrow, try removing `&*`: `a`
66
|
77
= note: `-D clippy::borrow-deref-ref` implied by `-D warnings`
88

99
error: deref on an immutable reference
10-
--> $DIR/borrow_deref_ref.rs:12:22
10+
--> $DIR/borrow_deref_ref.rs:16:22
1111
|
1212
LL | let b = &mut &*bar(&12);
1313
| ^^^^^^^^^^ help: if you would like to reborrow, try removing `&*`: `bar(&12)`
1414

1515
error: deref on an immutable reference
16-
--> $DIR/borrow_deref_ref.rs:55:23
16+
--> $DIR/borrow_deref_ref.rs:70:23
1717
|
1818
LL | let addr_y = &&*x as *const _ as usize; // assert ok
1919
| ^^^ help: if you would like to reborrow, try removing `&*`: `x`

0 commit comments

Comments
 (0)