also included String::from in cmp_owned and fixed deprecation in test

This commit is contained in:
llogiq 2015-06-11 16:53:23 +02:00
parent 23caf3cccc
commit 0e5b62c8d8
2 changed files with 10 additions and 4 deletions

View File

@ -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",

View File

@ -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
}