Merge pull request #65 from Manishearth/cmp_owned

check for str type of .to_owned() callee
This commit is contained in:
llogiq 2015-05-21 16:41:57 +02:00
commit 450c0fb40c

View file

@ -235,9 +235,10 @@ 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. \
Consider using {}.as_slice() to compare without allocation",
@ -260,3 +261,8 @@ fn check_to_owned(cx: &Context, expr: &Expr, other_span: Span) {
_ => ()
}
}
fn is_str_arg(cx: &Context, args: &[P<Expr>]) -> bool {
args.len() == 1 && if let ty_str =
walk_ty(expr_ty(cx.tcx, &*args[0])).sty { true } else { false }
}