refactor: check copied and cloned

This commit is contained in:
kyoto7250 2022-06-04 21:20:41 +09:00
parent 990f8bf5a6
commit 42cf98553a
7 changed files with 258 additions and 168 deletions

View file

@ -181,9 +181,11 @@ pub(super) fn check<'tcx>(
} }
fn acceptable_methods(method: &PathSegment<'_>) -> bool { fn acceptable_methods(method: &PathSegment<'_>) -> bool {
let methods: [Symbol; 6] = [ let methods: [Symbol; 8] = [
sym::clone, sym::clone,
sym::as_ref, sym::as_ref,
sym!(copied),
sym!(cloned),
sym!(as_deref), sym!(as_deref),
sym!(as_mut), sym!(as_mut),
sym!(as_deref_mut), sym!(as_deref_mut),

View file

@ -36,44 +36,52 @@ fn to_res<T>(_: T) -> Result<T, ()> {
unimplemented!() unimplemented!()
} }
struct OptionFoo { struct Issue8920<'a> {
field: Option<String>, option_field: Option<String>,
} result_field: Result<String, ()>,
ref_field: Option<&'a usize>,
struct ResultFoo {
field: Result<String, ()>,
} }
fn issue_8920() { fn issue_8920() {
let vec = vec![OptionFoo { let mut vec = vec![Issue8920 {
field: Some(String::from("str")), option_field: Some(String::from("str")),
result_field: Ok(String::from("str")),
ref_field: Some(&1),
}]; }];
let _ = vec
.iter()
.filter_map(|f| f.field.clone());
let mut vec = vec![ResultFoo {
field: Ok(String::from("str")),
}];
let _ = vec.iter().filter_map(|f| f.field.clone().ok());
let _ = vec let _ = vec
.iter() .iter()
.filter_map(|f| f.field.as_ref().ok()); .filter_map(|f| f.option_field.clone());
let _ = vec let _ = vec
.iter() .iter()
.filter_map(|f| f.field.as_deref().ok()); .filter_map(|f| f.ref_field.cloned());
let _ = vec
.iter()
.filter_map(|f| f.ref_field.copied());
let _ = vec
.iter()
.filter_map(|f| f.result_field.clone().ok());
let _ = vec
.iter()
.filter_map(|f| f.result_field.as_ref().ok());
let _ = vec
.iter()
.filter_map(|f| f.result_field.as_deref().ok());
let _ = vec let _ = vec
.iter_mut() .iter_mut()
.filter_map(|f| f.field.as_mut().ok()); .filter_map(|f| f.result_field.as_mut().ok());
let _ = vec let _ = vec
.iter_mut() .iter_mut()
.filter_map(|f| f.field.as_deref_mut().ok()); .filter_map(|f| f.result_field.as_deref_mut().ok());
let _ = vec let _ = vec
.iter() .iter()
.filter_map(|f| f.field.to_owned().ok()); .filter_map(|f| f.result_field.to_owned().ok());
} }

View file

@ -36,50 +36,61 @@ fn to_res<T>(_: T) -> Result<T, ()> {
unimplemented!() unimplemented!()
} }
struct OptionFoo { struct Issue8920<'a> {
field: Option<String>, option_field: Option<String>,
} result_field: Result<String, ()>,
ref_field: Option<&'a usize>,
struct ResultFoo {
field: Result<String, ()>,
} }
fn issue_8920() { fn issue_8920() {
let vec = vec![OptionFoo { let mut vec = vec![Issue8920 {
field: Some(String::from("str")), option_field: Some(String::from("str")),
result_field: Ok(String::from("str")),
ref_field: Some(&1),
}]; }];
let _ = vec
.iter()
.filter(|f| f.field.is_some())
.map(|f| f.field.clone().unwrap());
let mut vec = vec![ResultFoo {
field: Ok(String::from("str")),
}];
let _ = vec.iter().filter(|f| f.field.is_ok()).map(|f| f.field.clone().unwrap());
let _ = vec let _ = vec
.iter() .iter()
.filter(|f| f.field.is_ok()) .filter(|f| f.option_field.is_some())
.map(|f| f.field.as_ref().unwrap()); .map(|f| f.option_field.clone().unwrap());
let _ = vec let _ = vec
.iter() .iter()
.filter(|f| f.field.is_ok()) .filter(|f| f.ref_field.is_some())
.map(|f| f.field.as_deref().unwrap()); .map(|f| f.ref_field.cloned().unwrap());
let _ = vec
.iter()
.filter(|f| f.ref_field.is_some())
.map(|f| f.ref_field.copied().unwrap());
let _ = vec
.iter()
.filter(|f| f.result_field.is_ok())
.map(|f| f.result_field.clone().unwrap());
let _ = vec
.iter()
.filter(|f| f.result_field.is_ok())
.map(|f| f.result_field.as_ref().unwrap());
let _ = vec
.iter()
.filter(|f| f.result_field.is_ok())
.map(|f| f.result_field.as_deref().unwrap());
let _ = vec let _ = vec
.iter_mut() .iter_mut()
.filter(|f| f.field.is_ok()) .filter(|f| f.result_field.is_ok())
.map(|f| f.field.as_mut().unwrap()); .map(|f| f.result_field.as_mut().unwrap());
let _ = vec let _ = vec
.iter_mut() .iter_mut()
.filter(|f| f.field.is_ok()) .filter(|f| f.result_field.is_ok())
.map(|f| f.field.as_deref_mut().unwrap()); .map(|f| f.result_field.as_deref_mut().unwrap());
let _ = vec let _ = vec
.iter() .iter()
.filter(|f| f.field.is_ok()) .filter(|f| f.result_field.is_ok())
.map(|f| f.field.to_owned().unwrap()); .map(|f| f.result_field.to_owned().unwrap());
} }

