Skip to content

Commit 72c77e9

Browse files
committed
Lint casting small signed integers to raw pointers
1 parent ba1d065 commit 72c77e9

File tree

6 files changed

+61
-1
lines changed

6 files changed

+61
-1
lines changed

src/librustc_lint/builtin.rs

+51
Original file line numberDiff line numberDiff line change
@@ -1265,3 +1265,54 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnionsWithDropFields {
12651265
}
12661266
}
12671267
}
1268+
1269+
1270+
/// Lint for casts from types other than `usize` or `isize` to raw pointers
1271+
pub struct IntoToRawPtrCast;
1272+
1273+
declare_lint! {
1274+
pub INT_TO_RAW_PTR_CAST,
1275+
Deny,
1276+
"cast from signed int other than `isize` to raw pointer"
1277+
}
1278+
1279+
impl LintPass for IntoToRawPtrCast {
1280+
fn get_lints(&self) -> LintArray {
1281+
lint_array!(INT_TO_RAW_PTR_CAST)
1282+
}
1283+
}
1284+
1285+
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IntoToRawPtrCast {
1286+
fn check_expr(&mut self, cx: &LateContext, expr: &hir::Expr) {
1287+
if let hir::ExprCast(ref base, _) = expr.node {
1288+
let base_ty = cx.tables.expr_ty(base);
1289+
let target_ty = cx.tables.expr_ty(expr);
1290+
if let ty::TyRawPtr(..) = target_ty.sty {
1291+
match base_ty.sty {
1292+
ty::TyInt(ast::IntTy::Is) |
1293+
ty::TyInt(ast::IntTy::I128) |
1294+
ty::TyInt(ast::IntTy::I64) => return,
1295+
ty::TyInt(ast::IntTy::I32) => {
1296+
if cx.tcx.data_layout.pointer_size.bytes() <= 4 {
1297+
return;
1298+
}
1299+
},
1300+
ty::TyInt(ast::IntTy::I16) => {
1301+
if cx.tcx.data_layout.pointer_size.bytes() <= 2 {
1302+
return;
1303+
}
1304+
},
1305+
// these we report
1306+
ty::TyInt(_) => {},
1307+
// only int casts are relevant
1308+
_ => return,
1309+
}
1310+
cx.span_lint(
1311+
INT_TO_RAW_PTR_CAST,
1312+
expr.span,
1313+
&format!("cast from `{}` to raw pointer", base_ty),
1314+
);
1315+
}
1316+
}
1317+
}
1318+
}

src/librustc_lint/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
142142
PluginAsLibrary,
143143
MutableTransmutes,
144144
UnionsWithDropFields,
145+
IntoToRawPtrCast,
145146
);
146147

147148
add_builtin_with_new!(sess,
@@ -241,6 +242,10 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
241242
id: LintId::of(LATE_BOUND_LIFETIME_ARGUMENTS),
242243
reference: "issue #42868 <https://github.com/rust-lang/rust/issues/42868>",
243244
},
245+
FutureIncompatibleInfo {
246+
id: LintId::of(builtin::INT_TO_RAW_PTR_CAST),
247+
reference: "issue #43291 <https://github.com/rust-lang/rust/issues/43291>",
248+
},
244249
]);
245250

246251
// Register renamed and removed lints

src/libstd/sys_common/at_exit_imp.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub fn cleanup() {
4848
unsafe {
4949
LOCK.lock();
5050
let queue = QUEUE;
51-
QUEUE = if i == ITERS - 1 {1} else {0} as *mut _;
51+
QUEUE = if i == ITERS - 1 {1usize} else {0usize} as *mut _;
5252
LOCK.unlock();
5353

5454
// make sure we're not recursively cleaning up

src/test/run-pass/cast-to-infer-ty.rs

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
// Check that we allow a cast to `_` so long as the target type can be
1212
// inferred elsewhere.
1313

14+
#[allow(int_to_raw_ptr_cast)]
15+
1416
pub fn main() {
1517
let i: *const i32 = 0 as _;
1618
assert!(i.is_null());

src/test/run-pass/mir_misc_casts.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010

1111
#![feature(libc)]
12+
#![allow(int_to_raw_ptr_cast)]
1213

1314
extern crate libc;
1415

src/test/run-pass/supported-cast.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010

1111
#![feature(libc)]
12+
#![allow(int_to_raw_ptr_cast)]
1213

1314
extern crate libc;
1415

0 commit comments

Comments
 (0)