]> git.lizzy.rs Git - rust.git/blob - tests/ui/ptr_arg.rs
Merge pull request #2985 from phansch/riir_update_lints
[rust.git] / tests / ui / ptr_arg.rs
1 #![feature(tool_lints)]
2
3 #![allow(unused, clippy::many_single_char_names)]
4 #![warn(clippy::ptr_arg)]
5
6 use std::borrow::Cow;
7
8 fn do_vec(x: &Vec<i64>) {
9     //Nothing here
10 }
11
12 fn do_vec_mut(x: &mut Vec<i64>) { // no error here
13     //Nothing here
14 }
15
16 fn do_str(x: &String) {
17     //Nothing here either
18 }
19
20 fn do_str_mut(x: &mut String) { // no error here
21     //Nothing here either
22 }
23
24 fn main() {
25 }
26
27 trait Foo {
28     type Item;
29     fn do_vec(x: &Vec<i64>);
30     fn do_item(x: &Self::Item);
31 }
32
33 struct Bar;
34
35 // no error, in trait impl (#425)
36 impl Foo for Bar {
37     type Item = Vec<u8>;
38     fn do_vec(x: &Vec<i64>) {}
39     fn do_item(x: &Vec<u8>) {}
40 }
41
42 fn cloned(x: &Vec<u8>) -> Vec<u8> {
43     let e = x.clone();
44     let f = e.clone(); // OK
45     let g = x;
46     let h = g.clone(); // Alas, we cannot reliably detect this without following data.
47     let i = (e).clone();
48     x.clone()
49 }
50
51 fn str_cloned(x: &String) -> String {
52     let a = x.clone();
53     let b = x.clone();
54     let c = b.clone();
55     let d = a.clone()
56              .clone()
57              .clone();
58     x.clone()
59 }
60
61 fn false_positive_capacity(x: &Vec<u8>, y: &String) {
62     let a = x.capacity();
63     let b = y.clone();
64     let c = y.as_str();
65 }
66
67 fn false_positive_capacity_too(x: &String) -> String {
68     if x.capacity() > 1024 { panic!("Too large!"); }
69     x.clone()
70 }
71
72 #[allow(dead_code)]
73 fn test_cow_with_ref(c: &Cow<[i32]>) {
74 }
75
76 #[allow(dead_code)]
77 fn test_cow(c: Cow<[i32]>) {
78     let _c = c;
79 }
80
81 trait Foo2 {
82     fn do_string(&self);
83 }
84
85 // no error for &self references where self is of type String (#2293)
86 impl Foo2 for String { fn do_string(&self) {} }