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

cast_lossless is triggered on const fn #3656

Closed
roblabla opened this issue Jan 13, 2019 · 1 comment · Fixed by #3700 or #4473
Closed

cast_lossless is triggered on const fn #3656

roblabla opened this issue Jan 13, 2019 · 1 comment · Fixed by #3700 or #4473
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@roblabla
Copy link
Contributor

pub const fn ioport(ioport: u16) -> u32 {
    0b11_1111_1111 | ((ioport as u32) << 11)
}

This triggers the cast_lossless lint:

    Checking playground v0.0.1 (/playground)
warning: casting u16 to u32 may become silently lossy if types change
 --> src/main.rs:2:23
  |
2 |     0b11_1111_1111 | ((ioport as u32) << 11)
  |                       ^^^^^^^^^^^^^^^ help: try: `u32::from(ioport)`
  |
  = note: #[warn(clippy::cast_lossless)] on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_lossless

    Finished dev [unoptimized + debuginfo] target(s) in 1.39s

However, u32::from is not a const fn, and thus the suggested replacement will fail. Link to playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=e5a19374d44dd33555594f205a7b3aa5

Clippy version: clippy 0.0.212 (39bd844 2018-12-30)

@flip1995 flip1995 added the C-bug Category: Clippy is not doing the correct thing label Jan 13, 2019
@phansch phansch self-assigned this Jan 25, 2019
bors added a commit that referenced this issue Jan 28, 2019
…t_fn, r=oli-obk

Prevent incorrect cast_lossless suggestion in const_fn

`::from` is not a const fn, so applying the suggestion of
`cast_lossless` would fail to compile. The fix is to skip the lint if
the cast is found inside a const fn.

Fixes #3656
@thejpster
Copy link

So this issue still occurs if the fn is inside an impl block. See https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=f08f961bba682893e682f7a734ab2b30

@phansch phansch reopened this Aug 30, 2019
bors added a commit that referenced this issue Aug 30, 2019
Fix cast_lossless false positive in impl const fn

Fixes #3656 (comment)

changelog: Fix false positive in `cast_lossless`
bors added a commit that referenced this issue Aug 30, 2019
Fix cast_lossless false positive in impl const fn

Fixes #3656 (comment)

changelog: Fix false positive in `cast_lossless`
@bors bors closed this as completed in fb1ae17 Aug 30, 2019
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
4 participants