-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Author: Marcin Serwin <[email protected]>
- Loading branch information
1 parent
bc962c2
commit 73f3fce
Showing
17 changed files
with
588 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::is_no_std_crate; | ||
use clippy_utils::source::snippet_with_applicability; | ||
use clippy_utils::sugg::Sugg; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, Mutability, Ty, TyKind}; | ||
use rustc_lint::LateContext; | ||
use rustc_middle::ty::{self, TypeAndMut}; | ||
|
||
use super::REF_AS_PTR; | ||
|
||
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>, cast_to_hir_ty: &Ty<'_>) { | ||
let (cast_from, cast_to) = ( | ||
cx.typeck_results().expr_ty(cast_expr), | ||
cx.typeck_results().expr_ty(expr), | ||
); | ||
|
||
if matches!(cast_from.kind(), ty::Ref(..)) | ||
&& let ty::RawPtr(TypeAndMut { mutbl: to_mutbl, .. }) = cast_to.kind() | ||
{ | ||
let core_or_std = if is_no_std_crate(cx) { "core" } else { "std" }; | ||
let fn_name = match to_mutbl { | ||
Mutability::Not => "from_ref", | ||
Mutability::Mut => "from_mut", | ||
}; | ||
|
||
let mut app = Applicability::MachineApplicable; | ||
let turbofish = match &cast_to_hir_ty.kind { | ||
TyKind::Infer => String::new(), | ||
TyKind::Ptr(mut_ty) => { | ||
if matches!(mut_ty.ty.kind, TyKind::Infer) { | ||
String::new() | ||
} else { | ||
format!( | ||
"::<{}>", | ||
snippet_with_applicability(cx, mut_ty.ty.span, "/* type */", &mut app) | ||
) | ||
} | ||
}, | ||
_ => return, | ||
}; | ||
|
||
let cast_expr_sugg = Sugg::hir_with_applicability(cx, cast_expr, "_", &mut app); | ||
|
||
span_lint_and_sugg( | ||
cx, | ||
REF_AS_PTR, | ||
expr.span, | ||
"reference as raw pointer", | ||
"try", | ||
format!("{core_or_std}::ptr::{fn_name}{turbofish}({cast_expr_sugg})"), | ||
app, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ fn a() -> i32 { | |
0 | ||
} | ||
|
||
#[clippy::msrv = "1.75"] | ||
fn main() { | ||
let val = 1; | ||
let _p = &val as *const i32; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
#![warn(clippy::ref_as_ptr)] | ||
#![allow(clippy::unnecessary_mut_passed)] | ||
|
||
fn main() { | ||
let _ = std::ptr::from_ref(&1u8); | ||
let _ = std::ptr::from_ref::<u32>(&2u32); | ||
let _ = std::ptr::from_ref::<f64>(&3.0f64); | ||
|
||
let _ = std::ptr::from_ref(&4) as *const f32; | ||
let _ = std::ptr::from_ref::<f32>(&5.0f32) as *const u32; | ||
|
||
let _ = std::ptr::from_ref(&mut 6u8); | ||
let _ = std::ptr::from_ref::<u32>(&mut 7u32); | ||
let _ = std::ptr::from_ref::<f64>(&mut 8.0f64); | ||
|
||
let _ = std::ptr::from_ref(&mut 9) as *const f32; | ||
let _ = std::ptr::from_ref::<f32>(&mut 10.0f32) as *const u32; | ||
|
||
let _ = std::ptr::from_mut(&mut 11u8); | ||
let _ = std::ptr::from_mut::<u32>(&mut 12u32); | ||
let _ = std::ptr::from_mut::<f64>(&mut 13.0f64); | ||
|
||
let _ = std::ptr::from_mut(&mut 14) as *const f32; | ||
let _ = std::ptr::from_mut::<f32>(&mut 15.0f32) as *const u32; | ||
|
||
let _ = std::ptr::from_ref(&1u8); | ||
let _ = std::ptr::from_ref::<u32>(&2u32); | ||
let _ = std::ptr::from_ref::<f64>(&3.0f64); | ||
|
||
let _ = std::ptr::from_ref(&4) as *const f32; | ||
let _ = std::ptr::from_ref::<f32>(&5.0f32) as *const u32; | ||
|
||
let val = 1; | ||
let _ = std::ptr::from_ref(&val); | ||
let _ = std::ptr::from_ref::<i32>(&val); | ||
|
||
let _ = std::ptr::from_ref(&val) as *const f32; | ||
let _ = std::ptr::from_ref::<i32>(&val) as *const f64; | ||
|
||
let mut val: u8 = 2; | ||
let _ = std::ptr::from_mut::<u8>(&mut val); | ||
let _ = std::ptr::from_mut(&mut val); | ||
|
||
let _ = std::ptr::from_ref::<u8>(&mut val); | ||
let _ = std::ptr::from_ref(&mut val); | ||
|
||
let _ = std::ptr::from_ref::<u8>(&mut val) as *const f64; | ||
let _: *const Option<u8> = std::ptr::from_ref(&mut val) as *const _; | ||
|
||
let _ = std::ptr::from_ref::<[usize; 7]>(&std::array::from_fn(|i| i * i)); | ||
let _ = std::ptr::from_ref::<[usize; 8]>(&mut std::array::from_fn(|i| i * i)); | ||
let _ = std::ptr::from_mut::<[usize; 9]>(&mut std::array::from_fn(|i| i * i)); | ||
} | ||
|
||
#[clippy::msrv = "1.75"] | ||
fn _msrv_1_75() { | ||
let val = &42_i32; | ||
let mut_val = &mut 42_i32; | ||
|
||
// `std::ptr::from_{ref, mut}` was stabilized in 1.76. Do not lint this | ||
let _ = val as *const i32; | ||
let _ = mut_val as *mut i32; | ||
} | ||
|
||
#[clippy::msrv = "1.76"] | ||
fn _msrv_1_76() { | ||
let val = &42_i32; | ||
let mut_val = &mut 42_i32; | ||
|
||
let _ = std::ptr::from_ref::<i32>(val); | ||
let _ = std::ptr::from_mut::<i32>(mut_val); | ||
} | ||
|
||
fn foo(val: &[u8]) { | ||
let _ = std::ptr::from_ref(val); | ||
let _ = std::ptr::from_ref::<[u8]>(val); | ||
} | ||
|
||
fn bar(val: &mut str) { | ||
let _ = std::ptr::from_mut(val); | ||
let _ = std::ptr::from_mut::<str>(val); | ||
} | ||
|
||
struct X<'a>(&'a i32); | ||
|
||
impl<'a> X<'a> { | ||
fn foo(&self) -> *const i64 { | ||
std::ptr::from_ref(self.0) as *const _ | ||
} | ||
|
||
fn bar(&mut self) -> *const i64 { | ||
std::ptr::from_ref(self.0) as *const _ | ||
} | ||
} | ||
|
||
struct Y<'a>(&'a mut i32); | ||
|
||
impl<'a> Y<'a> { | ||
fn foo(&self) -> *const i64 { | ||
std::ptr::from_ref(self.0) as *const _ | ||
} | ||
|
||
fn bar(&mut self) -> *const i64 { | ||
std::ptr::from_ref(self.0) as *const _ | ||
} | ||
|
||
fn baz(&mut self) -> *const i64 { | ||
std::ptr::from_mut(self.0) as *mut _ | ||
} | ||
} |
Oops, something went wrong.