de-closured the item name getter

This commit is contained in:
Andre Bogus
2015-09-06 21:03:09 +02:00
parent 87e6099ad7
commit 468b410d04
3 changed files with 13 additions and 12 deletions

View File

@@ -5,7 +5,7 @@ use syntax::codemap::{Span, Spanned};
use rustc::middle::def_id::DefId; use rustc::middle::def_id::DefId;
use rustc::middle::ty::{self, MethodTraitItemId, ImplOrTraitItemId}; use rustc::middle::ty::{self, MethodTraitItemId, ImplOrTraitItemId};
use utils::{snippet, span_lint, walk_ptrs_ty, with_item_name}; use utils::{get_item_name, snippet, span_lint, walk_ptrs_ty};
declare_lint!(pub LEN_ZERO, Warn, declare_lint!(pub LEN_ZERO, Warn,
"checking `.len() == 0` or `.len() > 0` (or similar) when `.is_empty()` \ "checking `.len() == 0` or `.len() > 0` (or similar) when `.is_empty()` \
@@ -91,8 +91,8 @@ fn is_self_sig(sig: &MethodSig) -> bool {
fn check_cmp(cx: &Context, span: Span, left: &Expr, right: &Expr, op: &str) { fn check_cmp(cx: &Context, span: Span, left: &Expr, right: &Expr, op: &str) {
// check if we are in an is_empty() method // check if we are in an is_empty() method
if let Some(true) = with_item_name(cx, left, |n| n == "is_empty") { if let Some(name) = get_item_name(cx, left) {
return; if name == "is_empty" { return; }
} }
match (&left.node, &right.node) { match (&left.node, &right.node) {
(&ExprLit(ref lit), &ExprMethodCall(ref method, _, ref args)) => (&ExprLit(ref lit), &ExprMethodCall(ref method, _, ref args)) =>

View File

@@ -7,7 +7,7 @@ use syntax::codemap::{Span, Spanned};
use rustc_front::visit::FnKind; use rustc_front::visit::FnKind;
use rustc::middle::ty; use rustc::middle::ty;
use utils::{match_path, snippet, span_lint, walk_ptrs_ty, with_item_name}; use utils::{get_item_name, match_path, snippet, span_lint, walk_ptrs_ty};
use consts::constant; use consts::constant;
declare_lint!(pub TOPLEVEL_REF_ARG, Warn, declare_lint!(pub TOPLEVEL_REF_ARG, Warn,
@@ -92,12 +92,13 @@ impl LintPass for FloatCmp {
false, |c| c.0.as_float().map_or(false, |f| f == 0.0)) { false, |c| c.0.as_float().map_or(false, |f| f == 0.0)) {
return; return;
} }
if let Some(true) = with_item_name(cx, expr, |name| if let Some(name) = get_item_name(cx, expr) {
name == "eq" || name == "ne" || if name == "eq" || name == "ne" ||
name.as_str().starts_with("eq_") || name.as_str().starts_with("eq_") ||
name.as_str().ends_with("_eq")) { name.as_str().ends_with("_eq") {
return; return;
} }
}
span_lint(cx, FLOAT_CMP, expr.span, &format!( span_lint(cx, FLOAT_CMP, expr.span, &format!(
"{}-comparison of f32 or f64 detected. Consider changing this to \ "{}-comparison of f32 or f64 detected. Consider changing this to \
`abs({} - {}) < epsilon` for some suitable value of epsilon", `abs({} - {}) < epsilon` for some suitable value of epsilon",

View File

@@ -100,14 +100,14 @@ pub fn match_path(path: &Path, segments: &[&str]) -> bool {
|(a, b)| &a.identifier.name == b) |(a, b)| &a.identifier.name == b)
} }
pub fn with_item_name<T, F>(cx: &Context, expr: &Expr, f: F) -> Option<T> /// get the name of the item the expression is in, if available
where F: FnOnce(Name) -> T { pub fn get_item_name(cx: &Context, expr: &Expr) -> Option<Name> {
let parent_id = cx.tcx.map.get_parent(expr.id); let parent_id = cx.tcx.map.get_parent(expr.id);
match cx.tcx.map.find(parent_id) { match cx.tcx.map.find(parent_id) {
Some(NodeItem(&Item{ ref ident, .. })) | Some(NodeItem(&Item{ ref ident, .. })) |
Some(NodeTraitItem(&TraitItem{ id: _, ref ident, .. })) | Some(NodeTraitItem(&TraitItem{ id: _, ref ident, .. })) |
Some(NodeImplItem(&ImplItem{ id: _, ref ident, .. })) => { Some(NodeImplItem(&ImplItem{ id: _, ref ident, .. })) => {
Some(f(ident.name)) Some(ident.name)
}, },
_ => None, _ => None,
} }