rust-lang / rust-lang/rust-clippy

`unnecessary_cfg_test`

Open
#8,697 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

What it does

Since #[test] is already implicitly #[cfg(test)], it would not be needed to use #[cfg(test)] directly on a function.

I propose this lint to catch cases of

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

Suggesting instead to either remove the cfg (for inline tests), to add it to the module itself (for inline modules, e.g. #[cfg(test)] mod test {}) or to use #![cfg(test)] at the head of the file (for test files).

Lint Name

unnecessary_cfg_test

Category

style

Advantage

Removes redundant code

Drawbacks

None that I'm aware of

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

Could be written as:

// test file
#![cfg(test)]

#[test]
fn test() {}

// inline test
#[test]
fn test() {}

// inline test module
#[cfg(test)] 
mod test {
    #[test]
    fn test() {}
}

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

The issue does not name implementation files or tests; start by reading its examples and the referenced Cargo issue to understand the proposed cases. Done means adding the unnecessary_cfg_test lint and ensuring it handles inline tests, inline test modules, and test files as described.

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.