Skip to content

Commit e418020

Browse files
committed
Decide between ref_as_ptr and borrow_as_ptr based on msrv
1 parent d546f2c commit e418020

8 files changed

+12
-12
lines changed

clippy_lints/src/casts/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -797,11 +797,11 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
797797

798798
as_underscore::check(cx, expr, cast_to_hir);
799799

800-
if self.msrv.meets(msrvs::BORROW_AS_PTR) {
800+
if self.msrv.meets(msrvs::PTR_FROM_REF) {
801+
ref_as_ptr::check(cx, expr, cast_expr, cast_to_hir);
802+
} else if self.msrv.meets(msrvs::BORROW_AS_PTR) {
801803
borrow_as_ptr::check(cx, expr, cast_expr, cast_to_hir);
802804
}
803-
804-
ref_as_ptr::check(cx, expr, cast_expr, cast_to_hir, &self.msrv);
805805
}
806806

807807
cast_ptr_alignment::check(cx, expr);

clippy_lints/src/casts/ref_as_ptr.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use clippy_config::msrvs::{self, Msrv};
21
use clippy_utils::diagnostics::span_lint_and_sugg;
32
use clippy_utils::is_no_std_crate;
43
use clippy_utils::source::snippet_with_applicability;
@@ -10,10 +9,7 @@ use rustc_middle::ty::{self, TypeAndMut};
109

1110
use super::REF_AS_PTR;
1211

13-
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>, cast_to_hir_ty: &Ty<'_>, msrv: &Msrv) {
14-
if !msrv.meets(msrvs::PTR_FROM_REF) {
15-
return;
16-
}
12+
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>, cast_to_hir_ty: &Ty<'_>) {
1713
let (cast_from, cast_to) = (
1814
cx.typeck_results().expr_ty(cast_expr),
1915
cx.typeck_results().expr_ty(expr),

tests/ui/borrow_as_ptr.fixed

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ fn a() -> i32 {
55
0
66
}
77

8+
#[clippy::msrv = "1.75"]
89
fn main() {
910
let val = 1;
1011
let _p = std::ptr::addr_of!(val);

tests/ui/borrow_as_ptr.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ fn a() -> i32 {
55
0
66
}
77

8+
#[clippy::msrv = "1.75"]
89
fn main() {
910
let val = 1;
1011
let _p = &val as *const i32;

tests/ui/borrow_as_ptr.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: borrow as raw pointer
2-
--> $DIR/borrow_as_ptr.rs:10:14
2+
--> $DIR/borrow_as_ptr.rs:11:14
33
|
44
LL | let _p = &val as *const i32;
55
| ^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::addr_of!(val)`
@@ -8,7 +8,7 @@ LL | let _p = &val as *const i32;
88
= help: to override `-D warnings` add `#[allow(clippy::borrow_as_ptr)]`
99

1010
error: borrow as raw pointer
11-
--> $DIR/borrow_as_ptr.rs:17:18
11+
--> $DIR/borrow_as_ptr.rs:18:18
1212
|
1313
LL | let _p_mut = &mut val_mut as *mut i32;
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::addr_of_mut!(val_mut)`

tests/ui/borrow_as_ptr_no_std.fixed

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#![feature(lang_items, start, libc)]
33
#![no_std]
44

5+
#[clippy::msrv = "1.75"]
56
#[start]
67
fn main(_argc: isize, _argv: *const *const u8) -> isize {
78
let val = 1;

tests/ui/borrow_as_ptr_no_std.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#![feature(lang_items, start, libc)]
33
#![no_std]
44

5+
#[clippy::msrv = "1.75"]
56
#[start]
67
fn main(_argc: isize, _argv: *const *const u8) -> isize {
78
let val = 1;

tests/ui/borrow_as_ptr_no_std.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: borrow as raw pointer
2-
--> $DIR/borrow_as_ptr_no_std.rs:8:14
2+
--> $DIR/borrow_as_ptr_no_std.rs:9:14
33
|
44
LL | let _p = &val as *const i32;
55
| ^^^^^^^^^^^^^^^^^^ help: try: `core::ptr::addr_of!(val)`
@@ -8,7 +8,7 @@ LL | let _p = &val as *const i32;
88
= help: to override `-D warnings` add `#[allow(clippy::borrow_as_ptr)]`
99

1010
error: borrow as raw pointer
11-
--> $DIR/borrow_as_ptr_no_std.rs:11:18
11+
--> $DIR/borrow_as_ptr_no_std.rs:12:18
1212
|
1313
LL | let _p_mut = &mut val_mut as *mut i32;
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `core::ptr::addr_of_mut!(val_mut)`

0 commit comments

Comments
 (0)