Skip to content

Commit a1b0d1d

Browse files
committed
Auto merge of #43641 - oli-obk:truncating_for_fun_and___wait_what, r=<try>
Lint casting signed integers smaller than `isize` to raw pointers cc #43291 (keeping open as tracking issue)
2 parents 2400ebf + 72c77e9 commit a1b0d1d

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

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)