View file

@ -19,58 +19,76 @@ LL | let _ = (0..).filter(|&n| to_res(n).is_ok()).map(|a| to_res(a).unwrap_o
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `filter_map(|a| to_res(a).ok())` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `filter_map(|a| to_res(a).ok())`
error: `filter(..).map(..)` can be simplified as `filter_map(..)` error: `filter(..).map(..)` can be simplified as `filter_map(..)`
--> $DIR/manual_filter_map.rs:53:10 --> $DIR/manual_filter_map.rs:54:10
| |
LL | .filter(|f| f.field.is_some()) LL | .filter(|f| f.option_field.is_some())
| __________^ | __________^
LL | | .map(|f| f.field.clone().unwrap()); LL | | .map(|f| f.option_field.clone().unwrap());
| |__________________________________________^ help: try: `filter_map(|f| f.field.clone())` | |_________________________________________________^ help: try: `filter_map(|f| f.option_field.clone())`
error: `filter(..).map(..)` can be simplified as `filter_map(..)` error: `filter(..).map(..)` can be simplified as `filter_map(..)`
--> $DIR/manual_filter_map.rs:59:24 --> $DIR/manual_filter_map.rs:59:10
| |
LL | let _ = vec.iter().filter(|f| f.field.is_ok()).map(|f| f.field.clone().unwrap()); LL | .filter(|f| f.ref_field.is_some())
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `filter_map(|f| f.field.clone().ok())` | __________^
LL | | .map(|f| f.ref_field.cloned().unwrap());
| |_______________________________________________^ help: try: `filter_map(|f| f.ref_field.cloned())`
error: `filter(..).map(..)` can be simplified as `filter_map(..)` error: `filter(..).map(..)` can be simplified as `filter_map(..)`
--> $DIR/manual_filter_map.rs:63:10 --> $DIR/manual_filter_map.rs:64:10
| |
LL | .filter(|f| f.field.is_ok()) LL | .filter(|f| f.ref_field.is_some())
| __________^ | __________^
LL | | .map(|f| f.field.as_ref().unwrap()); LL | | .map(|f| f.ref_field.copied().unwrap());
| |___________________________________________^ help: try: `filter_map(|f| f.field.as_ref().ok())` | |_______________________________________________^ help: try: `filter_map(|f| f.ref_field.copied())`
error: `filter(..).map(..)` can be simplified as `filter_map(..)` error: `filter(..).map(..)` can be simplified as `filter_map(..)`
--> $DIR/manual_filter_map.rs:68:10 --> $DIR/manual_filter_map.rs:69:10
| |
LL | .filter(|f| f.field.is_ok()) LL | .filter(|f| f.result_field.is_ok())
| __________^ | __________^
LL | | .map(|f| f.field.as_deref().unwrap()); LL | | .map(|f| f.result_field.clone().unwrap());
| |_____________________________________________^ help: try: `filter_map(|f| f.field.as_deref().ok())` | |_________________________________________________^ help: try: `filter_map(|f| f.result_field.clone().ok())`
error: `filter(..).map(..)` can be simplified as `filter_map(..)` error: `filter(..).map(..)` can be simplified as `filter_map(..)`
--> $DIR/manual_filter_map.rs:73:10 --> $DIR/manual_filter_map.rs:74:10
| |
LL | .filter(|f| f.field.is_ok()) LL | .filter(|f| f.result_field.is_ok())
| __________^ | __________^
LL | | .map(|f| f.field.as_mut().unwrap()); LL | | .map(|f| f.result_field.as_ref().unwrap());
| |___________________________________________^ help: try: `filter_map(|f| f.field.as_mut().ok())` | |__________________________________________________^ help: try: `filter_map(|f| f.result_field.as_ref().ok())`
error: `filter(..).map(..)` can be simplified as `filter_map(..)` error: `filter(..).map(..)` can be simplified as `filter_map(..)`
--> $DIR/manual_filter_map.rs:78:10 --> $DIR/manual_filter_map.rs:79:10
| |
LL | .filter(|f| f.field.is_ok()) LL | .filter(|f| f.result_field.is_ok())
| __________^ | __________^
LL | | .map(|f| f.field.as_deref_mut().unwrap()); LL | | .map(|f| f.result_field.as_deref().unwrap());
| |_________________________________________________^ help: try: `filter_map(|f| f.field.as_deref_mut().ok())` | |____________________________________________________^ help: try: `filter_map(|f| f.result_field.as_deref().ok())`
error: `filter(..).map(..)` can be simplified as `filter_map(..)` error: `filter(..).map(..)` can be simplified as `filter_map(..)`
--> $DIR/manual_filter_map.rs:83:10 --> $DIR/manual_filter_map.rs:84:10
| |
LL | .filter(|f| f.field.is_ok()) LL | .filter(|f| f.result_field.is_ok())
| __________^ | __________^
LL | | .map(|f| f.field.to_owned().unwrap()); LL | | .map(|f| f.result_field.as_mut().unwrap());
| |_____________________________________________^ help: try: `filter_map(|f| f.field.to_owned().ok())` | |__________________________________________________^ help: try: `filter_map(|f| f.result_field.as_mut().ok())`
error: aborting due to 10 previous errors error: `filter(..).map(..)` can be simplified as `filter_map(..)`
--> $DIR/manual_filter_map.rs:89:10
|
LL | .filter(|f| f.result_field.is_ok())
| __________^
LL | | .map(|f| f.result_field.as_deref_mut().unwrap());
| |________________________________________________________^ help: try: `filter_map(|f| f.result_field.as_deref_mut().ok())`
error: `filter(..).map(..)` can be simplified as `filter_map(..)`
--> $DIR/manual_filter_map.rs:94:10
|
LL | .filter(|f| f.result_field.is_ok())
| __________^
LL | | .map(|f| f.result_field.to_owned().unwrap());
| |____________________________________________________^ help: try: `filter_map(|f| f.result_field.to_owned().ok())`
error: aborting due to 12 previous errors

View file

@ -36,40 +36,52 @@ fn to_res<T>(_: T) -> Result<T, ()> {
unimplemented!() unimplemented!()
} }
struct OptionFoo { struct Issue8920<'a> {
field: Option<String>, option_field: Option<String>,
} result_field: Result<String, ()>,
ref_field: Option<&'a usize>,
struct ResultFoo {
field: Result<String, ()>,
} }
fn issue_8920() { fn issue_8920() {
let vec = vec![OptionFoo { let mut vec = vec![Issue8920 {
field: Some(String::from("str")), option_field: Some(String::from("str")),
result_field: Ok(String::from("str")),
ref_field: Some(&1),
}]; }];
let _ = vec.iter().find_map(|f| f.field.clone());
let mut vec = vec![ResultFoo {
field: Ok(String::from("str")),
}];
let _ = vec.iter().find_map(|f| f.field.clone().ok());
let _ = vec.iter().find_map(|f| f.field.as_ref().ok());
let _ = vec let _ = vec
.iter() .iter()
.find_map(|f| f.field.as_deref().ok()); .find_map(|f| f.option_field.clone());
let _ = vec
.iter_mut()
.find_map(|f| f.field.as_mut().ok());
let _ = vec
.iter_mut()
.find_map(|f| f.field.as_deref_mut().ok());
let _ = vec let _ = vec
.iter() .iter()
.find_map(|f| f.field.to_owned().ok()); .find_map(|f| f.ref_field.cloned());
let _ = vec
.iter()
.find_map(|f| f.ref_field.copied());
let _ = vec
.iter()
.find_map(|f| f.result_field.clone().ok());
let _ = vec
.iter()
.find_map(|f| f.result_field.as_ref().ok());
let _ = vec
.iter()
.find_map(|f| f.result_field.as_deref().ok());
let _ = vec
.iter_mut()
.find_map(|f| f.result_field.as_mut().ok());
let _ = vec
.iter_mut()
.find_map(|f| f.result_field.as_deref_mut().ok());
let _ = vec
.iter()
.find_map(|f| f.result_field.to_owned().ok());
} }

