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

Clippy suggests an unintuitive fix #9964

Open
asonix opened this issue Nov 27, 2022 · 0 comments
Open

Clippy suggests an unintuitive fix #9964

asonix opened this issue Nov 27, 2022 · 0 comments
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@asonix
Copy link

asonix commented Nov 27, 2022

Summary

Clippy's suggested fix for returning 1 or 0 from an if statement is unintuitive

Reproducer

writing code like

if opt.is_some() {
    1
} else {
    0
}

leads clippy to suggest usize::from(opt.is_some()), i.e. coercing a boolean into an integer. It is not obvious to me that a boolean coerced into an integer will yield 0 for false and 1 for true. I'm not even sure why rust implements this From.

I've settled on opt.map_or(0, |_| 1) in order to avoid the lint while also still being clear about the output values.

Version

rustc 1.65.0 (897e37553 2022-11-02)
binary: rustc
commit-hash: 897e37553bba8b42751c67658967889d11ecd120
commit-date: 2022-11-02
host: x86_64-unknown-linux-gnu
release: 1.65.0
LLVM version: 15.0.0

Additional Labels

No response

@asonix asonix added the C-bug Category: Clippy is not doing the correct thing label Nov 27, 2022
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
Projects
None yet
Development

No branches or pull requests

1 participant