Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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
41 changes: 41 additions & 0 deletions src/librustc_lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -1225,3 +1226,43 @@ 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 {
match ctx.tables.cast_kinds.get(&source.id) {
Some(&CastKind::AddrPtrCast) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if let Some(&CastKind::AddrPtrCast) = ctx.tables.cast_kinds.get(&source.id) {
?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

certainly

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");
},
_ => {},
};
},
_ => {},
}
}
}
}
1 change: 1 addition & 0 deletions src/librustc_lint/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
PluginAsLibrary,
MutableTransmutes,
UnionsWithDropFields,
IntToPtrCast,
);

add_builtin_with_new!(sess,
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/sys/windows/backtrace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/sys/windows/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(|_| ())
}
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