diff --git a/src/misc.rs b/src/misc.rs index 5f5b07a15b7..b8ce6d725f4 100644 --- a/src/misc.rs +++ b/src/misc.rs @@ -235,11 +235,12 @@ impl LintPass for CmpOwned { fn check_to_owned(cx: &Context, expr: &Expr, other_span: Span) { match &expr.node { - &ExprMethodCall(Spanned{node: ref ident, ..}, _, _) => { + &ExprMethodCall(Spanned{node: ref ident, ..}, _, ref args) => { let name = ident.as_str(); - if name == "to_string" || name == "to_owned" { + if name == "to_string" || + name == "to_owned" && is_str_arg(cx, args) { cx.span_lint(CMP_OWNED, expr.span, &format!( - "this creates an owned instance just for comparison. + "this creates an owned instance just for comparison. \ Consider using {}.as_slice() to compare without allocation", cx.sess().codemap().span_to_snippet(other_span).unwrap_or( "..".to_string()))) @@ -250,7 +251,7 @@ fn check_to_owned(cx: &Context, expr: &Expr, other_span: Span) { if path.segments.iter().zip(["String", "from_str"].iter()).all( |(seg, name)| &seg.identifier.as_str() == name) { cx.span_lint(CMP_OWNED, expr.span, &format!( - "this creates an owned instance just for comparison. + "this creates an owned instance just for comparison. \ Consider using {}.as_slice() to compare without allocation", cx.sess().codemap().span_to_snippet(other_span).unwrap_or( "..".to_string()))) @@ -260,3 +261,8 @@ 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_str = + walk_ty(expr_ty(cx.tcx, &*args[0])).sty { true } else { false } +}