mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-10 23:24:24 +00:00
commit
6097b8240b
3 changed files with 37 additions and 12 deletions
|
@ -5,10 +5,9 @@
|
|||
|
||||
use rustc::lint::*;
|
||||
use rustc::hir;
|
||||
use rustc::hir::{Expr, Expr_, QPath};
|
||||
use rustc::hir::{Expr, Expr_, QPath, Ty_};
|
||||
use rustc::hir::intravisit::{NestedVisitorMap, Visitor};
|
||||
use syntax::ast::{self, Attribute, LitKind, NodeId, DUMMY_NODE_ID};
|
||||
use syntax::codemap::Span;
|
||||
use syntax::ast::{self, Attribute, LitKind, DUMMY_NODE_ID};
|
||||
use std::collections::HashMap;
|
||||
|
||||
/// **What it does:** Generates clippy code that detects the offending pattern
|
||||
|
@ -171,6 +170,12 @@ impl PrintVisitor {
|
|||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn print_qpath(&mut self, path: &QPath) {
|
||||
print!(" if match_qpath({}, &[", self.current);
|
||||
print_path(path, &mut true);
|
||||
println!("]);");
|
||||
}
|
||||
}
|
||||
|
||||
struct PrintVisitor {
|
||||
|
@ -260,9 +265,17 @@ impl<'tcx> Visitor<'tcx> for PrintVisitor {
|
|||
},
|
||||
}
|
||||
},
|
||||
Expr_::ExprCast(ref expr, ref _ty) => {
|
||||
Expr_::ExprCast(ref expr, ref ty) => {
|
||||
let cast_pat = self.next("expr");
|
||||
println!("Cast(ref {}, _) = {};", cast_pat, current);
|
||||
let cast_ty = self.next("cast_ty");
|
||||
let qp_label = self.next("qp");
|
||||
|
||||
println!("Cast(ref {}, ref {}) = {};", cast_pat, cast_ty, current);
|
||||
if let Ty_::TyPath(ref qp) = ty.node {
|
||||
println!(" if let Ty_::TyPath(ref {}) = {}.node;", qp_label, cast_ty);
|
||||
self.current = qp_label;
|
||||
self.print_qpath(&qp);
|
||||
}
|
||||
self.current = cast_pat;
|
||||
self.visit_expr(expr);
|
||||
},
|
||||
|
@ -376,7 +389,7 @@ impl<'tcx> Visitor<'tcx> for PrintVisitor {
|
|||
let path_pat = self.next("path");
|
||||
println!("Path(ref {}) = {};", path_pat, current);
|
||||
self.current = path_pat;
|
||||
self.visit_qpath(path, expr.id, expr.span);
|
||||
self.print_qpath(path);
|
||||
},
|
||||
Expr_::ExprAddrOf(mutability, ref inner) => {
|
||||
let inner_pat = self.next("inner");
|
||||
|
@ -431,7 +444,7 @@ impl<'tcx> Visitor<'tcx> for PrintVisitor {
|
|||
println!("Struct(ref {}, ref {}, None) = {};", path_pat, fields_pat, current);
|
||||
}
|
||||
self.current = path_pat;
|
||||
self.visit_qpath(path, expr.id, expr.span);
|
||||
self.print_qpath(path);
|
||||
println!(" if {}.len() == {};", fields_pat, fields.len());
|
||||
println!(" // unimplemented: field checks");
|
||||
},
|
||||
|
@ -446,11 +459,6 @@ impl<'tcx> Visitor<'tcx> for PrintVisitor {
|
|||
}
|
||||
}
|
||||
|
||||
fn visit_qpath(&mut self, path: &QPath, _: NodeId, _: Span) {
|
||||
print!(" if match_qpath({}, &[", self.current);
|
||||
print_path(path, &mut true);
|
||||
println!("]);");
|
||||
}
|
||||
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
|
||||
NestedVisitorMap::None
|
||||
}
|
||||
|
|
7
tests/ui/author.rs
Normal file
7
tests/ui/author.rs
Normal file
|
@ -0,0 +1,7 @@
|
|||
#![feature(plugin, custom_attribute)]
|
||||
|
||||
fn main() {
|
||||
|
||||
#[clippy(author)]
|
||||
let x: char = 0x45 as char;
|
||||
}
|
10
tests/ui/author.stdout
Normal file
10
tests/ui/author.stdout
Normal file
|
@ -0,0 +1,10 @@
|
|||
if_chain! {
|
||||
if let Expr_::ExprCast(ref expr, ref cast_ty) = stmt.node;
|
||||
if let Ty_::TyPath(ref qp) = cast_ty.node;
|
||||
if match_qpath(qp, &["char"]);
|
||||
if let Expr_::ExprLit(ref lit) = expr.node;
|
||||
if let LitKind::Int(69, _) = lit.node;
|
||||
then {
|
||||
// report your lint here
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue