How to suppress "function is never used" warning for a function used by tests?
Asked Answered
K

8

83

I'm writing a program in Rust and I have some tests for it. I wrote a helper function for these tests, but whenever I build using cargo build it warns me that the function is never used:

warning: function is never used: ... #[warn(dead_code)] on by default

How I can mark this function as used so as not to get the warnings?

Kristopher answered 2/10, 2015 at 5:0 Comment(1)
Adding a pub before mod utils fixed the problem for me as pointed out in this commentEndolymph
L
100

Specific question

How I can mark this function as used so as not to get the warnings?

The Rust compiler runs many lints to warn you about possible issues in your code and the dead_code lint is one of them. It can be very useful in pointing out mistakes when code is complete, but may also be a nuisance at earlier stages. Often, this can be solved by either deleting unused code, or by marking a public method. However, all lints can be turned off by allowing them, and your error message (#[warn(dead_code)] on by default) contains the name of the lint you could disable.

#[allow(dead_code)]
fn my_unused_function() {}

Alternative for testing

I wrote a helper function for these tests, but whenever I build using cargo build it warns me that the function is never used.

This happens to be a special case, which is that code that is only used for testing isn't needed in the real executable and should probably not be included.

In order to optionally disable compilation of test code, you can mark it accordingly using the cfg attribute with the test profile.

#[cfg(test)]
fn my_test_specific_function() {}

When marked in this way, the compiler knows to ignore the method during compilation. This is similar to commonly used ifdef usage in other languages like C or C++, where you are telling a preprocessor to ignore the enclosed code unless TESTING is defined.

#ifdef TESTING
...
#endif
Lula answered 2/10, 2015 at 13:43 Comment(6)
Am I missing something? I'm doing TDD and don't want to see warnings for tests that are being used by tests when I'm testing. If I were to produce a release build without the tests and with no implemented callers, then I would expect to see warnings, as the functions are truly not being used. Is there a way to have Rust warn me when the functions are actually not being used?Hitandrun
That's where the second case comes in, #[cfg(test)] would not turn off any dead code checks, so you would still have all warnings as expected, but the tests methods would only be included in a test version.Lula
Can this be configured project-wide in some Cargo configuration file?Summarize
Worth noting that cargo check --tests runs checks in the test configuration, and thus will not flag items as dead if they are used in tests.Stithy
It's #![allow(dead_code)], right? Your example is missing the !Beat
@MichaelMrozek #![...] applies the attribute to the encompassing scope whereas #[...] is just applied to the following item. As shown, this suppresses the warning only for that function. See Attributes in the Rust ReferenceSacksen
P
46

For people getting this warning while making a rust library, you may get this if you don't have your modules set to pub in your lib.rs.

pub mod foo;
Predict answered 9/6, 2021 at 4:12 Comment(3)
I found this useful when having a shared module in the /tests folder. Each test file is compiled separately to a test runner binary and so any functions not used by that particular test file are marked as 'never used' even if other test files use them. Marking 'mod' statements for the common module as 'pub' means those warnings go away.Kunzite
Nailed it. I always forget this piece of the puzzle when creating library functions.Presumptive
In my case, the functions provided by a library I wrote were not declared pub, which was the reason for the warnings, rust thought they were internal to the library but other than in the unit test, they were not used. Thank you so much!Manly
C
20

If something is only used in tests, it should be omitted altogether. This can be done with the #[cfg(test)] attribute.

Cornwall answered 2/10, 2015 at 6:20 Comment(4)
"If something is only used in tests, it should be omitted altogether." What if you're writing a library, where the intent is for others' code, not this one, to use it?Tehee
@SilasBarta: then it will be a public function, and not dead code.Cornwall
Because the other responses aren't saying this very explicitly, and I keep having to rediscover this myself: if you're getting a dead code warning on a library function that you have tests for, one common reason for it is that you forgot to mark it pub.Shinny
@rspeer Also, the method you are getting the warning for might be public but the struct that method belongs to might not be.Fries
S
14

There is another situation where this can occur. If you have several helper functions in a module, e.g. in tests/utils/mod.rs and then several integration tests (tests/a.rs, tests/b.rs) each of which does

mod utils;
use utils::...;

then you will get dead code warnings if you do not use all of the code from all of the tests. For example if test a.rs only uses utils::foo and b.rs only uses utils::bar then you will get dead code warnings for both.

That is because each test is compiled as an independent crate. Here is the bug report for it. It looks difficult to solve so I wouldn't hold my breath.

Spherical answered 9/6, 2021 at 10:35 Comment(1)
The only answer that faces the real problem. Adding a pub before mod utils fixed the problem for me, as pointed out in this comment. Thanks for the referenceEndolymph
C
7

dead_code is a lint, which means you can allow it on the thing that's causing it to trigger.

#[allow(dead_code)]
fn dummy() {}

fn main() {}
Conscription answered 2/10, 2015 at 5:31 Comment(0)
M
5

You can disable specific lints for a whole project in Rust by going into your main.rs file and adding the following at the very top of the file:

#![allow(
dead_code,
unused_imports
)]
Marshallmarshallese answered 11/12, 2021 at 21:1 Comment(0)
H
0

You max prefix the unused function name with an underscore:

fn _dummy() {}

fn main() {}

See: https://doc.rust-lang.org/rustc/lints/listing/warn-by-default.html#dead-code

Holoenzyme answered 1/7, 2021 at 14:18 Comment(0)
A
0

For some reason I found that setting the main function to public:

pub fn main()

and then copying my main.rs file to lib.rs

cp src/main.rs src/lib.rs

then recompiling fixed this.

No need to use a macro for it, although the macro should work for non main functions.

Alvaalvan answered 20/1, 2023 at 7:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.