2023-07-27 08:23:04 -05:00
|
|
|
//@aux-build:proc_macros.rs
|
2024-02-28 07:17:14 -06:00
|
|
|
//@compile-flags: -Zdeduplicate-diagnostics=yes
|
2021-01-03 15:34:11 -06:00
|
|
|
|
|
|
|
#![warn(clippy::ptr_as_ptr)]
|
|
|
|
|
2023-08-02 15:00:41 -05:00
|
|
|
#[macro_use]
|
2023-03-07 08:40:55 -06:00
|
|
|
extern crate proc_macros;
|
2023-08-02 15:00:41 -05:00
|
|
|
|
|
|
|
mod issue_11278_a {
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct T<D: std::fmt::Debug + ?Sized> {
|
|
|
|
pub p: D,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod issue_11278_b {
|
|
|
|
pub fn f(o: &mut super::issue_11278_a::T<dyn std::fmt::Debug>) -> super::issue_11278_a::T<String> {
|
|
|
|
// Retain `super`
|
|
|
|
*unsafe { Box::from_raw(Box::into_raw(Box::new(o)).cast::<super::issue_11278_a::T<String>>()) }
|
|
|
|
}
|
|
|
|
}
|
2022-02-16 18:08:53 -06:00
|
|
|
|
2023-03-07 08:40:55 -06:00
|
|
|
#[inline_macros]
|
2021-01-03 15:34:11 -06:00
|
|
|
fn main() {
|
|
|
|
let ptr: *const u32 = &42_u32;
|
|
|
|
let mut_ptr: *mut u32 = &mut 42_u32;
|
|
|
|
|
|
|
|
let _ = ptr.cast::<i32>();
|
|
|
|
let _ = mut_ptr.cast::<i32>();
|
|
|
|
|
|
|
|
// Make sure the lint can handle the difference in their operator precedences.
|
|
|
|
unsafe {
|
|
|
|
let ptr_ptr: *const *const u32 = &ptr;
|
|
|
|
let _ = (*ptr_ptr).cast::<i32>();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Changes in mutability. Do not lint this.
|
|
|
|
let _ = ptr as *mut i32;
|
|
|
|
let _ = mut_ptr as *const i32;
|
|
|
|
|
|
|
|
// `pointer::cast` cannot perform unsized coercions unlike `as`. Do not lint this.
|
|
|
|
let ptr_of_array: *const [u32; 4] = &[1, 2, 3, 4];
|
|
|
|
let _ = ptr_of_array as *const [u32];
|
|
|
|
let _ = ptr_of_array as *const dyn std::fmt::Debug;
|
|
|
|
|
|
|
|
// Ensure the lint doesn't produce unnecessary turbofish for inferred types.
|
|
|
|
let _: *const i32 = ptr.cast();
|
|
|
|
let _: *mut i32 = mut_ptr.cast();
|
2022-02-16 18:08:53 -06:00
|
|
|
|
|
|
|
// Make sure the lint is triggered inside a macro
|
2023-03-07 08:40:55 -06:00
|
|
|
let _ = inline!($ptr.cast::<i32>());
|
2022-02-16 18:08:53 -06:00
|
|
|
|
|
|
|
// Do not lint inside macros from external crates
|
2023-03-07 08:40:55 -06:00
|
|
|
let _ = external!($ptr as *const i32);
|
2021-01-03 15:34:11 -06:00
|
|
|
}
|
2021-01-04 15:17:31 -06:00
|
|
|
|
2022-11-19 06:50:02 -06:00
|
|
|
#[clippy::msrv = "1.37"]
|
2021-01-04 15:17:31 -06:00
|
|
|
fn _msrv_1_37() {
|
|
|
|
let ptr: *const u32 = &42_u32;
|
|
|
|
let mut_ptr: *mut u32 = &mut 42_u32;
|
|
|
|
|
|
|
|
// `pointer::cast` was stabilized in 1.38. Do not lint this
|
|
|
|
let _ = ptr as *const i32;
|
|
|
|
let _ = mut_ptr as *mut i32;
|
|
|
|
}
|
|
|
|
|
2022-11-19 06:50:02 -06:00
|
|
|
#[clippy::msrv = "1.38"]
|
2021-01-04 15:17:31 -06:00
|
|
|
fn _msrv_1_38() {
|
|
|
|
let ptr: *const u32 = &42_u32;
|
|
|
|
let mut_ptr: *mut u32 = &mut 42_u32;
|
|
|
|
|
|
|
|
let _ = ptr.cast::<i32>();
|
|
|
|
let _ = mut_ptr.cast::<i32>();
|
|
|
|
}
|
2023-12-02 19:25:38 -06:00
|
|
|
|
|
|
|
#[allow(clippy::unnecessary_cast)]
|
|
|
|
mod null {
|
|
|
|
fn use_path_mut() -> *mut u32 {
|
|
|
|
use std::ptr;
|
|
|
|
ptr::null_mut::<u32>()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn full_path_mut() -> *mut u32 {
|
|
|
|
std::ptr::null_mut::<u32>()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn core_path_mut() -> *mut u32 {
|
|
|
|
use core::ptr;
|
|
|
|
ptr::null_mut::<u32>()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn full_core_path_mut() -> *mut u32 {
|
|
|
|
core::ptr::null_mut::<u32>()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn use_path() -> *const u32 {
|
|
|
|
use std::ptr;
|
|
|
|
ptr::null::<u32>()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn full_path() -> *const u32 {
|
|
|
|
std::ptr::null::<u32>()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn core_path() -> *const u32 {
|
|
|
|
use core::ptr;
|
|
|
|
ptr::null::<u32>()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn full_core_path() -> *const u32 {
|
|
|
|
core::ptr::null::<u32>()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod null_ptr_infer {
|
|
|
|
fn use_path_mut() -> *mut u32 {
|
|
|
|
use std::ptr;
|
|
|
|
ptr::null_mut()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn full_path_mut() -> *mut u32 {
|
|
|
|
std::ptr::null_mut()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn core_path_mut() -> *mut u32 {
|
|
|
|
use core::ptr;
|
|
|
|
ptr::null_mut()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn full_core_path_mut() -> *mut u32 {
|
|
|
|
core::ptr::null_mut()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn use_path() -> *const u32 {
|
|
|
|
use std::ptr;
|
|
|
|
ptr::null()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn full_path() -> *const u32 {
|
|
|
|
std::ptr::null()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn core_path() -> *const u32 {
|
|
|
|
use core::ptr;
|
|
|
|
ptr::null()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn full_core_path() -> *const u32 {
|
|
|
|
core::ptr::null()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod null_entire_infer {
|
|
|
|
fn use_path_mut() -> *mut u32 {
|
|
|
|
use std::ptr;
|
|
|
|
ptr::null_mut()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn full_path_mut() -> *mut u32 {
|
|
|
|
std::ptr::null_mut()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn core_path_mut() -> *mut u32 {
|
|
|
|
use core::ptr;
|
|
|
|
ptr::null_mut()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn full_core_path_mut() -> *mut u32 {
|
|
|
|
core::ptr::null_mut()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn use_path() -> *const u32 {
|
|
|
|
use std::ptr;
|
|
|
|
ptr::null()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn full_path() -> *const u32 {
|
|
|
|
std::ptr::null()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn core_path() -> *const u32 {
|
|
|
|
use core::ptr;
|
|
|
|
ptr::null()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn full_core_path() -> *const u32 {
|
|
|
|
core::ptr::null()
|
|
|
|
}
|
|
|
|
}
|