-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check for Deref trait impl + add fixed version
- Loading branch information
Showing
5 changed files
with
149 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// run-rustfix | ||
|
||
#![allow(unused_variables, clippy::many_single_char_names, clippy::clone_double_ref)] | ||
#![warn(clippy::explicit_deref_method)] | ||
|
||
use std::ops::{Deref, DerefMut}; | ||
|
||
fn concat(deref_str: &str) -> String { | ||
format!("{}bar", deref_str) | ||
} | ||
|
||
fn just_return(deref_str: &str) -> &str { | ||
deref_str | ||
} | ||
|
||
fn main() { | ||
let a: &mut String = &mut String::from("foo"); | ||
|
||
// these should require linting | ||
|
||
let b: &str = &*a; | ||
|
||
let b: &mut str = &mut *a; | ||
|
||
// 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); | ||
|
||
// following should not require linting | ||
|
||
let b = just_return(a).deref(); | ||
|
||
let b: String = concat(just_return(a).deref()); | ||
|
||
let b: String = a.deref().clone(); | ||
|
||
let b: usize = a.deref_mut().len(); | ||
|
||
let b: &usize = &a.deref().len(); | ||
|
||
let b: &str = a.deref().deref(); | ||
|
||
let b: &str = &*a; | ||
|
||
let b: &mut str = &mut *a; | ||
|
||
macro_rules! expr_deref { | ||
($body:expr) => { | ||
$body.deref() | ||
}; | ||
} | ||
let b: &str = expr_deref!(a); | ||
|
||
let opt_a = Some(a); | ||
let b = opt_a.unwrap().deref(); | ||
|
||
// 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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters