Skip to content

Commit e3c13da

Browse files
committed
Change recomendation to: &[type] from Cow<type>
1 parent fc5b377 commit e3c13da

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

clippy_lints/src/ptr.rs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use std::borrow::Cow;
44
use rustc::hir::*;
55
use rustc::hir::map::NodeItem;
6+
use rustc::hir::QPath;
67
use rustc::lint::*;
78
use rustc::ty;
89
use syntax::ast::NodeId;
@@ -214,20 +215,32 @@ fn check_fn(cx: &LateContext, decl: &FnDecl, fn_id: NodeId, opt_body_id: Option<
214215
);
215216
}
216217
} else if match_type(cx, ty, &paths::COW) {
217-
let as_str = format!("{}", snippet_opt(cx, arg.span).unwrap());
218-
let mut cc = as_str.chars();
219-
cc.next();
220-
let replacement: String = cc.collect();
221-
222-
span_lint_and_then(
223-
cx,
224-
PTR_ARG,
225-
arg.span,
226-
"using a reference to `Cow` is not recommended.",
227-
|db| {
228-
db.span_suggestion(arg.span, "change this to", replacement);
229-
},
230-
);
218+
if_chain! {
219+
if let TyRptr(_, MutTy { ref ty, ..} ) = arg.node;
220+
if let TyPath(ref path) = ty.node;
221+
if let QPath::Resolved(None, ref pp) = *path;
222+
if let [ref bx] = *pp.segments;
223+
if let Some(ref params) = bx.parameters;
224+
if !params.parenthesized;
225+
if let [ref inner] = *params.types;
226+
then {
227+
let replacement = snippet_opt(cx, inner.span);
228+
match replacement {
229+
Some(r) => {
230+
span_lint_and_then(
231+
cx,
232+
PTR_ARG,
233+
arg.span,
234+
"using a reference to `Cow` is not recommended.",
235+
|db| {
236+
db.span_suggestion(arg.span, "change this to", "&".to_owned() + &r);
237+
},
238+
);
239+
},
240+
None => (),
241+
}
242+
}
243+
}
231244
}
232245
}
233246
}

tests/ui/needless_borrow.stderr

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,11 @@ error: this pattern creates a reference to a reference
3838
50 | let _ = v.iter().filter(|&ref a| a.is_empty());
3939
| ^^^^^ help: change this to: `a`
4040

41-
a> $DIR/needless_borrow.rs:56:25: 56:36
42-
b> $DIR/needless_borrow.rs:56:25: 56:36
4341
error: using a reference to `Cow` is not recommended.
4442
--> $DIR/needless_borrow.rs:56:25
4543
|
4644
56 | fn test_cow_with_ref(c: &Cow<[i32]>) {
47-
| ^^^^^^^^^^^ help: change this to: `Cow<[i32]>`
45+
| ^^^^^^^^^^^ help: change this to: `&[i32]`
4846
|
4947
= note: `-D ptr-arg` implied by `-D warnings`
5048

0 commit comments

Comments
 (0)