Skip to content

Commit ec69a57

Browse files
committed
Lint casting integers to dangling pointers
1 parent cf58e1c commit ec69a57

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-6
lines changed

clippy_lints/src/misc.rs

+32-6
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,24 @@ declare_lint! {
201201
"using 0 as *{const, mut} T"
202202
}
203203

204+
/// **What it does:** Catch casts from `1` to some pointer type
205+
///
206+
/// **Why is this bad?** This generally means a dangling pointer and is better expressed as
207+
/// {`std`, `core`}`::ptr::`{`dangling`, `dangling_mut`}.
208+
///
209+
/// **Known problems:** None.
210+
///
211+
/// **Example:**
212+
///
213+
/// ```rust
214+
/// 1 as *const u32
215+
/// ```
216+
declare_lint! {
217+
pub DANGLING_PTR,
218+
Allow,
219+
"using 1 as *{const, mut} T"
220+
}
221+
204222
/// **What it does:** Checks for (in-)equality comparisons on floating-point
205223
/// value and constant, except in functions called `*eq*` (which probably
206224
/// implement equality for a type involving floats).
@@ -237,6 +255,7 @@ impl LintPass for Pass {
237255
USED_UNDERSCORE_BINDING,
238256
SHORT_CIRCUIT_STATEMENT,
239257
ZERO_PTR,
258+
DANGLING_PTR,
240259
FLOAT_CMP_CONST
241260
)
242261
}
@@ -628,14 +647,21 @@ fn check_cast(cx: &LateContext, span: Span, e: &Expr, ty: &Ty) {
628647
if let TyPtr(MutTy { mutbl, .. }) = ty.node;
629648
if let ExprLit(ref lit) = e.node;
630649
if let LitKind::Int(value, ..) = lit.node;
631-
if value == 0;
632650
if !in_constant(cx, e.id);
633651
then {
634-
let msg = match mutbl {
635-
Mutability::MutMutable => "`0 as *mut _` detected. Consider using `ptr::null_mut()`",
636-
Mutability::MutImmutable => "`0 as *const _` detected. Consider using `ptr::null()`",
637-
};
638-
span_lint(cx, ZERO_PTR, span, msg);
652+
if value == 0 {
653+
let msg = match mutbl {
654+
Mutability::MutMutable => "`0 as *mut _` detected. Consider using `ptr::null_mut()`",
655+
Mutability::MutImmutable => "`0 as *const _` detected. Consider using `ptr::null()`",
656+
};
657+
span_lint(cx, ZERO_PTR, span, msg);
658+
} else if value == 1 {
659+
let msg = match mutbl {
660+
Mutability::MutMutable => "`1 as *mut _` detected. Consider using `ptr::dangling_mut()`",
661+
Mutability::MutImmutable => "`1 as *const _` detected. Consider using `ptr::dangling()`",
662+
};
663+
span_lint(cx, DANGLING_PTR, span, msg);
664+
}
639665
}
640666
}
641667
}

tests/ui/dangling_ptr.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![warn(dangling_ptr)]
2+
3+
#[allow(unused_variables)]
4+
fn main() {
5+
let x = 1 as *const usize;
6+
let y = 1 as *mut f64;
7+
8+
let z = 1;
9+
let z = z as *const usize; // this is currently not caught
10+
}

tests/ui/dangling_ptr.stderr

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: `1 as *const _` detected. Consider using `ptr::dangling()`
2+
--> $DIR/dangling_ptr.rs:5:13
3+
|
4+
5 | let x = 1 as *const usize;
5+
| ^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D dangling-ptr` implied by `-D warnings`
8+
9+
error: `1 as *mut _` detected. Consider using `ptr::dangling_mut()`
10+
--> $DIR/dangling_ptr.rs:6:13
11+
|
12+
6 | let y = 1 as *mut f64;
13+
| ^^^^^^^^^^^^^
14+

0 commit comments

Comments
 (0)