rust-lang / rust-lang/rust-clippy

Module named test without cfgtest

Open
#8,568 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

Whenever you have a mod test block without a #[cfg(test)] it is probably an error.

I had code which had a package as a dev dependency and only being used in the tests. Running the tests worked but not building or running the binary. The error given was very confusing.

error[E0432]: unresolved import `approx`
  --> src/main.rs:57:9
   |
57 |     use approx::assert_relative_eq;
   |         ^^^^^^ use of undeclared crate or module `approx`
Lint Name

mod test without #[cfg(test)]

Category

suspicious, cargo

Advantage
  • Test module won't be compiled to binary/library by accident.
  • Forgetting #[cfg(test)] from test module is just probably a mistake almost all of the time.
Drawbacks

None that I can think of.

Example
mod test {
    use super::*;
    use approx::assert_relative_eq;
}

Could be written as:

#[cfg(test)]
mod test {
    use super::*;
    use approx::assert_relative_eq;
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating existing Clippy lints in the suspicious and cargo categories that inspect module declarations, then review their associated tests and registration points. Implement detection for an unconditionally compiled module named test and verify that the example without #[cfg(test)] is warned about while the guarded form is accepted.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.