Skip to content

Wrong help message suggested for unwrap_or_else #13293

New issue

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

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

Already on GitHub? # to your account

Closed
nichmor opened this issue Aug 21, 2024 · 1 comment · Fixed by #13299
Closed

Wrong help message suggested for unwrap_or_else #13293

nichmor opened this issue Aug 21, 2024 · 1 comment · Fixed by #13299
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@nichmor
Copy link

nichmor commented Aug 21, 2024

Summary

I've encountered a small issue when clippy generates a help message.

I'm using unwrap_or_else and it hint's me to use unwrap_or providing again unwrap_or as instead example ( instead of my unwrap_or_else).

I'm happy to work on fixing this if it's not a duplicate / or taken in work by someone else.

Reproducer

I tried this code:

let channel_name = package
            .channel
            .strip_prefix(channel_config.channel_alias.as_str())
            .unwrap_or_else(|| package.channel.as_str())
            .trim_end_matches('/');

I expected to see this happen:

warning: unnecessary closure used to substitute value for `Option::None`
   --> src/cli/search.rs:412:28
    |
412 |           let channel_name = package
    |  ____________________________^
413 | |             .channel
414 | |             .strip_prefix(channel_config.channel_alias.as_str())
415 | |             .unwrap_or_else(|| package.channel.as_str())
    | |______________------------------------------------------^
    |                |
    |                help: use `unwrap_or(package.channel.as_str())` instead: `unwrap_or_else(|| package.channel.as_str())`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_lazy_evaluations
    = note: `#[warn(clippy::unnecessary_lazy_evaluations)]` on by default

Instead, this happened:

warning: unnecessary closure used to substitute value for `Option::None`
   --> src/cli/search.rs:412:28
    |
412 |           let channel_name = package
    |  ____________________________^
413 | |             .channel
414 | |             .strip_prefix(channel_config.channel_alias.as_str())
415 | |             .unwrap_or_else(|| package.channel.as_str())
    | |______________------------------------------------------^
    |                |
    |                help: use `unwrap_or(..)` instead: `unwrap_or(package.channel.as_str())`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_lazy_evaluations
    = note: `#[warn(clippy::unnecessary_lazy_evaluations)]` on by default

Version

rustc 1.80.0 (051478957 2024-07-21)
binary: rustc
commit-hash: 051478957371ee0084a7c0913941d2a8c4757bb9
commit-date: 2024-07-21
host: aarch64-apple-darwin
release: 1.80.0
LLVM version: 18.1.7

Additional Labels

No response

@nichmor nichmor added the C-bug Category: Clippy is not doing the correct thing label Aug 21, 2024
@nichmor nichmor changed the title Wrong help message suggested for Unnecessary closure used to substitute value for Option::None`` Wrong help message suggested for Unnecessary closure used Aug 21, 2024
@nichmor nichmor changed the title Wrong help message suggested for Unnecessary closure used Wrong help message suggested for unwrap_or_else Aug 21, 2024
@Alexendoo
Copy link
Member

The part after the : is the suggestion which is correct here, but yeah inline suggestions aren't the clearest. There's a plan to move away from them globally (rust-lang/rust#127282) but that may take a while, a fix to make it clearer right now would be to change the lint to use span_suggestion_verbose

@Alexendoo Alexendoo linked a pull request Aug 24, 2024 that will close this issue
bors added a commit that referenced this issue Aug 24, 2024
…xendoo

Fix suggestion unnecessary_lazy_eval

As mentioned at #13293 improve suggestion via span_suggestion_verbose

changelog[`unnecessary_lazy_eval`]: use span_suggestion_verbose for suggestions
bors added a commit that referenced this issue Aug 24, 2024
…xendoo

Fix suggestion unnecessary_lazy_eval

As mentioned at #13293 improve suggestion via span_suggestion_verbose

changelog: none
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
C-bug Category: Clippy is not doing the correct thing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants