Skip to content
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

[WIP] Add lint for int to ptr cast #43339

Closed
wants to merge 3 commits into from
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
38 changes: 38 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,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");
Copy link
Contributor

Choose a reason for hiding this comment

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

The message is a placeholder, but I think "casting from a potentially non-pointer-sized integer to a pointer" would be better English.

},
_ => {},
};
}
}
}
}
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