-
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.
Auto merge of #4945 - Areredify:as_deref, r=flip1995
add `option_as_ref_deref` lint changelog: add a new lint that lints `option.as_ref().map(Deref::deref)` (and similar calls), which could be expressed more succinctly as `option.as_deref[_mut]()`. Closes #4918.
- Loading branch information
Showing
9 changed files
with
294 additions
and
3 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
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
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,38 @@ | ||
// run-rustfix | ||
|
||
#![allow(unused_imports, clippy::redundant_clone)] | ||
#![warn(clippy::option_as_ref_deref)] | ||
|
||
use std::ffi::{CString, OsString}; | ||
use std::ops::{Deref, DerefMut}; | ||
use std::path::PathBuf; | ||
|
||
fn main() { | ||
let mut opt = Some(String::from("123")); | ||
|
||
let _ = opt.clone().as_deref().map(str::len); | ||
|
||
#[rustfmt::skip] | ||
let _ = opt.clone().as_deref() | ||
.map(str::len); | ||
|
||
let _ = opt.as_deref_mut(); | ||
|
||
let _ = opt.as_deref(); | ||
let _ = opt.as_deref(); | ||
let _ = opt.as_deref_mut(); | ||
let _ = opt.as_deref_mut(); | ||
let _ = Some(CString::new(vec![]).unwrap()).as_deref(); | ||
let _ = Some(OsString::new()).as_deref(); | ||
let _ = Some(PathBuf::new()).as_deref(); | ||
let _ = Some(Vec::<()>::new()).as_deref(); | ||
let _ = Some(Vec::<()>::new()).as_deref_mut(); | ||
|
||
let _ = opt.as_deref(); | ||
let _ = opt.clone().as_deref_mut().map(|x| x.len()); | ||
|
||
let vc = vec![String::new()]; | ||
let _ = Some(1_usize).as_ref().map(|x| vc[*x].as_str()); // should not be linted | ||
|
||
let _: Option<&str> = Some(&String::new()).as_ref().map(|x| x.as_str()); // should not be linted | ||
} |
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,41 @@ | ||
// run-rustfix | ||
|
||
#![allow(unused_imports, clippy::redundant_clone)] | ||
#![warn(clippy::option_as_ref_deref)] | ||
|
||
use std::ffi::{CString, OsString}; | ||
use std::ops::{Deref, DerefMut}; | ||
use std::path::PathBuf; | ||
|
||
fn main() { | ||
let mut opt = Some(String::from("123")); | ||
|
||
let _ = opt.clone().as_ref().map(Deref::deref).map(str::len); | ||
|
||
#[rustfmt::skip] | ||
let _ = opt.clone() | ||
.as_ref().map( | ||
Deref::deref | ||
) | ||
.map(str::len); | ||
|
||
let _ = opt.as_mut().map(DerefMut::deref_mut); | ||
|
||
let _ = opt.as_ref().map(String::as_str); | ||
let _ = opt.as_ref().map(|x| x.as_str()); | ||
let _ = opt.as_mut().map(String::as_mut_str); | ||
let _ = opt.as_mut().map(|x| x.as_mut_str()); | ||
let _ = Some(CString::new(vec![]).unwrap()).as_ref().map(CString::as_c_str); | ||
let _ = Some(OsString::new()).as_ref().map(OsString::as_os_str); | ||
let _ = Some(PathBuf::new()).as_ref().map(PathBuf::as_path); | ||
let _ = Some(Vec::<()>::new()).as_ref().map(Vec::as_slice); | ||
let _ = Some(Vec::<()>::new()).as_mut().map(Vec::as_mut_slice); | ||
|
||
let _ = opt.as_ref().map(|x| x.deref()); | ||
let _ = opt.clone().as_mut().map(|x| x.deref_mut()).map(|x| x.len()); | ||
|
||
let vc = vec![String::new()]; | ||
let _ = Some(1_usize).as_ref().map(|x| vc[*x].as_str()); // should not be linted | ||
|
||
let _: Option<&str> = Some(&String::new()).as_ref().map(|x| x.as_str()); // should not be linted | ||
} |
Oops, something went wrong.