From 468b410d04a486bf9208be5136feee93d3818c21 Mon Sep 17 00:00:00 2001 From: Andre Bogus Date: Sun, 6 Sep 2015 21:03:09 +0200 Subject: [PATCH] de-closured the item name getter --- src/len_zero.rs | 6 +++--- src/misc.rs | 13 +++++++------ src/utils.rs | 6 +++--- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/len_zero.rs b/src/len_zero.rs index 206f18217d1..45b9c844af3 100644 --- a/src/len_zero.rs +++ b/src/len_zero.rs @@ -5,7 +5,7 @@ use syntax::codemap::{Span, Spanned}; use rustc::middle::def_id::DefId; 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, "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) { // check if we are in an is_empty() method - if let Some(true) = with_item_name(cx, left, |n| n == "is_empty") { - return; + if let Some(name) = get_item_name(cx, left) { + if name == "is_empty" { return; } } match (&left.node, &right.node) { (&ExprLit(ref lit), &ExprMethodCall(ref method, _, ref args)) => diff --git a/src/misc.rs b/src/misc.rs index 87cc512f819..8891b000b59 100644 --- a/src/misc.rs +++ b/src/misc.rs @@ -7,7 +7,7 @@ use syntax::codemap::{Span, Spanned}; use rustc_front::visit::FnKind; 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; declare_lint!(pub TOPLEVEL_REF_ARG, Warn, @@ -92,11 +92,12 @@ impl LintPass for FloatCmp { false, |c| c.0.as_float().map_or(false, |f| f == 0.0)) { return; } - if let Some(true) = with_item_name(cx, expr, |name| - name == "eq" || name == "ne" || - name.as_str().starts_with("eq_") || - name.as_str().ends_with("_eq")) { - return; + if let Some(name) = get_item_name(cx, expr) { + if name == "eq" || name == "ne" || + name.as_str().starts_with("eq_") || + name.as_str().ends_with("_eq") { + return; + } } span_lint(cx, FLOAT_CMP, expr.span, &format!( "{}-comparison of f32 or f64 detected. Consider changing this to \ diff --git a/src/utils.rs b/src/utils.rs index c781b74b359..d6e529048c9 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -100,14 +100,14 @@ pub fn match_path(path: &Path, segments: &[&str]) -> bool { |(a, b)| &a.identifier.name == b) } -pub fn with_item_name(cx: &Context, expr: &Expr, f: F) -> Option -where F: FnOnce(Name) -> T { +/// get the name of the item the expression is in, if available +pub fn get_item_name(cx: &Context, expr: &Expr) -> Option { let parent_id = cx.tcx.map.get_parent(expr.id); match cx.tcx.map.find(parent_id) { Some(NodeItem(&Item{ ref ident, .. })) | Some(NodeTraitItem(&TraitItem{ id: _, ref ident, .. })) | Some(NodeImplItem(&ImplItem{ id: _, ref ident, .. })) => { - Some(f(ident.name)) + Some(ident.name) }, _ => None, }