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 never used function warning #101699

Closed
leonardo-m opened this issue Sep 11, 2022 · 3 comments
Closed

False never used function warning #101699

leonardo-m opened this issue Sep 11, 2022 · 3 comments
Labels
C-bug Category: This is a bug.

Comments

@leonardo-m
Copy link

I'm not sure if this is expected. In this code I've used a const test function, and I call it inside a const assert:

macro_rules! const_assert {
    ($x:expr $(,)?) => {
        const _: () = assert!($x);
    };
}

fn main() {
    const fn test(n: u32) -> bool {
        n > 0
    }
    const N: u32 = 100;
    const_assert!(test(N));
    dbg!(N);
}

This code works correctly, but rusts gives a "warning: function test is never used".

@leonardo-m leonardo-m added the C-bug Category: This is a bug. label Sep 11, 2022
@est31
Copy link
Member

est31 commented Sep 11, 2022

Smaller reproduction:

    const fn test() {}
    const _: () = test();

It works if you replace the _ with FOO and print the FOO somewhere else. I guess what's missing is the special treatment of _.

@ehuss
Copy link
Contributor

ehuss commented Sep 11, 2022

I think this is nearly a duplicate of #96952. See also #101532.

I think it's very possible that when using const _, the compiler assumes the value is never "used", and thus evaluating the const expression has no effect. However, that is no longer true since #89508 where panic support was added. (Just an idle speculation.)

@est31
Copy link
Member

est31 commented Sep 11, 2022

Oh yeah it's a duplicate. That's why it was so familiar to me. I guess this issue should be closed as dupe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

3 participants