2023-04-23 06:03:09 -05:00
|
|
|
//@run-rustfix
|
2022-10-06 02:44:38 -05:00
|
|
|
#![warn(clippy::explicit_deref_methods)]
|
2023-05-31 23:33:30 -05:00
|
|
|
#![allow(unused_variables, unused_must_use)]
|
2022-06-04 06:34:07 -05:00
|
|
|
#![allow(
|
2022-10-06 02:44:38 -05:00
|
|
|
clippy::borrow_deref_ref,
|
2023-04-28 12:17:46 -05:00
|
|
|
suspicious_double_ref_op,
|
2022-10-06 02:44:38 -05:00
|
|
|
clippy::explicit_auto_deref,
|
2022-06-04 06:34:07 -05:00
|
|
|
clippy::needless_borrow,
|
2023-05-31 23:33:30 -05:00
|
|
|
clippy::no_effect,
|
2022-10-06 02:44:38 -05:00
|
|
|
clippy::uninlined_format_args
|
2022-06-04 06:34:07 -05:00
|
|
|
)]
|
2020-02-25 16:06:24 -06:00
|
|
|
|
|
|
|
use std::ops::{Deref, DerefMut};
|
|
|
|
|
|
|
|
fn concat(deref_str: &str) -> String {
|
|
|
|
format!("{}bar", deref_str)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn just_return(deref_str: &str) -> &str {
|
|
|
|
deref_str
|
|
|
|
}
|
|
|
|
|
2020-03-21 13:34:56 -05:00
|
|
|
struct CustomVec(Vec<u8>);
|
|
|
|
impl Deref for CustomVec {
|
|
|
|
type Target = Vec<u8>;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Vec<u8> {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-31 23:33:30 -05:00
|
|
|
struct Aaa;
|
|
|
|
|
|
|
|
impl Deref for Aaa {
|
|
|
|
type Target = ();
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
todo!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DerefMut for Aaa {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
todo!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-25 16:06:24 -06:00
|
|
|
fn main() {
|
|
|
|
let a: &mut String = &mut String::from("foo");
|
|
|
|
|
|
|
|
// these should require linting
|
|
|
|
|
|
|
|
let b: &str = &*a;
|
|
|
|
|
2021-03-25 13:29:11 -05:00
|
|
|
let b: &mut str = &mut **a;
|
2020-02-25 16:06:24 -06:00
|
|
|
|
|
|
|
// both derefs should get linted here
|
|
|
|
let b: String = format!("{}, {}", &*a, &*a);
|
|
|
|
|
|
|
|
println!("{}", &*a);
|
|
|
|
|
|
|
|
#[allow(clippy::match_single_binding)]
|
|
|
|
match &*a {
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
let b: String = concat(&*a);
|
|
|
|
|
2021-03-25 13:29:11 -05:00
|
|
|
let b = just_return(a);
|
2020-03-07 08:33:27 -06:00
|
|
|
|
2021-03-25 13:29:11 -05:00
|
|
|
let b: String = concat(just_return(a));
|
2020-02-25 16:06:24 -06:00
|
|
|
|
2021-03-25 13:29:11 -05:00
|
|
|
let b: &str = &**a;
|
2020-02-25 16:06:24 -06:00
|
|
|
|
2020-03-07 08:33:27 -06:00
|
|
|
let opt_a = Some(a.clone());
|
|
|
|
let b = &*opt_a.unwrap();
|
|
|
|
|
2023-06-01 14:29:56 -05:00
|
|
|
// make sure `Aaa::deref` instead of `aaa.deref()` is not linted, as well as fully qualified
|
|
|
|
// syntax
|
2023-05-31 23:33:30 -05:00
|
|
|
|
2023-06-01 14:29:56 -05:00
|
|
|
Aaa::deref(&Aaa);
|
|
|
|
Aaa::deref_mut(&mut Aaa);
|
|
|
|
<Aaa as Deref>::deref(&Aaa);
|
|
|
|
<Aaa as DerefMut>::deref_mut(&mut Aaa);
|
2023-05-31 23:33:30 -05:00
|
|
|
let mut aaa = Aaa;
|
2023-06-01 14:29:56 -05:00
|
|
|
Aaa::deref(&aaa);
|
|
|
|
Aaa::deref_mut(&mut aaa);
|
2023-05-31 23:33:30 -05:00
|
|
|
|
2020-03-07 08:33:27 -06:00
|
|
|
// following should not require linting
|
|
|
|
|
2020-03-21 13:34:56 -05:00
|
|
|
let cv = CustomVec(vec![0, 42]);
|
|
|
|
let c = cv.deref()[0];
|
|
|
|
|
2020-03-07 08:33:27 -06:00
|
|
|
let b: &str = &*a.deref();
|
2020-02-25 16:06:24 -06:00
|
|
|
|
|
|
|
let b: String = a.deref().clone();
|
|
|
|
|
|
|
|
let b: usize = a.deref_mut().len();
|
|
|
|
|
|
|
|
let b: &usize = &a.deref().len();
|
|
|
|
|
|
|
|
let b: &str = &*a;
|
|
|
|
|
|
|
|
let b: &mut str = &mut *a;
|
|
|
|
|
|
|
|
macro_rules! expr_deref {
|
|
|
|
($body:expr) => {
|
|
|
|
$body.deref()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
let b: &str = expr_deref!(a);
|
|
|
|
|
2021-03-25 13:29:11 -05:00
|
|
|
let b: &str = expr_deref!(&*a);
|
|
|
|
|
2020-02-25 16:06:24 -06: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();
|
|
|
|
}
|