]> git.lizzy.rs Git - rust.git/blob - tests/ui/ptr_arg.rs
rustfmt tests
[rust.git] / tests / ui / ptr_arg.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 #![allow(unused, clippy::many_single_char_names)]
11 #![warn(clippy::ptr_arg)]
12
13 use std::borrow::Cow;
14
15 fn do_vec(x: &Vec<i64>) {
16     //Nothing here
17 }
18
19 fn do_vec_mut(x: &mut Vec<i64>) {
20     // no error here
21     //Nothing here
22 }
23
24 fn do_str(x: &String) {
25     //Nothing here either
26 }
27
28 fn do_str_mut(x: &mut String) {
29     // no error here
30     //Nothing here either
31 }
32
33 fn main() {}
34
35 trait Foo {
36     type Item;
37     fn do_vec(x: &Vec<i64>);
38     fn do_item(x: &Self::Item);
39 }
40
41 struct Bar;
42
43 // no error, in trait impl (#425)
44 impl Foo for Bar {
45     type Item = Vec<u8>;
46     fn do_vec(x: &Vec<i64>) {}
47     fn do_item(x: &Vec<u8>) {}
48 }
49
50 fn cloned(x: &Vec<u8>) -> Vec<u8> {
51     let e = x.clone();
52     let f = e.clone(); // OK
53     let g = x;
54     let h = g.clone(); // Alas, we cannot reliably detect this without following data.
55     let i = (e).clone();
56     x.clone()
57 }
58
59 fn str_cloned(x: &String) -> String {
60     let a = x.clone();
61     let b = x.clone();
62     let c = b.clone();
63     let d = a.clone().clone().clone();
64     x.clone()
65 }
66
67 fn false_positive_capacity(x: &Vec<u8>, y: &String) {
68     let a = x.capacity();
69     let b = y.clone();
70     let c = y.as_str();
71 }
72
73 fn false_positive_capacity_too(x: &String) -> String {
74     if x.capacity() > 1024 {
75         panic!("Too large!");
76     }
77     x.clone()
78 }
79
80 #[allow(dead_code)]
81 fn test_cow_with_ref(c: &Cow<[i32]>) {}
82
83 #[allow(dead_code)]
84 fn test_cow(c: Cow<[i32]>) {
85     let _c = c;
86 }
87
88 trait Foo2 {
89     fn do_string(&self);
90 }
91
92 // no error for &self references where self is of type String (#2293)
93 impl Foo2 for String {
94     fn do_string(&self) {}
95 }