]> git.lizzy.rs Git - rust.git/blob - tests/ui/dereference.rs
Report using stmts and expr + tests
[rust.git] / tests / ui / dereference.rs
1 #![allow(unused_variables, clippy::many_single_char_names, clippy::clone_double_ref)]
2 #![warn(clippy::explicit_deref_method)]
3
4 use std::ops::{Deref, DerefMut};
5
6 fn concat(deref_str: &str) -> String {
7     format!("{}bar", deref_str)
8 }
9
10 fn just_return(deref_str: &str) -> &str {
11     deref_str
12 }
13
14 fn main() {
15     let a: &mut String = &mut String::from("foo");
16
17     // these should require linting
18
19     let b: &str = a.deref();
20
21     let b: &mut str = a.deref_mut();
22
23     // both derefs should get linted here
24     let b: String = format!("{}, {}", a.deref(), a.deref());
25
26     println!("{}", a.deref());
27
28     #[allow(clippy::match_single_binding)]
29     match a.deref() {
30         _ => (),
31     }
32
33     let b: String = concat(a.deref());
34
35     // following should not require linting
36
37     let b = just_return(a).deref();
38
39     let b: String = concat(just_return(a).deref());
40
41     let b: String = a.deref().clone();
42
43     let b: usize = a.deref_mut().len();
44
45     let b: &usize = &a.deref().len();
46
47     let b: &str = a.deref().deref();
48
49     let b: &str = &*a;
50
51     let b: &mut str = &mut *a;
52
53     macro_rules! expr_deref {
54         ($body:expr) => {
55             $body.deref()
56         };
57     }
58     let b: &str = expr_deref!(a);
59
60     let opt_a = Some(a);
61     let b = opt_a.unwrap().deref();
62 }