Rust-analyzer is triggering 'function never used' even though they are clearly being used

Any ideas? :grinning: :grinning: :grinning: :grinning: :grinning: :grinning: :grinning: :grinning: :grinning: :grinning: :grinning: :grinning: :grinning: :grinning: :grinning: :grinning: :grinning:

Are these messages from rust analyzer itself or is it just output that originates from cargo check? (If you're unsure, run cargo check and read its output in the terminal.) Are the unused functions perhaps used in other functions that are unused themself? With these things said, if you still can't figure things out yourself, you'll probably need to provide more details, e. g. some code and error messages :wink:

1 Like

One thing to be aware of is that in this snippet, foo will be flagged as unused, even though it is called in the body of bar:

pub mod stuff {
    fn foo() {}
    fn bar() { foo() }

This is intentional: because bar is itself unused (and not pub), it does not count when it comes to tracing used-ness. The analysis is transitive.


I think that is maybe what is happening here, which would point to my imports being wrong somehow, that is to say i want functions that are part of an import to be shown as used, even if they are non-public..

I don't think imports (that is, use) affect the unused analysis at all, because from the compiler's perspective they just rename an item that's already visible.

mod x {
    // will be flagged as unused
    pub(super) fn foo() {}

mod y {
    use super::x::foo;

If the warnings get on your nerves you can temporarily add #![allow(dead_code)] (or the more expansive #![allow(unused)]) to your crate root.


A common mistake is to mod the same file from several places in the project, which means it will be compiled multiple times as distinct modules. That mistake can lead to these apparently spurious warnings.

Do you have the same mod line repeated in more than one place in the project?


that fixed it!!! You are the greatest!!!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.