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

False positive: missing_const_for_fn #4041

Closed
snejugal opened this issue Apr 28, 2019 · 1 comment
Closed

False positive: missing_const_for_fn #4041

snejugal opened this issue Apr 28, 2019 · 1 comment
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@snejugal
Copy link

snejugal commented Apr 28, 2019

I simplified the problem to this code:

#![deny(clippy::missing_const_for_fn)]

pub struct StringWrapper {
    string: String,
}

pub fn get_string(wrapper: StringWrapper) -> String {
    wrapper.string
}

If I run clippy, it suggests to add const to get_string signature:

snejugal ~/stuff/clippy-bug $ cargo +nightly-2019-04-26 clippy
    Checking clippy-bug v0.1.0 (/home/snejugal/stuff/clippy-bug)
error: this could be a const_fn
 --> src/lib.rs:7:1
  |
7 | / pub fn get_string(wrapper: StringWrapper) -> String {
8 | |     wrapper.string
9 | | }
  | |_^
  |
note: lint level defined here
 --> src/lib.rs:1:9
  |
1 | #![deny(clippy::missing_const_for_fn)]
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#missing_const_for_fn

error: aborting due to previous error

error: Could not compile `clippy-bug`.

To learn more, run the command again with --verbose.

But if I add const, the code refuses to compile:

snejugal ~/stuff/clippy-bug $ cargo +nightly-2019-04-26 clippy
    Checking clippy-bug v0.1.0 (/home/snejugal/stuff/clippy-bug)
error[E0493]: destructors cannot be evaluated at compile-time
 --> src/lib.rs:7:25
  |
7 | pub const fn get_string(wrapper: StringWrapper) -> String {
  |                         ^^^^^^^ constant functions cannot evaluate destructors

error: aborting due to previous error

error: Could not compile `clippy-bug`.

To learn more, run the command again with --verbose.

The code compiles though if I use u8 instead of String.

This problem persists on both stable and nightly:

snejugal ~/stuff/clippy-bug $ cargo +nightly-2019-04-26 clippy -V
clippy 0.0.212 (8c0e038 2019-04-25)
snejugal ~/stuff/clippy-bug $ cargo clippy -V
clippy 0.0.212 (726176e 2019-04-18)
@matthiaskrgr matthiaskrgr added C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied labels Apr 28, 2019
@phansch phansch self-assigned this May 19, 2019
@phansch
Copy link
Member

phansch commented May 19, 2019

Thanks for the report! This is a bug in rust and should be fixed there and probably not in Clippy, so I'm going ahead and close this issue.

Until it's fixed, you can disable the lint for that specific function with:

#[allow(clippy::missing_const_for_fn)]
pub fn get_string(wrapper: StringWrapper) -> String {
    wrapper.string
}

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-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

No branches or pull requests

3 participants