Skip to content

Commit 2f7babb

Browse files
committed
New zero_ptr lint.
This fixes #1047. I did not bother to add a full suggestion, because replacing with `std::ptr::null()` may still lead to inference failures.
1 parent 8a227c7 commit 2f7babb

File tree

7 files changed

+71
-4
lines changed

7 files changed

+71
-4
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Change Log
22
All notable changes to this project will be documented in this file.
33

4+
* New [`zero_ptr`] lint
45
* New [`never_loop`] lint
56
* New [`mut_from_ref`] lint
67

@@ -472,5 +473,6 @@ All notable changes to this project will be documented in this file.
472473
[`wrong_transmute`]: https://github.com/Manishearth/rust-clippy/wiki#wrong_transmute
473474
[`zero_divided_by_zero`]: https://github.com/Manishearth/rust-clippy/wiki#zero_divided_by_zero
474475
[`zero_prefixed_literal`]: https://github.com/Manishearth/rust-clippy/wiki#zero_prefixed_literal
476+
[`zero_ptr`]: https://github.com/Manishearth/rust-clippy/wiki#zero_ptr
475477
[`zero_width_space`]: https://github.com/Manishearth/rust-clippy/wiki#zero_width_space
476478
<!-- end autogenerated links to wiki -->

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ transparently:
180180

181181
## Lints
182182

183-
There are 191 lints included in this crate:
183+
There are 192 lints included in this crate:
184184

185185
name | default | triggers on
186186
-----------------------------------------------------------------------------------------------------------------------|---------|----------------------------------------------------------------------------------------------------------------------------------
@@ -374,6 +374,7 @@ name
374374
[wrong_transmute](https://github.com/Manishearth/rust-clippy/wiki#wrong_transmute) | warn | transmutes that are confusing at best, undefined behaviour at worst and always useless
375375
[zero_divided_by_zero](https://github.com/Manishearth/rust-clippy/wiki#zero_divided_by_zero) | warn | usage of `0.0 / 0.0` to obtain NaN instead of std::f32::NaN or std::f64::NaN
376376
[zero_prefixed_literal](https://github.com/Manishearth/rust-clippy/wiki#zero_prefixed_literal) | warn | integer literals starting with `0`
377+
[zero_ptr](https://github.com/Manishearth/rust-clippy/wiki#zero_ptr) | warn | using 0 as *{const, mut} T
377378
[zero_width_space](https://github.com/Manishearth/rust-clippy/wiki#zero_width_space) | deny | using a zero-width space in a string literal, which is confusing
378379

379380
More to come, please [file an issue](https://github.com/Manishearth/rust-clippy/issues) if you have ideas!

clippy_lints/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
450450
misc_early::REDUNDANT_CLOSURE_CALL,
451451
misc_early::UNNEEDED_FIELD_PATTERN,
452452
misc_early::ZERO_PREFIXED_LITERAL,
453+
misc_early::ZERO_PTR,
453454
mut_reference::UNNECESSARY_MUT_PASSED,
454455
mutex_atomic::MUTEX_ATOMIC,
455456
needless_bool::BOOL_COMPARISON,

clippy_lints/src/misc_early.rs

+37-1
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,23 @@ declare_lint! {
162162
"shadowing a builtin type"
163163
}
164164

165+
/// **What it does:** Catch casts from `0` to some pointer type
166+
///
167+
/// **Why is this bad?** This generally means `null` and is better expressed as
168+
/// {`std`, `core`}`::ptr::`{`null`, `null_mut`}.
169+
///
170+
/// **Known problems:** None.
171+
///
172+
/// **Example:**
173+
///
174+
/// ```rust
175+
/// ß as *const u32
176+
/// ```
177+
declare_lint! {
178+
pub ZERO_PTR,
179+
Warn,
180+
"using 0 as *{const, mut} T"
181+
}
165182

166183
#[derive(Copy, Clone)]
167184
pub struct MiscEarly;
@@ -175,7 +192,8 @@ impl LintPass for MiscEarly {
175192
MIXED_CASE_HEX_LITERALS,
176193
UNSEPARATED_LITERAL_SUFFIX,
177194
ZERO_PREFIXED_LITERAL,
178-
BUILTIN_TYPE_SHADOW)
195+
BUILTIN_TYPE_SHADOW,
196+
ZERO_PTR)
179197
}
180198
}
181199

@@ -363,6 +381,9 @@ impl EarlyLintPass for MiscEarly {
363381
}
364382
}}
365383
},
384+
ExprKind::Cast(ref e, ref ty) => {
385+
check_cast(cx, expr.span, e, ty);
386+
},
366387
_ => (),
367388
}
368389
}
@@ -391,3 +412,18 @@ impl EarlyLintPass for MiscEarly {
391412
}
392413
}
393414
}
415+
416+
fn check_cast(cx: &EarlyContext, span: Span, e: &Expr, ty: &Ty) {
417+
if_let_chain! {[
418+
let TyKind::Ptr(MutTy { mutbl, .. }) = ty.node,
419+
let ExprKind::Lit(ref lit) = e.node,
420+
let LitKind::Int(value, ..) = lit.node,
421+
value == 0
422+
], {
423+
let msg = match mutbl {
424+
Mutability::Mutable => "`0 as *mut _` detected. Consider using `ptr::null_mut()`",
425+
Mutability::Immutable => "`0 as *const _` detected. Consider using `ptr::null()`",
426+
};
427+
span_lint(cx, ZERO_PTR, span, msg);
428+
}}
429+
}

tests/ui/transmute.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ fn issue1231() {
9191
bar: &'a T,
9292
}
9393

94-
let raw = 0 as *const i32;
94+
let raw = 42 as *const i32;
9595
let _: &Foo<u8> = unsafe { std::mem::transmute::<_, &Foo<_>>(raw) };
9696

9797

@@ -103,7 +103,7 @@ fn issue1231() {
103103

104104

105105
type Bar<'a> = &'a u8;
106-
let raw = 0 as *const i32;
106+
let raw = 42 as *const i32;
107107
unsafe { std::mem::transmute::<_, Bar>(raw) };
108108

109109

tests/ui/zero_ptr.rs

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

tests/ui/zero_ptr.stderr

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
warning: `0 as *const _` detected. Consider using `ptr::null()`
2+
--> $DIR/zero_ptr.rs:6:13
3+
|
4+
6 | let x = 0 as *const usize;
5+
| ^^^^^^^^^^^^^^^^^
6+
|
7+
= note: #[warn(zero_ptr)] on by default
8+
9+
warning: `0 as *mut _` detected. Consider using `ptr::null_mut()`
10+
--> $DIR/zero_ptr.rs:7:13
11+
|
12+
7 | let y = 0 as *mut f64;
13+
| ^^^^^^^^^^^^^
14+
|
15+
= note: #[warn(zero_ptr)] on by default
16+

0 commit comments

Comments
 (0)