Skip to content

Commit c8b10ac

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

File tree

5 files changed

+51
-6
lines changed

5 files changed

+51
-6
lines changed

clippy_lints/src/casts/borrow_as_ptr.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::snippet_with_context;
3-
use clippy_utils::std_or_core;
3+
use clippy_utils::{is_lint_allowed, std_or_core};
44
use rustc_errors::Applicability;
55
use rustc_hir::{BorrowKind, Expr, ExprKind, Mutability, Ty, TyKind};
66
use rustc_lint::LateContext;
@@ -13,10 +13,11 @@ pub(super) fn check<'tcx>(
1313
expr: &'tcx Expr<'_>,
1414
cast_expr: &'tcx Expr<'_>,
1515
cast_to: &'tcx Ty<'_>,
16-
) {
16+
) -> bool {
1717
if matches!(cast_to.kind, TyKind::Ptr(_))
1818
&& let ExprKind::AddrOf(BorrowKind::Ref, mutability, e) = cast_expr.kind
1919
&& let Some(std_or_core) = std_or_core(cx)
20+
&& !is_lint_allowed(cx, BORROW_AS_PTR, expr.hir_id)
2021
{
2122
let macro_name = match mutability {
2223
Mutability::Not => "addr_of",
@@ -31,7 +32,7 @@ pub(super) fn check<'tcx>(
3132
.get(base.hir_id)
3233
.is_some_and(|x| x.iter().any(|adj| matches!(adj.kind, Adjust::Deref(_))))
3334
}) {
34-
return;
35+
return false;
3536
}
3637

3738
span_lint_and_sugg(
@@ -43,5 +44,7 @@ pub(super) fn check<'tcx>(
4344
format!("{std_or_core}::ptr::{macro_name}!({snip})"),
4445
Applicability::MachineApplicable,
4546
);
47+
return true;
4648
}
49+
false
4750
}

clippy_lints/src/casts/mod.rs

Lines changed: 6 additions & 3 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) {
809+
let was_borrow_as_ptr_emitted = if self.msrv.meets(msrvs::BORROW_AS_PTR) {
810+
borrow_as_ptr::check(cx, expr, cast_from_expr, cast_to_hir)
811+
} else {
812+
false
813+
};
814+
if self.msrv.meets(msrvs::PTR_FROM_REF) && !was_borrow_as_ptr_emitted {
810815
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);
813816
}
814817
}
815818

tests/ui/borrow_and_ref_as_ptr.fixed

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Make sure that `ref_as_ptr` is not emitted when `borrow_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(&raw const val);
10+
f(&raw 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 `ref_as_ptr` is not emitted when `borrow_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+
}

tests/ui/borrow_and_ref_as_ptr.stderr

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error: borrow as raw pointer
2+
--> tests/ui/borrow_and_ref_as_ptr.rs:9:7
3+
|
4+
LL | f(&val as *const _);
5+
| ^^^^^^^^^^^^^^^^ help: try: `&raw const val`
6+
|
7+
= note: `-D clippy::borrow-as-ptr` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::borrow_as_ptr)]`
9+
10+
error: borrow 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: `&raw mut val`
15+
16+
error: aborting due to 2 previous errors
17+

0 commit comments

Comments
 (0)