fork/rust
git clone https://git.julia.blue/fork/rust
3bcad65fbf6
master
1#![warn(clippy::explicit_deref_methods)] 2#![allow(unused_variables, unused_must_use)] 3#![allow( 4 clippy::borrow_deref_ref, 5 suspicious_double_ref_op, 6 noop_method_call, 7 clippy::explicit_auto_deref, 8 clippy::needless_borrow, 9 clippy::no_effect, 10 clippy::uninlined_format_args, 11 clippy::unnecessary_literal_unwrap 12)] 13 14use std::ops::{Deref, DerefMut}; 15 16fn concat(deref_str: &str) -> String { 17 format!("{}bar", deref_str) 18} 19 20fn just_return(deref_str: &str) -> &str { 21 deref_str 22} 23 24struct CustomVec(Vec<u8>); 25impl Deref for CustomVec { 26 type Target = Vec<u8>; 27 28 fn deref(&self) -> &Vec<u8> { 29 &self.0 30 } 31} 32 33struct Aaa; 34 35impl Deref for Aaa { 36 type Target = (); 37 38 fn deref(&self) -> &Self::Target { 39 todo!(); 40 } 41} 42 43impl DerefMut for Aaa { 44 fn deref_mut(&mut self) -> &mut Self::Target { 45 todo!(); 46 } 47} 48 49fn main() { 50 let a: &mut String = &mut String::from("foo"); 51 52 // these should require linting 53 54 let b: &str = &*a; 55 56 let b: &mut str = &mut **a; 57 58 // both derefs should get linted here 59 let b: String = format!("{}, {}", &*a, &*a); 60 61 println!("{}", &*a); 62 63 #[allow(clippy::match_single_binding)] 64 match &*a { 65 _ => (), 66 } 67 68 let b: String = concat(&*a); 69 70 let b = just_return(a); 71 72 let b: String = concat(just_return(a)); 73 74 let b: &str = &**a; 75 76 let opt_a = Some(a.clone()); 77 let b = &*opt_a.unwrap(); 78 79 // make sure `Aaa::deref` instead of `aaa.deref()` is not linted, as well as fully qualified 80 // syntax 81 82 Aaa::deref(&Aaa); 83 Aaa::deref_mut(&mut Aaa); 84 <Aaa as Deref>::deref(&Aaa); 85 <Aaa as DerefMut>::deref_mut(&mut Aaa); 86 let mut aaa = Aaa; 87 Aaa::deref(&aaa); 88 Aaa::deref_mut(&mut aaa); 89 90 // following should not require linting 91 92 let cv = CustomVec(vec![0, 42]); 93 let c = cv.deref()[0]; 94 95 let b: &str = &*a.deref(); 96 97 let b: String = a.deref().clone(); 98 99 let b: usize = a.deref_mut().len(); 100 101 let b: &usize = &a.deref().len(); 102 103 let b: &str = &*a; 104 105 let b: &mut str = &mut *a; 106 107 macro_rules! expr_deref { 108 ($body:expr) => { 109 $body.deref() 110 }; 111 } 112 let b: &str = expr_deref!(a); 113 114 let b: &str = expr_deref!(&*a); 115 116 // The struct does not implement Deref trait 117 #[derive(Copy, Clone)] 118 struct NoLint(u32); 119 impl NoLint { 120 pub fn deref(self) -> u32 { 121 self.0 122 } 123 pub fn deref_mut(self) -> u32 { 124 self.0 125 } 126 } 127 let no_lint = NoLint(42); 128 let b = no_lint.deref(); 129 let b = no_lint.deref_mut(); 130}