-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
148 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::source::SpanRangeExt; | ||
use clippy_utils::{expr_or_init, is_expr_positive_literal, std_or_core}; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, Mutability, Ty, TyKind}; | ||
use rustc_lint::LateContext; | ||
|
||
use super::DANGLING_PTR; | ||
|
||
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, from: &Expr<'_>, to: &Ty<'_>) { | ||
if let TyKind::Ptr(ref mut_ty) = to.kind { | ||
let init_expr = expr_or_init(cx, from); | ||
if is_expr_positive_literal(init_expr) | ||
&& let Some(std_or_core) = std_or_core(cx) | ||
&& let Some(var) = from.span.get_source_text(cx) | ||
{ | ||
let (msg, sugg_fn) = match mut_ty.mutbl { | ||
Mutability::Not => (format!("`{var} as *const _` detected"), "ptr::dangling"), | ||
Mutability::Mut => (format!("`{var} as *mut _` detected"), "ptr::dangling_mut"), | ||
}; | ||
|
||
let sugg = if let TyKind::Infer = mut_ty.ty.kind { | ||
format!("{std_or_core}::{sugg_fn}()") | ||
} else if let Some(mut_ty_snip) = mut_ty.ty.span.get_source_text(cx) { | ||
format!("{std_or_core}::{sugg_fn}::<{mut_ty_snip}>()") | ||
} else { | ||
return; | ||
}; | ||
|
||
span_lint_and_sugg( | ||
cx, | ||
DANGLING_PTR, | ||
expr.span, | ||
msg, | ||
"try", | ||
sugg, | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
} | ||
} |
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,15 @@ | ||
#![warn(clippy::dangling_ptr)] | ||
|
||
pub fn foo(_const: *const f32, _mut: *mut i64) {} | ||
|
||
fn main() { | ||
let _ = std::ptr::dangling::<usize>(); | ||
let _ = std::ptr::dangling_mut::<f64>(); | ||
let _: *const u8 = std::ptr::dangling(); | ||
|
||
foo(0 as _, 0 as _); | ||
foo(std::ptr::dangling(), std::ptr::dangling_mut()); | ||
|
||
let z = 1; | ||
let _ = std::ptr::dangling::<usize>(); | ||
} |
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,15 @@ | ||
#![warn(clippy::dangling_ptr)] | ||
|
||
pub fn foo(_const: *const f32, _mut: *mut i64) {} | ||
|
||
fn main() { | ||
let _ = 1 as *const usize; | ||
let _ = 1 as *mut f64; | ||
let _: *const u8 = 1 as *const _; | ||
|
||
foo(0 as _, 0 as _); | ||
foo(1 as *const _, 1 as *mut _); | ||
|
||
let z = 1; | ||
let _ = z as *const usize; | ||
} |
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,41 @@ | ||
error: `1 as *const _` detected | ||
--> tests/ui/dangling_ptr.rs:6:13 | ||
| | ||
LL | let _ = 1 as *const usize; | ||
| ^^^^^^^^^^^^^^^^^ help: try: `std::ptr::dangling::<usize>()` | ||
| | ||
= note: `-D clippy::dangling-ptr` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::dangling_ptr)]` | ||
|
||
error: `1 as *mut _` detected | ||
--> tests/ui/dangling_ptr.rs:7:13 | ||
| | ||
LL | let _ = 1 as *mut f64; | ||
| ^^^^^^^^^^^^^ help: try: `std::ptr::dangling_mut::<f64>()` | ||
|
||
error: `1 as *const _` detected | ||
--> tests/ui/dangling_ptr.rs:8:24 | ||
| | ||
LL | let _: *const u8 = 1 as *const _; | ||
| ^^^^^^^^^^^^^ help: try: `std::ptr::dangling()` | ||
|
||
error: `1 as *const _` detected | ||
--> tests/ui/dangling_ptr.rs:11:9 | ||
| | ||
LL | foo(1 as *const _, 1 as *mut _); | ||
| ^^^^^^^^^^^^^ help: try: `std::ptr::dangling()` | ||
|
||
error: `1 as *mut _` detected | ||
--> tests/ui/dangling_ptr.rs:11:24 | ||
| | ||
LL | foo(1 as *const _, 1 as *mut _); | ||
| ^^^^^^^^^^^ help: try: `std::ptr::dangling_mut()` | ||
|
||
error: `z as *const _` detected | ||
--> tests/ui/dangling_ptr.rs:14:13 | ||
| | ||
LL | let _ = z as *const usize; | ||
| ^^^^^^^^^^^^^^^^^ help: try: `std::ptr::dangling::<usize>()` | ||
|
||
error: aborting due to 6 previous errors | ||
|