]> git.lizzy.rs Git - rust.git/blob - tests/ui/dereference.fixed
iterate List by value
[rust.git] / tests / ui / dereference.fixed
1 // run-rustfix
2
3 #![allow(unused_variables, clippy::many_single_char_names, clippy::clone_double_ref)]
4 #![warn(clippy::explicit_deref_methods)]
5
6 use std::ops::{Deref, DerefMut};
7
8 fn concat(deref_str: &str) -> String {
9     format!("{}bar", deref_str)
10 }
11
12 fn just_return(deref_str: &str) -> &str {
13     deref_str
14 }
15
16 struct CustomVec(Vec<u8>);
17 impl Deref for CustomVec {
18     type Target = Vec<u8>;
19
20     fn deref(&self) -> &Vec<u8> {
21         &self.0
22     }
23 }
24
25 fn main() {
26     let a: &mut String = &mut String::from("foo");
27
28     // these should require linting
29
30     let b: &str = &*a;
31
32     let b: &mut str = &mut *a;
33
34     // both derefs should get linted here
35     let b: String = format!("{}, {}", &*a, &*a);
36
37     println!("{}", &*a);
38
39     #[allow(clippy::match_single_binding)]
40     match &*a {
41         _ => (),
42     }
43
44     let b: String = concat(&*a);
45
46     let b = &*just_return(a);
47
48     let b: String = concat(&*just_return(a));
49
50     let b: &str = &*a.deref();
51
52     let opt_a = Some(a.clone());
53     let b = &*opt_a.unwrap();
54
55     // following should not require linting
56
57     let cv = CustomVec(vec![0, 42]);
58     let c = cv.deref()[0];
59
60     let b: &str = &*a.deref();
61
62     let b: String = a.deref().clone();
63
64     let b: usize = a.deref_mut().len();
65
66     let b: &usize = &a.deref().len();
67
68     let b: &str = &*a;
69
70     let b: &mut str = &mut *a;
71
72     macro_rules! expr_deref {
73         ($body:expr) => {
74             $body.deref()
75         };
76     }
77     let b: &str = expr_deref!(a);
78
79     // The struct does not implement Deref trait
80     #[derive(Copy, Clone)]
81     struct NoLint(u32);
82     impl NoLint {
83         pub fn deref(self) -> u32 {
84             self.0
85         }
86         pub fn deref_mut(self) -> u32 {
87             self.0
88         }
89     }
90     let no_lint = NoLint(42);
91     let b = no_lint.deref();
92     let b = no_lint.deref_mut();
93 }