Skip to content

Commit df5b521

Browse files
committed
fix the order of emitting ref_as_ptr and borrow_as_ptr
1 parent b4163f0 commit df5b521

File tree

5 files changed

+50
-6
lines changed

5 files changed

+50
-6
lines changed

clippy_lints/src/casts/mod.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -806,10 +806,13 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
806806

807807
as_underscore::check(cx, expr, cast_to_hir);
808808

809-
if self.msrv.meets(msrvs::PTR_FROM_REF) {
810-
ref_as_ptr::check(cx, expr, cast_from_expr, cast_to_hir);
811-
} else if self.msrv.meets(msrvs::BORROW_AS_PTR) {
812-
borrow_as_ptr::check(cx, expr, cast_from_expr, cast_to_hir);
809+
let was_ptr_from_ref_emitted = if self.msrv.meets(msrvs::PTR_FROM_REF) {
810+
ref_as_ptr::check(cx, expr, cast_from_expr, cast_to_hir)
811+
} else {
812+
false
813+
};
814+
if self.msrv.meets(msrvs::BORROW_AS_PTR) && !was_ptr_from_ref_emitted {
815+
borrow_as_ptr::check(cx, expr, cast_from_expr, cast_to_hir, &self.msrv);
813816
}
814817
}
815818

clippy_lints/src/casts/ref_as_ptr.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub(super) fn check<'tcx>(
1414
expr: &'tcx Expr<'_>,
1515
cast_expr: &'tcx Expr<'_>,
1616
cast_to_hir_ty: &Ty<'_>,
17-
) {
17+
) -> bool {
1818
let (cast_from, cast_to) = (
1919
cx.typeck_results().expr_ty(cast_expr),
2020
cx.typeck_results().expr_ty(expr),
@@ -45,7 +45,7 @@ pub(super) fn check<'tcx>(
4545
)
4646
}
4747
},
48-
_ => return,
48+
_ => return false,
4949
};
5050

5151
let cast_expr_sugg = Sugg::hir_with_applicability(cx, cast_expr, "_", &mut app);
@@ -59,5 +59,7 @@ pub(super) fn check<'tcx>(
5959
format!("{std_or_core}::ptr::{fn_name}{turbofish}({cast_expr_sugg})"),
6060
app,
6161
);
62+
return true;
6263
}
64+
false
6365
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Make sure that `borrow_as_ptr` is not emitted when `ref_as_ptr` is.
2+
3+
#![warn(clippy::ref_as_ptr, clippy::borrow_as_ptr)]
4+
5+
fn f<T>(_: T) {}
6+
7+
fn main() {
8+
let mut val = 0;
9+
f(std::ptr::from_ref(&val));
10+
f(std::ptr::from_mut::<i32>(&mut val));
11+
}

tests/ui/borrow_and_ref_as_ptr.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Make sure that `borrow_as_ptr` is not emitted when `ref_as_ptr` is.
2+
3+
#![warn(clippy::ref_as_ptr, clippy::borrow_as_ptr)]
4+
5+
fn f<T>(_: T) {}
6+
7+
fn main() {
8+
let mut val = 0;
9+
f(&val as *const _);
10+
f(&mut val as *mut i32);
11+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error: reference as raw pointer
2+
--> tests/ui/borrow_and_ref_as_ptr.rs:9:7
3+
|
4+
LL | f(&val as *const _);
5+
| ^^^^^^^^^^^^^^^^ help: try: `std::ptr::from_ref(&val)`
6+
|
7+
= note: `-D clippy::ref-as-ptr` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::ref_as_ptr)]`
9+
10+
error: reference as raw pointer
11+
--> tests/ui/borrow_and_ref_as_ptr.rs:10:7
12+
|
13+
LL | f(&mut val as *mut i32);
14+
| ^^^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::from_mut::<i32>(&mut val)`
15+
16+
error: aborting due to 2 previous errors
17+

0 commit comments

Comments
 (0)