View file

@ -36,44 +36,61 @@ fn to_res<T>(_: T) -> Result<T, ()> {
unimplemented!() unimplemented!()
} }
struct OptionFoo { struct Issue8920<'a> {
field: Option<String>, option_field: Option<String>,
} result_field: Result<String, ()>,
ref_field: Option<&'a usize>,
struct ResultFoo {
field: Result<String, ()>,
} }
fn issue_8920() { fn issue_8920() {
let vec = vec![OptionFoo { let mut vec = vec![Issue8920 {
field: Some(String::from("str")), option_field: Some(String::from("str")),
result_field: Ok(String::from("str")),
ref_field: Some(&1),
}]; }];
let _ = vec.iter().find(|f| f.field.is_some()).map(|f| f.field.clone().unwrap());
let mut vec = vec![ResultFoo {
field: Ok(String::from("str")),
}];
let _ = vec.iter().find(|f| f.field.is_ok()).map(|f| f.field.clone().unwrap());
let _ = vec.iter().find(|f| f.field.is_ok()).map(|f| f.field.as_ref().unwrap());
let _ = vec let _ = vec
.iter() .iter()
.find(|f| f.field.is_ok()) .find(|f| f.option_field.is_some())
.map(|f| f.field.as_deref().unwrap()); .map(|f| f.option_field.clone().unwrap());
let _ = vec
.iter_mut()
.find(|f| f.field.is_ok())
.map(|f| f.field.as_mut().unwrap());
let _ = vec
.iter_mut()
.find(|f| f.field.is_ok())
.map(|f| f.field.as_deref_mut().unwrap());
let _ = vec let _ = vec
.iter() .iter()
.find(|f| f.field.is_ok()) .find(|f| f.ref_field.is_some())
.map(|f| f.field.to_owned().unwrap()); .map(|f| f.ref_field.cloned().unwrap());
let _ = vec
.iter()
.find(|f| f.ref_field.is_some())
.map(|f| f.ref_field.copied().unwrap());
let _ = vec
.iter()
.find(|f| f.result_field.is_ok())
.map(|f| f.result_field.clone().unwrap());
let _ = vec
.iter()
.find(|f| f.result_field.is_ok())
.map(|f| f.result_field.as_ref().unwrap());
let _ = vec
.iter()
.find(|f| f.result_field.is_ok())
.map(|f| f.result_field.as_deref().unwrap());
let _ = vec
.iter_mut()
.find(|f| f.result_field.is_ok())
.map(|f| f.result_field.as_mut().unwrap());
let _ = vec
.iter_mut()
.find(|f| f.result_field.is_ok())
.map(|f| f.result_field.as_deref_mut().unwrap());
let _ = vec
.iter()
.find(|f| f.result_field.is_ok())
.map(|f| f.result_field.to_owned().unwrap());
} }

