Skip to content

Commit 1992979

Browse files
authored
Merge pull request #1561 from Manishearth/zero_ptr
New zero_ptr lint.
2 parents e494a69 + 3c04109 commit 1992979

File tree

7 files changed

+70
-3
lines changed

7 files changed

+70
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
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

@@ -474,5 +475,6 @@ All notable changes to this project will be documented in this file.
474475
[`wrong_transmute`]: https://github.com/Manishearth/rust-clippy/wiki#wrong_transmute
475476
[`zero_divided_by_zero`]: https://github.com/Manishearth/rust-clippy/wiki#zero_divided_by_zero
476477
[`zero_prefixed_literal`]: https://github.com/Manishearth/rust-clippy/wiki#zero_prefixed_literal
478+
[`zero_ptr`]: https://github.com/Manishearth/rust-clippy/wiki#zero_ptr
477479
[`zero_width_space`]: https://github.com/Manishearth/rust-clippy/wiki#zero_width_space
478480
<!-- end autogenerated links to wiki -->

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ name
376376
[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
377377
[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
378378
[zero_prefixed_literal](https://github.com/Manishearth/rust-clippy/wiki#zero_prefixed_literal) | warn | integer literals starting with `0`
379+
[zero_ptr](https://github.com/Manishearth/rust-clippy/wiki#zero_ptr) | warn | using 0 as *{const, mut} T
379380
[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
380381

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

clippy_lints/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
452452
misc_early::REDUNDANT_CLOSURE_CALL,
453453
misc_early::UNNEEDED_FIELD_PATTERN,
454454
misc_early::ZERO_PREFIXED_LITERAL,
455+
misc_early::ZERO_PTR,
455456
mut_reference::UNNECESSARY_MUT_PASSED,
456457
mutex_atomic::MUTEX_ATOMIC,
457458
needless_bool::BOOL_COMPARISON,

clippy_lints/src/misc_early.rs

Lines changed: 37 additions & 1 deletion
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+
/// 0 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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 11 additions & 0 deletions
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

Lines changed: 16 additions & 0 deletions
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)