Skip to content

Lint casting signed integers smaller than isize to raw pointers #43641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/librustc_lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1265,3 +1265,54 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnionsWithDropFields {
}
}
}


/// Lint for casts from types other than `usize` or `isize` to raw pointers
pub struct IntoToRawPtrCast;

declare_lint! {
pub INT_TO_RAW_PTR_CAST,
Deny,
"cast from signed int other than `isize` to raw pointer"
}

impl LintPass for IntoToRawPtrCast {
fn get_lints(&self) -> LintArray {
lint_array!(INT_TO_RAW_PTR_CAST)
}
}

impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IntoToRawPtrCast {
fn check_expr(&mut self, cx: &LateContext, expr: &hir::Expr) {
if let hir::ExprCast(ref base, _) = expr.node {
let base_ty = cx.tables.expr_ty(base);
let target_ty = cx.tables.expr_ty(expr);
if let ty::TyRawPtr(..) = target_ty.sty {
match base_ty.sty {
ty::TyInt(ast::IntTy::Is) |
ty::TyInt(ast::IntTy::I128) |
ty::TyInt(ast::IntTy::I64) => return,
ty::TyInt(ast::IntTy::I32) => {
if cx.tcx.data_layout.pointer_size.bytes() <= 4 {
return;
}
},
ty::TyInt(ast::IntTy::I16) => {
if cx.tcx.data_layout.pointer_size.bytes() <= 2 {
return;
}
},
// these we report
ty::TyInt(_) => {},
// only int casts are relevant
_ => return,
}
cx.span_lint(
INT_TO_RAW_PTR_CAST,
expr.span,
&format!("cast from `{}` to raw pointer", base_ty),
);
}
}
}
}
5 changes: 5 additions & 0 deletions src/librustc_lint/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
PluginAsLibrary,
MutableTransmutes,
UnionsWithDropFields,
IntoToRawPtrCast,
);

add_builtin_with_new!(sess,
Expand Down Expand Up @@ -241,6 +242,10 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
id: LintId::of(LATE_BOUND_LIFETIME_ARGUMENTS),
reference: "issue #42868 <https://github.com/rust-lang/rust/issues/42868>",
},
FutureIncompatibleInfo {
id: LintId::of(builtin::INT_TO_RAW_PTR_CAST),
reference: "issue #43291 <https://github.com/rust-lang/rust/issues/43291>",
},
]);

// Register renamed and removed lints
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/sys_common/at_exit_imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub fn cleanup() {
unsafe {
LOCK.lock();
let queue = QUEUE;
QUEUE = if i == ITERS - 1 {1} else {0} as *mut _;
QUEUE = if i == ITERS - 1 {1usize} else {0usize} as *mut _;
LOCK.unlock();

// make sure we're not recursively cleaning up
Expand Down
2 changes: 2 additions & 0 deletions src/test/run-pass/cast-to-infer-ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
// Check that we allow a cast to `_` so long as the target type can be
// inferred elsewhere.

#[allow(int_to_raw_ptr_cast)]

pub fn main() {
let i: *const i32 = 0 as _;
assert!(i.is_null());
Expand Down
1 change: 1 addition & 0 deletions src/test/run-pass/mir_misc_casts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// except according to those terms.

#![feature(libc)]
#![allow(int_to_raw_ptr_cast)]

extern crate libc;

Expand Down
1 change: 1 addition & 0 deletions src/test/run-pass/supported-cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// except according to those terms.

#![feature(libc)]
#![allow(int_to_raw_ptr_cast)]

extern crate libc;

Expand Down