Lint passing Cow by reference

Add lint for reference to Cow to the same place in the code where
lint for reference to String lives.

https://github.com/rust-lang-nursery/rust-clippy/issues/2405
This commit is contained in:
bootandy 2018-02-28 10:24:10 -05:00
parent adb8f7ee4b
commit 9a002e52e5
3 changed files with 36 additions and 2 deletions

View File

@ -213,6 +213,21 @@ fn check_fn(cx: &LateContext, decl: &FnDecl, fn_id: NodeId, opt_body_id: Option<
},
);
}
} else if match_type(cx, ty, &paths::COW) {
let as_str = format!("{}", snippet_opt(cx, arg.span).unwrap());
let mut cc = as_str.chars();
cc.next();
let replacement: String = cc.collect();
span_lint_and_then(
cx,
PTR_ARG,
arg.span,
"using a reference to `Cow` is not recommended.",
|db| {
db.span_suggestion(arg.span, "change this to", replacement);
},
);
}
}
}

View File

@ -1,4 +1,4 @@
use std::borrow::Cow;
fn x(y: &i32) -> i32 {
@ -51,3 +51,12 @@ fn issue_1432() {
let _ = v.iter().filter(|&a| a.is_empty());
}
#[allow(dead_code)]
fn test_cow_with_ref(c: &Cow<[i32]>) {
}
#[allow(dead_code)]
fn test_cow(c: Cow<[i32]>) {
let _c = c;
}

View File

@ -38,5 +38,15 @@ error: this pattern creates a reference to a reference
50 | let _ = v.iter().filter(|&ref a| a.is_empty());
| ^^^^^ help: change this to: `a`
error: aborting due to 6 previous errors
a> $DIR/needless_borrow.rs:56:25: 56:36
b> $DIR/needless_borrow.rs:56:25: 56:36
error: using a reference to `Cow` is not recommended.
--> $DIR/needless_borrow.rs:56:25
|
56 | fn test_cow_with_ref(c: &Cow<[i32]>) {
| ^^^^^^^^^^^ help: change this to: `Cow<[i32]>`
|
= note: `-D ptr-arg` implied by `-D warnings`
error: aborting due to 7 previous errors