View file

@ -19,54 +19,76 @@ LL | let _ = (0..).find(|&n| to_res(n).is_ok()).map(|a| to_res(a).unwrap_or(
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `find_map(|a| to_res(a).ok())` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `find_map(|a| to_res(a).ok())`
error: `find(..).map(..)` can be simplified as `find_map(..)` error: `find(..).map(..)` can be simplified as `find_map(..)`
--> $DIR/manual_find_map.rs:51:24 --> $DIR/manual_find_map.rs:54:10
| |
LL | let _ = vec.iter().find(|f| f.field.is_some()).map(|f| f.field.clone().unwrap()); LL | .find(|f| f.option_field.is_some())
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `find_map(|f| f.field.clone())`
error: `find(..).map(..)` can be simplified as `find_map(..)`
--> $DIR/manual_find_map.rs:56:24
|
LL | let _ = vec.iter().find(|f| f.field.is_ok()).map(|f| f.field.clone().unwrap());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `find_map(|f| f.field.clone().ok())`
error: `find(..).map(..)` can be simplified as `find_map(..)`
--> $DIR/manual_find_map.rs:58:24
|
LL | let _ = vec.iter().find(|f| f.field.is_ok()).map(|f| f.field.as_ref().unwrap());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `find_map(|f| f.field.as_ref().ok())`
error: `find(..).map(..)` can be simplified as `find_map(..)`
--> $DIR/manual_find_map.rs:62:10
|
LL | .find(|f| f.field.is_ok())
| __________^ | __________^
LL | | .map(|f| f.field.as_deref().unwrap()); LL | | .map(|f| f.option_field.clone().unwrap());
| |_____________________________________________^ help: try: `find_map(|f| f.field.as_deref().ok())` | |_________________________________________________^ help: try: `find_map(|f| f.option_field.clone())`
error: `find(..).map(..)` can be simplified as `find_map(..)` error: `find(..).map(..)` can be simplified as `find_map(..)`
--> $DIR/manual_find_map.rs:67:10 --> $DIR/manual_find_map.rs:59:10
| |
LL | .find(|f| f.field.is_ok()) LL | .find(|f| f.ref_field.is_some())
| __________^ | __________^
LL | | .map(|f| f.field.as_mut().unwrap()); LL | | .map(|f| f.ref_field.cloned().unwrap());
| |___________________________________________^ help: try: `find_map(|f| f.field.as_mut().ok())` | |_______________________________________________^ help: try: `find_map(|f| f.ref_field.cloned())`
error: `find(..).map(..)` can be simplified as `find_map(..)` error: `find(..).map(..)` can be simplified as `find_map(..)`
--> $DIR/manual_find_map.rs:72:10 --> $DIR/manual_find_map.rs:64:10
| |
LL | .find(|f| f.field.is_ok()) LL | .find(|f| f.ref_field.is_some())
| __________^ | __________^
LL | | .map(|f| f.field.as_deref_mut().unwrap()); LL | | .map(|f| f.ref_field.copied().unwrap());
| |_________________________________________________^ help: try: `find_map(|f| f.field.as_deref_mut().ok())` | |_______________________________________________^ help: try: `find_map(|f| f.ref_field.copied())`
error: `find(..).map(..)` can be simplified as `find_map(..)` error: `find(..).map(..)` can be simplified as `find_map(..)`
--> $DIR/manual_find_map.rs:77:10 --> $DIR/manual_find_map.rs:69:10
| |
LL | .find(|f| f.field.is_ok()) LL | .find(|f| f.result_field.is_ok())
| __________^ | __________^
LL | | .map(|f| f.field.to_owned().unwrap()); LL | | .map(|f| f.result_field.clone().unwrap());
| |_____________________________________________^ help: try: `find_map(|f| f.field.to_owned().ok())` | |_________________________________________________^ help: try: `find_map(|f| f.result_field.clone().ok())`
error: aborting due to 10 previous errors error: `find(..).map(..)` can be simplified as `find_map(..)`
--> $DIR/manual_find_map.rs:74:10
|
LL | .find(|f| f.result_field.is_ok())
| __________^
LL | | .map(|f| f.result_field.as_ref().unwrap());
| |__________________________________________________^ help: try: `find_map(|f| f.result_field.as_ref().ok())`
error: `find(..).map(..)` can be simplified as `find_map(..)`
--> $DIR/manual_find_map.rs:79:10
|
LL | .find(|f| f.result_field.is_ok())
| __________^
LL | | .map(|f| f.result_field.as_deref().unwrap());
| |____________________________________________________^ help: try: `find_map(|f| f.result_field.as_deref().ok())`
error: `find(..).map(..)` can be simplified as `find_map(..)`
--> $DIR/manual_find_map.rs:84:10
|
LL | .find(|f| f.result_field.is_ok())
| __________^
LL | | .map(|f| f.result_field.as_mut().unwrap());
| |__________________________________________________^ help: try: `find_map(|f| f.result_field.as_mut().ok())`
error: `find(..).map(..)` can be simplified as `find_map(..)`
--> $DIR/manual_find_map.rs:89:10
|
LL | .find(|f| f.result_field.is_ok())
| __________^
LL | | .map(|f| f.result_field.as_deref_mut().unwrap());
| |________________________________________________________^ help: try: `find_map(|f| f.result_field.as_deref_mut().ok())`
error: `find(..).map(..)` can be simplified as `find_map(..)`
--> $DIR/manual_find_map.rs:94:10
|
LL | .find(|f| f.result_field.is_ok())
| __________^
LL | | .map(|f| f.result_field.to_owned().unwrap());
| |____________________________________________________^ help: try: `find_map(|f| f.result_field.to_owned().ok())`
error: aborting due to 12 previous errors