Skip to content
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

manual_unwrap_or_default fails to consider deref type coercion #12666

Closed
MingweiSamuel opened this issue Apr 10, 2024 · 3 comments
Closed

manual_unwrap_or_default fails to consider deref type coercion #12666

MingweiSamuel opened this issue Apr 10, 2024 · 3 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have L-suspicious Lint: Belongs in the suspicious lint group

Comments

@MingweiSamuel
Copy link
Contributor

MingweiSamuel commented Apr 10, 2024

Summary

clippy::manual_unwrap_or_default fails to consider deref type coercion for the returned values

Lint Name

manual_unwrap_or_default

Reproducer

Playground: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=b8497c471fa7991f62cb1ef29fe18e31

fn foo(s: &Option<String>) -> &str {
    if let Some(s) = s {
        s
    } else {
        ""
    }
}

fn bar(s: Option<&String>) -> &str {
    if let Some(s) = s {
        s
    } else {
        ""
    }
}

Clippy suggests the code s.unwrap_or_default() and then rust suggests &s.unwrap_or_default(), which naturally will not work due to returning a reference to a temporary local value.

A way to simplify foo would be to use as_deref() first:

s.as_deref().unwrap_or_default()

A way to simplify bar would be to deref the inner value using map first:

s.map(std::ops::Deref::deref).unwrap_or_default()
// or
s.map(|s| &**s).unwrap_or_default()

Version

Playground:
Nightly channel
Build using the Nightly version: 1.79.0-nightly
(2024-04-08 ab5bda1aa70f707014e2)

Local:
rustc 1.79.0-nightly (8b2459c1f 2024-04-09)
binary: rustc
commit-hash: 8b2459c1f21187f9792d99310171a15e64feb9cf
commit-date: 2024-04-09
host: x86_64-apple-darwin
release: 1.79.0-nightly
LLVM version: 18.1.3

Additional Labels

No response

@MingweiSamuel MingweiSamuel added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Apr 10, 2024
@MingweiSamuel MingweiSamuel changed the title clippy::manual_unwrap_or_default fails to consider deref type coercion manual_unwrap_or_default fails to consider deref type coercion Apr 10, 2024
@J-ZhengLi J-ZhengLi added the L-suspicious Lint: Belongs in the suspicious lint group label Apr 15, 2024
@J-ZhengLi
Copy link
Member

@rustbot claim

@J-ZhengLi
Copy link
Member

J-ZhengLi commented Apr 24, 2024

@rustbot release-assignment

It doesn't trigger anymore, possibly fixed by #12610

@MingweiSamuel
Copy link
Contributor Author

Duplicate of #12564 I guess

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have L-suspicious Lint: Belongs in the suspicious lint group
Projects
None yet
Development

No branches or pull requests

2 participants