-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat: add cfg_not_test lint #11293
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
Merged
Merged
feat: add cfg_not_test lint #11293
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use clippy_utils::diagnostics::span_lint_and_then; | ||
use rustc_ast::NestedMetaItem; | ||
use rustc_lint::{EarlyContext, EarlyLintPass}; | ||
use rustc_session::declare_lint_pass; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for usage of `cfg` that excludes code from `test` builds. (i.e., `#{cfg(not(test))]`) | ||
/// | ||
/// ### Why is this bad? | ||
/// This may give the false impression that a codebase has 100% coverage, yet actually has untested code. | ||
/// Enabling this also guards against excessive mockery as well, which is an anti-pattern. | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// # fn important_check() {} | ||
/// #[cfg(not(test))] | ||
/// important_check(); // I'm not actually tested, but not including me will falsely increase coverage! | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// # fn important_check() {} | ||
/// important_check(); | ||
/// ``` | ||
#[clippy::version = "1.73.0"] | ||
pub CFG_NOT_TEST, | ||
restriction, | ||
"enforce against excluding code from test builds" | ||
} | ||
|
||
declare_lint_pass!(CfgNotTest => [CFG_NOT_TEST]); | ||
|
||
impl EarlyLintPass for CfgNotTest { | ||
fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &rustc_ast::Attribute) { | ||
if attr.has_name(rustc_span::sym::cfg) && contains_not_test(attr.meta_item_list().as_deref(), false) { | ||
span_lint_and_then( | ||
cx, | ||
CFG_NOT_TEST, | ||
attr.span, | ||
"code is excluded from test builds", | ||
|diag| { | ||
diag.help("consider not excluding any code from test builds"); | ||
diag.note_once("this could increase code coverage despite not actually being tested"); | ||
}, | ||
); | ||
} | ||
} | ||
} | ||
|
||
fn contains_not_test(list: Option<&[NestedMetaItem]>, not: bool) -> bool { | ||
Centri3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
list.is_some_and(|list| { | ||
list.iter().any(|item| { | ||
item.ident().is_some_and(|ident| match ident.name { | ||
rustc_span::sym::not => contains_not_test(item.meta_item_list(), !not), | ||
rustc_span::sym::test => not, | ||
_ => contains_not_test(item.meta_item_list(), not), | ||
}) | ||
}) | ||
}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#![allow(unused)] | ||
mrnossiom marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#![warn(clippy::cfg_not_test)] | ||
|
||
fn important_check() {} | ||
|
||
fn main() { | ||
// Statement | ||
#[cfg(not(test))] | ||
let answer = 42; | ||
|
||
// Expression | ||
#[cfg(not(test))] | ||
important_check(); | ||
|
||
// Make sure only not(test) are checked, not other attributes | ||
#[cfg(not(foo))] | ||
important_check(); | ||
} | ||
|
||
#[cfg(not(not(test)))] | ||
struct CfgNotTest; | ||
|
||
// Deeply nested `not(test)` | ||
#[cfg(not(test))] | ||
fn foo() {} | ||
#[cfg(all(debug_assertions, not(test)))] | ||
fn bar() {} | ||
#[cfg(not(any(not(debug_assertions), test)))] | ||
fn baz() {} | ||
|
||
#[cfg(test)] | ||
mod tests {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
error: code is excluded from test builds | ||
--> tests/ui/cfg_not_test.rs:8:5 | ||
| | ||
LL | #[cfg(not(test))] | ||
| ^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: consider not excluding any code from test builds | ||
= note: this could increase code coverage despite not actually being tested | ||
= note: `-D clippy::cfg-not-test` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::cfg_not_test)]` | ||
|
||
error: code is excluded from test builds | ||
--> tests/ui/cfg_not_test.rs:12:5 | ||
| | ||
LL | #[cfg(not(test))] | ||
| ^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: consider not excluding any code from test builds | ||
|
||
error: code is excluded from test builds | ||
--> tests/ui/cfg_not_test.rs:24:1 | ||
| | ||
LL | #[cfg(not(test))] | ||
| ^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: consider not excluding any code from test builds | ||
|
||
error: code is excluded from test builds | ||
--> tests/ui/cfg_not_test.rs:26:1 | ||
| | ||
LL | #[cfg(all(debug_assertions, not(test)))] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: consider not excluding any code from test builds | ||
|
||
error: code is excluded from test builds | ||
--> tests/ui/cfg_not_test.rs:28:1 | ||
| | ||
LL | #[cfg(not(any(not(debug_assertions), test)))] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: consider not excluding any code from test builds | ||
|
||
error: aborting due to 5 previous errors | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.