rust-clippy/tests/ui/dereference.rs

39 lines
870 B
Rust
Raw Normal View History

2020-01-23 15:28:01 +00:00
#![allow(unused_variables, clippy::many_single_char_names, clippy::clone_double_ref)]
#![warn(clippy::explicit_deref_method)]
2018-10-03 16:53:39 +00:00
use std::ops::{Deref, DerefMut};
fn main() {
let a: &mut String = &mut String::from("foo");
// these should require linting
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-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();
// only first deref should get linted here
let b: &str = a.deref().deref();
// both derefs should get linted here
let b: String = format!("{}, {}", a.deref(), a.deref());
2018-10-03 16:53:39 +00:00
// these should not require linting
2020-01-23 15:28:01 +00:00
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);
2018-10-03 16:53:39 +00:00
}