2023-04-23 11:03:09 +00:00
|
|
|
//@run-rustfix
|
2022-10-06 07:44:38 +00:00
|
|
|
#![warn(clippy::explicit_deref_methods)]
|
|
|
|
#![allow(unused_variables)]
|
2022-06-04 11:34:07 +00:00
|
|
|
#![allow(
|
2022-10-06 07:44:38 +00:00
|
|
|
clippy::borrow_deref_ref,
|
2022-06-04 11:34:07 +00:00
|
|
|
clippy::clone_double_ref,
|
2022-10-06 07:44:38 +00:00
|
|
|
clippy::explicit_auto_deref,
|
2022-06-04 11:34:07 +00:00
|
|
|
clippy::needless_borrow,
|
2022-10-06 07:44:38 +00:00
|
|
|
clippy::uninlined_format_args
|
2022-06-04 11:34:07 +00:00
|
|
|
)]
|
2018-10-03 16:53:39 +00:00
|
|
|
|
|
|
|
use std::ops::{Deref, DerefMut};
|
|
|
|
|
2020-01-26 18:48:30 +00:00
|
|
|
fn concat(deref_str: &str) -> String {
|
|
|
|
format!("{}bar", deref_str)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn just_return(deref_str: &str) -> &str {
|
|
|
|
deref_str
|
|
|
|
}
|
|
|
|
|
2020-03-21 18:34:56 +00:00
|
|
|
struct CustomVec(Vec<u8>);
|
|
|
|
impl Deref for CustomVec {
|
|
|
|
type Target = Vec<u8>;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Vec<u8> {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-03 16:53:39 +00:00
|
|
|
fn main() {
|
|
|
|
let a: &mut String = &mut String::from("foo");
|
|
|
|
|
|
|
|
// these should require linting
|
2020-01-26 18:48:30 +00:00
|
|
|
|
2020-01-23 15:28:01 +00:00
|
|
|
let b: &str = a.deref();
|
2018-10-03 16:53:39 +00:00
|
|
|
|
2020-01-23 15:28:01 +00:00
|
|
|
let b: &mut str = a.deref_mut();
|
2018-10-03 16:53:39 +00:00
|
|
|
|
2020-01-26 18:48:30 +00:00
|
|
|
// both derefs should get linted here
|
|
|
|
let b: String = format!("{}, {}", a.deref(), a.deref());
|
|
|
|
|
|
|
|
println!("{}", a.deref());
|
|
|
|
|
|
|
|
#[allow(clippy::match_single_binding)]
|
|
|
|
match a.deref() {
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
let b: String = concat(a.deref());
|
|
|
|
|
|
|
|
let b = just_return(a).deref();
|
|
|
|
|
|
|
|
let b: String = concat(just_return(a).deref());
|
|
|
|
|
2020-03-07 14:33:27 +00:00
|
|
|
let b: &str = a.deref().deref();
|
|
|
|
|
|
|
|
let opt_a = Some(a.clone());
|
|
|
|
let b = opt_a.unwrap().deref();
|
|
|
|
|
|
|
|
// following should not require linting
|
|
|
|
|
2020-03-21 18:34:56 +00:00
|
|
|
let cv = CustomVec(vec![0, 42]);
|
|
|
|
let c = cv.deref()[0];
|
|
|
|
|
2020-03-07 14:33:27 +00:00
|
|
|
let b: &str = &*a.deref();
|
|
|
|
|
2020-01-23 15:28:01 +00:00
|
|
|
let b: String = a.deref().clone();
|
2018-10-03 16:53:39 +00:00
|
|
|
|
2020-01-23 15:28:01 +00:00
|
|
|
let b: usize = a.deref_mut().len();
|
2018-10-03 16:53:39 +00:00
|
|
|
|
2020-01-23 15:28:01 +00:00
|
|
|
let b: &usize = &a.deref().len();
|
|
|
|
|
|
|
|
let b: &str = &*a;
|
|
|
|
|
|
|
|
let b: &mut str = &mut *a;
|
2018-10-03 16:53:39 +00:00
|
|
|
|
2020-01-23 15:28:01 +00:00
|
|
|
macro_rules! expr_deref {
|
|
|
|
($body:expr) => {
|
|
|
|
$body.deref()
|
|
|
|
};
|
2018-10-03 16:53:39 +00:00
|
|
|
}
|
2020-01-23 15:28:01 +00:00
|
|
|
let b: &str = expr_deref!(a);
|
2020-01-26 18:48:30 +00:00
|
|
|
|
2021-03-25 18:29:11 +00:00
|
|
|
let b: &str = expr_deref!(a.deref());
|
|
|
|
|
2020-02-25 22:06:24 +00:00
|
|
|
// The struct does not implement Deref trait
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
struct NoLint(u32);
|
|
|
|
impl NoLint {
|
|
|
|
pub fn deref(self) -> u32 {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
pub fn deref_mut(self) -> u32 {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let no_lint = NoLint(42);
|
|
|
|
let b = no_lint.deref();
|
|
|
|
let b = no_lint.deref_mut();
|
2018-10-03 16:53:39 +00:00
|
|
|
}
|