diff --git a/README.md b/README.md index 6727707717f..b893c4e7a17 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ A collection of lints to catch common mistakes and improve your Rust code. [Jump to usage instructions](#usage) ##Lints -There are 73 lints included in this crate: +There are 74 lints included in this crate: name | default | meaning -------------------------------------------------------------------------------------------------------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ @@ -77,6 +77,7 @@ name [unstable_as_mut_slice](https://github.com/Manishearth/rust-clippy/wiki#unstable_as_mut_slice) | warn | as_mut_slice is not stable and can be replaced by &mut v[..]see https://github.com/rust-lang/rust/issues/27729 [unstable_as_slice](https://github.com/Manishearth/rust-clippy/wiki#unstable_as_slice) | warn | as_slice is not stable and can be replaced by & v[..]see https://github.com/rust-lang/rust/issues/27729 [unused_collect](https://github.com/Manishearth/rust-clippy/wiki#unused_collect) | warn | `collect()`ing an iterator without using the result; this is usually better written as a for loop +[useless_transmute](https://github.com/Manishearth/rust-clippy/wiki#useless_transmute) | warn | transmutes that have the same to and from types [while_let_loop](https://github.com/Manishearth/rust-clippy/wiki#while_let_loop) | warn | `loop { if let { ... } else break }` can be written as a `while let` loop [while_let_on_iterator](https://github.com/Manishearth/rust-clippy/wiki#while_let_on_iterator) | warn | using a while-let loop instead of a for loop on an iterator [wrong_pub_self_convention](https://github.com/Manishearth/rust-clippy/wiki#wrong_pub_self_convention) | allow | defining a public method named with an established prefix (like "into_") that takes `self` with the wrong convention diff --git a/src/lib.rs b/src/lib.rs index 970a244e2cd..afbf3d2a92c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -55,6 +55,7 @@ pub mod needless_features; pub mod needless_update; pub mod no_effect; pub mod temporary_assignment; +pub mod transmute; mod reexport { pub use syntax::ast::{Name, Ident, NodeId}; @@ -104,6 +105,7 @@ pub fn plugin_registrar(reg: &mut Registry) { reg.register_late_lint_pass(box no_effect::NoEffectPass); reg.register_late_lint_pass(box map_clone::MapClonePass); reg.register_late_lint_pass(box temporary_assignment::TemporaryAssignmentPass); + reg.register_late_lint_pass(box transmute::UselessTransmute); reg.register_lint_group("clippy_pedantic", vec![ methods::OPTION_UNWRAP_USED, @@ -175,6 +177,7 @@ pub fn plugin_registrar(reg: &mut Registry) { returns::LET_AND_RETURN, returns::NEEDLESS_RETURN, temporary_assignment::TEMPORARY_ASSIGNMENT, + transmute::USELESS_TRANSMUTE, types::BOX_VEC, types::LET_UNIT_VALUE, types::LINKEDLIST, diff --git a/src/transmute.rs b/src/transmute.rs new file mode 100644 index 00000000000..ab1397ea7fe --- /dev/null +++ b/src/transmute.rs @@ -0,0 +1,37 @@ +use rustc::lint::*; +use rustc_front::hir::*; +use utils; + +declare_lint! { + pub USELESS_TRANSMUTE, + Warn, + "transmutes that have the same to and from types" +} + +pub struct UselessTransmute; + +impl LintPass for UselessTransmute { + fn get_lints(&self) -> LintArray { + lint_array!(USELESS_TRANSMUTE) + } +} + +impl LateLintPass for UselessTransmute { + fn check_expr(&mut self, cx: &LateContext, e: &Expr) { + if let ExprCall(ref path_expr, ref args) = e.node { + if let ExprPath(None, _) = path_expr.node { + let def_id = cx.tcx.def_map.borrow()[&path_expr.id].def_id(); + + if utils::match_def_path(cx, def_id, &["core", "intrinsics", "transmute"]) { + let from_ty = cx.tcx.expr_ty(&args[0]); + let to_ty = cx.tcx.expr_ty(e); + + if from_ty == to_ty { + cx.span_lint(USELESS_TRANSMUTE, e.span, + &format!("transmute from a type (`{}`) to itself", from_ty)); + } + } + } + } + } +} diff --git a/tests/compile-fail/transmute.rs b/tests/compile-fail/transmute.rs new file mode 100644 index 00000000000..0a2d09d9431 --- /dev/null +++ b/tests/compile-fail/transmute.rs @@ -0,0 +1,46 @@ +#![feature(core)] +#![feature(plugin)] +#![plugin(clippy)] +#![deny(useless_transmute)] + +extern crate core; + +use std::mem::transmute as my_transmute; +use std::vec::Vec as MyVec; + +fn my_vec() -> MyVec { + vec![] +} + +#[allow(needless_lifetimes)] +unsafe fn _generic<'a, T, U: 'a>(t: &'a T) { + let _: &'a T = core::intrinsics::transmute(t); + //~^ ERROR transmute from a type (`&'a T`) to itself + + let _: &'a U = core::intrinsics::transmute(t); +} + +fn main() { + unsafe { + let _: Vec = core::intrinsics::transmute(my_vec()); + //~^ ERROR transmute from a type (`collections::vec::Vec`) to itself + + let _: Vec = core::mem::transmute(my_vec()); + //~^ ERROR transmute from a type (`collections::vec::Vec`) to itself + + let _: Vec = std::intrinsics::transmute(my_vec()); + //~^ ERROR transmute from a type (`collections::vec::Vec`) to itself + + let _: Vec = std::mem::transmute(my_vec()); + //~^ ERROR transmute from a type (`collections::vec::Vec`) to itself + + let _: Vec = my_transmute(my_vec()); + //~^ ERROR transmute from a type (`collections::vec::Vec`) to itself + + let _: Vec = core::intrinsics::transmute(my_vec()); + let _: Vec = core::mem::transmute(my_vec()); + let _: Vec = std::intrinsics::transmute(my_vec()); + let _: Vec = std::mem::transmute(my_vec()); + let _: Vec = my_transmute(my_vec()); + } +}