Skip to content

Lint manual_unwrap_or_default for Result as well #12897

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion clippy_lints/src/manual_unwrap_or_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ fn get_some<'tcx>(cx: &LateContext<'tcx>, pat: &Pat<'tcx>) -> Option<HirId> {
// Since it comes from a pattern binding, we need to get the parent to actually match
// against it.
&& let Some(def_id) = cx.tcx.opt_parent(def_id)
&& cx.tcx.lang_items().get(LangItem::OptionSome) == Some(def_id)
&& (cx.tcx.lang_items().get(LangItem::OptionSome) == Some(def_id)
|| cx.tcx.lang_items().get(LangItem::ResultOk) == Some(def_id))
{
let mut bindings = Vec::new();
pat.each_binding(|_, id, _, _| bindings.push(id));
Expand All @@ -80,6 +81,14 @@ fn get_none<'tcx>(cx: &LateContext<'tcx>, arm: &Arm<'tcx>) -> Option<&'tcx Expr<
&& cx.tcx.lang_items().get(LangItem::OptionNone) == Some(def_id)
{
Some(arm.body)
} else if let PatKind::TupleStruct(QPath::Resolved(_, path), _, _)= arm.pat.kind
&& let Some(def_id) = path.res.opt_def_id()
// Since it comes from a pattern binding, we need to get the parent to actually match
// against it.
&& let Some(def_id) = cx.tcx.opt_parent(def_id)
&& cx.tcx.lang_items().get(LangItem::ResultErr) == Some(def_id)
{
Some(arm.body)
} else if let PatKind::Wild = arm.pat.kind {
// We consider that the `Some` check will filter it out if it's not right.
Some(arm.body)
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/manual_unwrap_or_default.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ fn main() {
Some(x) => x,
None => &[],
};

let x: Result<String, i64> = Ok(String::new());
x.unwrap_or_default();

let x: Result<String, i64> = Ok(String::new());
x.unwrap_or_default();
}

// Issue #12531
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/manual_unwrap_or_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ fn main() {
Some(x) => x,
None => &[],
};

let x: Result<String, i64> = Ok(String::new());
match x {
//~^ ERROR: match can be simplified with `.unwrap_or_default()`
Ok(v) => v,
Err(_) => String::new(),
};

let x: Result<String, i64> = Ok(String::new());
if let Ok(v) = x {
//~^ ERROR: if let can be simplified with `.unwrap_or_default()`
v
} else {
String::new()
};
}

// Issue #12531
Expand Down
25 changes: 23 additions & 2 deletions tests/ui/manual_unwrap_or_default.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,28 @@ LL | | };
| |_____^ help: replace it with: `x.unwrap_or_default()`

error: match can be simplified with `.unwrap_or_default()`
--> tests/ui/manual_unwrap_or_default.rs:56:20
--> tests/ui/manual_unwrap_or_default.rs:52:5
|
LL | / match x {
LL | |
LL | | Ok(v) => v,
LL | | Err(_) => String::new(),
LL | | };
| |_____^ help: replace it with: `x.unwrap_or_default()`

error: if let can be simplified with `.unwrap_or_default()`
--> tests/ui/manual_unwrap_or_default.rs:59:5
|
LL | / if let Ok(v) = x {
LL | |
LL | | v
LL | | } else {
LL | | String::new()
LL | | };
| |_____^ help: replace it with: `x.unwrap_or_default()`

error: match can be simplified with `.unwrap_or_default()`
--> tests/ui/manual_unwrap_or_default.rs:71:20
|
LL | Some(_) => match *b {
| ____________________^
Expand All @@ -62,5 +83,5 @@ LL | | _ => 0,
LL | | },
| |_________^ help: replace it with: `(*b).unwrap_or_default()`

error: aborting due to 6 previous errors
error: aborting due to 8 previous errors

6 changes: 5 additions & 1 deletion tests/ui/useless_conversion_try.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#![deny(clippy::useless_conversion)]
#![allow(clippy::needless_if, clippy::unnecessary_fallible_conversions)]
#![allow(
clippy::needless_if,
clippy::unnecessary_fallible_conversions,
clippy::manual_unwrap_or_default
)]

fn test_generic<T: Copy>(val: T) -> T {
let _ = T::try_from(val).unwrap();
Expand Down
18 changes: 9 additions & 9 deletions tests/ui/useless_conversion_try.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: useless conversion to the same type: `T`
--> tests/ui/useless_conversion_try.rs:5:13
--> tests/ui/useless_conversion_try.rs:9:13
|
LL | let _ = T::try_from(val).unwrap();
| ^^^^^^^^^^^^^^^^
Expand All @@ -12,63 +12,63 @@ LL | #![deny(clippy::useless_conversion)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^

error: useless conversion to the same type: `T`
--> tests/ui/useless_conversion_try.rs:7:5
--> tests/ui/useless_conversion_try.rs:11:5
|
LL | val.try_into().unwrap()
| ^^^^^^^^^^^^^^
|
= help: consider removing `.try_into()`

error: useless conversion to the same type: `std::string::String`
--> tests/ui/useless_conversion_try.rs:30:21
--> tests/ui/useless_conversion_try.rs:34:21
|
LL | let _: String = "foo".to_string().try_into().unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider removing `.try_into()`

error: useless conversion to the same type: `std::string::String`
--> tests/ui/useless_conversion_try.rs:32:21
--> tests/ui/useless_conversion_try.rs:36:21
|
LL | let _: String = TryFrom::try_from("foo".to_string()).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider removing `TryFrom::try_from()`

error: useless conversion to the same type: `std::string::String`
--> tests/ui/useless_conversion_try.rs:34:13
--> tests/ui/useless_conversion_try.rs:38:13
|
LL | let _ = String::try_from("foo".to_string()).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider removing `String::try_from()`

error: useless conversion to the same type: `std::string::String`
--> tests/ui/useless_conversion_try.rs:36:13
--> tests/ui/useless_conversion_try.rs:40:13
|
LL | let _ = String::try_from(format!("A: {:04}", 123)).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider removing `String::try_from()`

error: useless conversion to the same type: `std::string::String`
--> tests/ui/useless_conversion_try.rs:38:21
--> tests/ui/useless_conversion_try.rs:42:21
|
LL | let _: String = format!("Hello {}", "world").try_into().unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider removing `.try_into()`

error: useless conversion to the same type: `std::string::String`
--> tests/ui/useless_conversion_try.rs:40:21
--> tests/ui/useless_conversion_try.rs:44:21
|
LL | let _: String = String::new().try_into().unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider removing `.try_into()`

error: useless conversion to the same type: `std::string::String`
--> tests/ui/useless_conversion_try.rs:42:27
--> tests/ui/useless_conversion_try.rs:46:27
|
LL | let _: String = match String::from("_").try_into() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down