From cf96042c65b4c232d0a73f6a8514e59e0358719b Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Tue, 11 Aug 2015 20:57:21 +0200 Subject: [PATCH] move walk_ty() to utils module and rename to walk_ptrs_ty --- src/len_zero.rs | 5 ++--- src/misc.rs | 15 ++++----------- src/strings.rs | 5 ++--- src/utils.rs | 8 ++++++++ 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/len_zero.rs b/src/len_zero.rs index 0877fa95238..8aa4c626760 100644 --- a/src/len_zero.rs +++ b/src/len_zero.rs @@ -9,8 +9,7 @@ use rustc::middle::ty::{self, TypeVariants, TypeAndMut, MethodTraitItemId, ImplO use rustc::middle::def::{DefTy, DefStruct, DefTrait}; use syntax::codemap::{Span, Spanned}; use syntax::ast::*; -use misc::walk_ty; -use utils::span_lint; +use utils::{span_lint, walk_ptrs_ty}; declare_lint!(pub LEN_ZERO, Warn, "Warn when .is_empty() could be used instead of checking .len()"); @@ -136,7 +135,7 @@ fn has_is_empty(cx: &Context, expr: &Expr) -> bool { |iids| iids.iter().any(|i| is_is_empty(cx, i))))) } - let ty = &walk_ty(&cx.tcx.expr_ty(expr)); + let ty = &walk_ptrs_ty(&cx.tcx.expr_ty(expr)); match ty.sty { ty::TyTrait(_) => cx.tcx.trait_item_def_ids.borrow().get( &ty.ty_to_def_id().expect("trait impl not found")).map_or(false, diff --git a/src/misc.rs b/src/misc.rs index 444062a5919..a1b3dcb32b8 100644 --- a/src/misc.rs +++ b/src/misc.rs @@ -7,14 +7,7 @@ use rustc::lint::{Context, LintPass, LintArray, Lint, Level}; use rustc::middle::ty; use syntax::codemap::{Span, Spanned}; -use utils::{match_path, snippet, span_lint, span_help_and_lint}; - -pub fn walk_ty<'t>(ty: ty::Ty<'t>) -> ty::Ty<'t> { - match ty.sty { - ty::TyRef(_, ref tm) | ty::TyRawPtr(ref tm) => walk_ty(tm.ty), - _ => ty - } -} +use utils::{match_path, snippet, span_lint, span_help_and_lint, walk_ptrs_ty}; /// Handles uncategorized lints /// Currently handles linting of if-let-able matches @@ -87,7 +80,7 @@ impl LintPass for StrToStringPass { } fn is_str(cx: &Context, expr: &ast::Expr) -> bool { - match walk_ty(cx.tcx.expr_ty(expr)).sty { + match walk_ptrs_ty(cx.tcx.expr_ty(expr)).sty { ty::TyStr => true, _ => false } @@ -175,7 +168,7 @@ impl LintPass for FloatCmp { } fn is_float(cx: &Context, expr: &Expr) -> bool { - if let ty::TyFloat(_) = walk_ty(cx.tcx.expr_ty(expr)).sty { + if let ty::TyFloat(_) = walk_ptrs_ty(cx.tcx.expr_ty(expr)).sty { true } else { false @@ -274,7 +267,7 @@ fn check_to_owned(cx: &Context, expr: &Expr, other_span: Span) { fn is_str_arg(cx: &Context, args: &[P]) -> bool { args.len() == 1 && if let ty::TyStr = - walk_ty(cx.tcx.expr_ty(&*args[0])).sty { true } else { false } + walk_ptrs_ty(cx.tcx.expr_ty(&*args[0])).sty { true } else { false } } declare_lint!(pub NEEDLESS_RETURN, Warn, diff --git a/src/strings.rs b/src/strings.rs index 3384eed8da5..b6bc7654e47 100644 --- a/src/strings.rs +++ b/src/strings.rs @@ -8,9 +8,8 @@ use rustc::middle::ty::TypeVariants::TyStruct; use syntax::ast::*; use syntax::codemap::{Span, Spanned}; use eq_op::is_exp_equal; -use misc::walk_ty; use types::match_ty_unwrap; -use utils::{match_def_path, span_lint}; +use utils::{match_def_path, span_lint, walk_ptrs_ty}; declare_lint! { pub STRING_ADD_ASSIGN, @@ -38,7 +37,7 @@ impl LintPass for StringAdd { } fn is_string(cx: &Context, e: &Expr) -> bool { - if let TyStruct(did, _) = walk_ty(cx.tcx.expr_ty(e)).sty { + if let TyStruct(did, _) = walk_ptrs_ty(cx.tcx.expr_ty(e)).sty { match_def_path(cx, did.did, &["std", "string", "String"]) } else { false } } diff --git a/src/utils.rs b/src/utils.rs index 9b3b94e113e..107f5c6f99b 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -84,3 +84,11 @@ pub fn span_help_and_lint(cx: &Context, lint: &'static Lint, span: Span, cx.sess().fileline_help(span, help); } } + +/// return the base type for references and raw pointers +pub fn walk_ptrs_ty<'t>(ty: ty::Ty<'t>) -> ty::Ty<'t> { + match ty.sty { + ty::TyRef(_, ref tm) | ty::TyRawPtr(ref tm) => walk_ptrs_ty(tm.ty), + _ => ty + } +}