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