From 0e5b62c8d83046595f1be9231af3564644c4cfc1 Mon Sep 17 00:00:00 2001 From: llogiq Date: Thu, 11 Jun 2015 16:53:23 +0200 Subject: [PATCH] also included String::from in cmp_owned and fixed deprecation in test --- src/misc.rs | 6 +++--- tests/compile-fail/cmp_owned.rs | 8 +++++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/misc.rs b/src/misc.rs index 5d0d79544e9..ec609fd2618 100644 --- a/src/misc.rs +++ b/src/misc.rs @@ -7,8 +7,8 @@ use rustc::lint::{Context, LintPass, LintArray, Lint, Level}; use rustc::middle::ty::{self, expr_ty, ty_str, ty_ptr, ty_rptr, ty_float}; use syntax::codemap::{Span, Spanned}; - use types::span_note_and_lint; +use utils::match_path; pub fn walk_ty<'t>(ty: ty::Ty<'t>) -> ty::Ty<'t> { match ty.sty { @@ -248,8 +248,8 @@ fn check_to_owned(cx: &Context, expr: &Expr, other_span: Span) { }, &ExprCall(ref path, _) => { if let &ExprPath(None, ref path) = &path.node { - if path.segments.iter().zip(["String", "from_str"].iter()).all( - |(seg, name)| &seg.identifier.as_str() == name) { + if match_path(path, &["String", "from_str"]) || + match_path(path, &["String", "from"]) { cx.span_lint(CMP_OWNED, expr.span, &format!( "this creates an owned instance just for comparison. \ Consider using {}.as_slice() to compare without allocation", diff --git a/tests/compile-fail/cmp_owned.rs b/tests/compile-fail/cmp_owned.rs index a8b0cb32f6f..d7399e6d3aa 100644 --- a/tests/compile-fail/cmp_owned.rs +++ b/tests/compile-fail/cmp_owned.rs @@ -13,5 +13,11 @@ fn main() { x != "foo".to_owned(); //~ERROR this creates an owned instance - x != String::from_str("foo"); //~ERROR this creates an owned instance + #[allow(deprecated)] // for from_str + fn old_timey(x : &str) { + x != String::from_str("foo"); //~ERROR this creates an owned instance + } + old_timey(x); + + x != String::from("foo"); //~ERROR this creates an owned instance }