Add lint for useless transmutes

Closes #441.
This commit is contained in:
Andrew Paseltiner 2015-11-11 09:28:31 -05:00
parent d57fa7bc5b
commit e8a239a1a2
4 changed files with 88 additions and 1 deletions

View file

@ -6,7 +6,7 @@ A collection of lints to catch common mistakes and improve your Rust code.
[Jump to usage instructions](#usage) [Jump to usage instructions](#usage)
##Lints ##Lints
There are 73 lints included in this crate: There are 74 lints included in this crate:
name | default | meaning 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_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 [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 [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_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 [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 [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

View file

@ -55,6 +55,7 @@ pub mod needless_features;
pub mod needless_update; pub mod needless_update;
pub mod no_effect; pub mod no_effect;
pub mod temporary_assignment; pub mod temporary_assignment;
pub mod transmute;
mod reexport { mod reexport {
pub use syntax::ast::{Name, Ident, NodeId}; 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 no_effect::NoEffectPass);
reg.register_late_lint_pass(box map_clone::MapClonePass); reg.register_late_lint_pass(box map_clone::MapClonePass);
reg.register_late_lint_pass(box temporary_assignment::TemporaryAssignmentPass); reg.register_late_lint_pass(box temporary_assignment::TemporaryAssignmentPass);
reg.register_late_lint_pass(box transmute::UselessTransmute);
reg.register_lint_group("clippy_pedantic", vec![ reg.register_lint_group("clippy_pedantic", vec![
methods::OPTION_UNWRAP_USED, methods::OPTION_UNWRAP_USED,
@ -175,6 +177,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
returns::LET_AND_RETURN, returns::LET_AND_RETURN,
returns::NEEDLESS_RETURN, returns::NEEDLESS_RETURN,
temporary_assignment::TEMPORARY_ASSIGNMENT, temporary_assignment::TEMPORARY_ASSIGNMENT,
transmute::USELESS_TRANSMUTE,
types::BOX_VEC, types::BOX_VEC,
types::LET_UNIT_VALUE, types::LET_UNIT_VALUE,
types::LINKEDLIST, types::LINKEDLIST,

37
src/transmute.rs Normal file
View file

@ -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));
}
}
}
}
}
}

View file

@ -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<i32> {
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<i32> = core::intrinsics::transmute(my_vec());
//~^ ERROR transmute from a type (`collections::vec::Vec<i32>`) to itself
let _: Vec<i32> = core::mem::transmute(my_vec());
//~^ ERROR transmute from a type (`collections::vec::Vec<i32>`) to itself
let _: Vec<i32> = std::intrinsics::transmute(my_vec());
//~^ ERROR transmute from a type (`collections::vec::Vec<i32>`) to itself
let _: Vec<i32> = std::mem::transmute(my_vec());
//~^ ERROR transmute from a type (`collections::vec::Vec<i32>`) to itself
let _: Vec<i32> = my_transmute(my_vec());
//~^ ERROR transmute from a type (`collections::vec::Vec<i32>`) to itself
let _: Vec<u32> = core::intrinsics::transmute(my_vec());
let _: Vec<u32> = core::mem::transmute(my_vec());
let _: Vec<u32> = std::intrinsics::transmute(my_vec());
let _: Vec<u32> = std::mem::transmute(my_vec());
let _: Vec<u32> = my_transmute(my_vec());
}
}