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

@ -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,

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