diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index 02d68a41b4cc4..266ef3ca3dc37 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -31,6 +31,7 @@ use rustc::hir::def::Def; use rustc::hir::def_id::DefId; use rustc::cfg; +use rustc::ty::cast::CastKind; use rustc::ty::subst::Substs; use rustc::ty::{self, Ty}; use rustc::traits::{self, Reveal}; @@ -1225,3 +1226,40 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnionsWithDropFields { } } } + +/// Lint for casting from non-{i,u}size integers to pointers. +pub struct IntToPtrCast; + +declare_lint! { + INT_TO_PTR_CAST, + Warn, + "casting from non-{i,u}size integers to pointers" +} + +impl LintPass for IntToPtrCast { + fn get_lints(&self) -> LintArray { + lint_array!(INT_TO_PTR_CAST) + } +} + +impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IntToPtrCast { + fn check_expr(&mut self, ctx: &LateContext, expr: &hir::Expr) { + if let hir::ExprCast(ref source, _) = expr.node { + if let Some(&CastKind::AddrPtrCast) = ctx.tables.cast_kinds.get(&source.id) { + let source_ty = &ctx.tables.expr_ty(&source).sty; + match source_ty { + &ty::TyInt(ast::IntTy::Is) | + &ty::TyUint(ast::UintTy::Us) => {}, + &ty::TyInt(_) | + &ty::TyUint(_) => { + ctx.span_lint( + INT_TO_PTR_CAST, + expr.span, + "casting from a different-size integer to a pointer"); + }, + _ => {}, + }; + } + } + } +} diff --git a/src/librustc_lint/lib.rs b/src/librustc_lint/lib.rs index a03f12c3dfbca..2d65df4abee49 100644 --- a/src/librustc_lint/lib.rs +++ b/src/librustc_lint/lib.rs @@ -141,6 +141,7 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) { PluginAsLibrary, MutableTransmutes, UnionsWithDropFields, + IntToPtrCast, ); add_builtin_with_new!(sess, diff --git a/src/libstd/sys/windows/backtrace/mod.rs b/src/libstd/sys/windows/backtrace/mod.rs index 3c3fd8d3e4a9c..f1673f0e1e1fe 100644 --- a/src/libstd/sys/windows/backtrace/mod.rs +++ b/src/libstd/sys/windows/backtrace/mod.rs @@ -95,8 +95,8 @@ pub fn unwind_backtrace(frames: &mut [Frame]) frame.AddrReturn.Offset == 0 { break } frames[i] = Frame { - symbol_addr: (addr - 1) as *const c_void, - exact_position: (addr - 1) as *const c_void, + symbol_addr: (addr - 1) as usize as *const c_void, + exact_position: (addr - 1) as usize as *const c_void, }; i += 1; } diff --git a/src/libstd/sys/windows/net.rs b/src/libstd/sys/windows/net.rs index cd8acff6b0cb8..97aa4653f7dfc 100644 --- a/src/libstd/sys/windows/net.rs +++ b/src/libstd/sys/windows/net.rs @@ -282,7 +282,7 @@ impl Socket { fn set_no_inherit(&self) -> io::Result<()> { sys::cvt(unsafe { - c::SetHandleInformation(self.0 as c::HANDLE, + c::SetHandleInformation(self.0 as usize as c::HANDLE, c::HANDLE_FLAG_INHERIT, 0) }).map(|_| ()) } diff --git a/src/libstd/sys_common/at_exit_imp.rs b/src/libstd/sys_common/at_exit_imp.rs index ce6fd4cb0754b..8a9fe783aa5bf 100644 --- a/src/libstd/sys_common/at_exit_imp.rs +++ b/src/libstd/sys_common/at_exit_imp.rs @@ -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