Skip to content

Commit 4acfacb

Browse files
committed
new lint: redundant_test_prefix basic help, no auto-fix suggestion
1 parent a5a033d commit 4acfacb

File tree

8 files changed

+144
-0
lines changed

8 files changed

+144
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6115,6 +6115,7 @@ Released 2018-09-13
61156115
[`redundant_pub_crate`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pub_crate
61166116
[`redundant_slicing`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_slicing
61176117
[`redundant_static_lifetimes`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_static_lifetimes
6118+
[`redundant_test_prefix`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_test_prefix
61186119
[`redundant_type_annotations`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_type_annotations
61196120
[`ref_as_ptr`]: https://rust-lang.github.io/rust-clippy/master/index.html#ref_as_ptr
61206121
[`ref_binding_to_reference`]: https://rust-lang.github.io/rust-clippy/master/index.html#ref_binding_to_reference
@@ -6459,4 +6460,5 @@ Released 2018-09-13
64596460
[`verbose-bit-mask-threshold`]: https://doc.rust-lang.org/clippy/lint_configuration.html#verbose-bit-mask-threshold
64606461
[`warn-on-all-wildcard-imports`]: https://doc.rust-lang.org/clippy/lint_configuration.html#warn-on-all-wildcard-imports
64616462
[`warn-unsafe-macro-metavars-in-private-macros`]: https://doc.rust-lang.org/clippy/lint_configuration.html#warn-unsafe-macro-metavars-in-private-macros
6463+
[`redundant-test-prefix-in-integration-tests`]: https://doc.rust-lang.org/clippy/lint_configuration.html#redundant-test-prefix-in-integration-tests
64626464
<!-- end autogenerated links to configuration documentation -->

book/src/lint_configuration.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,3 +1088,13 @@ Whether to also emit warnings for unsafe blocks with metavariable expansions in
10881088
---
10891089
**Affected lints:**
10901090
* [`macro_metavars_in_unsafe`](https://rust-lang.github.io/rust-clippy/master/index.html#macro_metavars_in_unsafe)
1091+
1092+
1093+
## `redundant-test-prefix-in-integration-tests`
1094+
Whether to include integration tests in the linting process or not.
1095+
1096+
**Default Value:** `false`
1097+
1098+
---
1099+
**Affected lints:**
1100+
* [`redundant_test_prefix`](https://rust-lang.github.io/rust-clippy/master/index.html#redundant_test_prefix)

clippy_config/src/conf.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,9 @@ define_Conf! {
846846
/// Whether to also emit warnings for unsafe blocks with metavariable expansions in **private** macros.
847847
#[lints(macro_metavars_in_unsafe)]
848848
warn_unsafe_macro_metavars_in_private_macros: bool = false,
849+
/// Whether to include integration tests in the linting process or not.
850+
#[lints(redundant_test_prefix)]
851+
redundant_test_prefix_in_integration_tests: bool = false,
849852
}
850853

851854
/// Search for the configuration file.

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,7 @@ pub static LINTS: &[&crate::LintInfo] = &[
667667
crate::redundant_slicing::DEREF_BY_SLICING_INFO,
668668
crate::redundant_slicing::REDUNDANT_SLICING_INFO,
669669
crate::redundant_static_lifetimes::REDUNDANT_STATIC_LIFETIMES_INFO,
670+
crate::redundant_test_prefix::REDUNDANT_TEST_PREFIX_INFO,
670671
crate::redundant_type_annotations::REDUNDANT_TYPE_ANNOTATIONS_INFO,
671672
crate::ref_option_ref::REF_OPTION_REF_INFO,
672673
crate::ref_patterns::REF_PATTERNS_INFO,

clippy_lints/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ mod redundant_locals;
319319
mod redundant_pub_crate;
320320
mod redundant_slicing;
321321
mod redundant_static_lifetimes;
322+
mod redundant_test_prefix;
322323
mod redundant_type_annotations;
323324
mod ref_option_ref;
324325
mod ref_patterns;
@@ -982,5 +983,6 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
982983
store.register_late_pass(move |_| Box::new(non_std_lazy_statics::NonStdLazyStatic::new(conf)));
983984
store.register_late_pass(|_| Box::new(manual_option_as_slice::ManualOptionAsSlice::new(conf)));
984985
store.register_late_pass(|_| Box::new(single_option_map::SingleOptionMap));
986+
store.register_late_pass(move |_| Box::new(redundant_test_prefix::RedundantTestPrefix::new(conf)));
985987
// add lints here, do not remove this comment, it's used in `new_lint`
986988
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
use clippy_config::Conf;
2+
use clippy_utils::diagnostics::span_lint_and_help;
3+
use clippy_utils::{is_in_cfg_test, is_in_test_function};
4+
use rustc_hir::intravisit::FnKind;
5+
use rustc_hir::{Body, FnDecl};
6+
use rustc_lint::{LateContext, LateLintPass};
7+
use rustc_session::{ impl_lint_pass};
8+
use rustc_span::Span;
9+
use rustc_span::def_id::LocalDefId;
10+
11+
declare_clippy_lint! {
12+
/// ### What it does
13+
/// Checks for test functions (functions annotated with `#[test]`) that prefixed with `test_`
14+
/// which is redundant.
15+
///
16+
/// ### Why is this bad?
17+
/// This is redundant because the test functions are already annotated with `#[test]`.
18+
/// Moreover, it clutters the output of `cargo test` as test functions are expanded as
19+
/// `module::tests::test_name` in the output.
20+
///
21+
/// ### Example
22+
/// ```no_run
23+
/// #[test]
24+
/// fn test_my_use_case() {
25+
/// // test code
26+
/// }
27+
/// ```
28+
/// Use instead:
29+
/// ```no_run
30+
/// fn my_use_case() {
31+
/// // test code
32+
/// }
33+
/// ```
34+
#[clippy::version = "1.84.0"]
35+
pub REDUNDANT_TEST_PREFIX,
36+
pedantic,
37+
"redundant `test_` prefix in test function name"
38+
}
39+
40+
pub struct RedundantTestPrefix {
41+
in_integration_tests: bool,
42+
}
43+
44+
impl RedundantTestPrefix {
45+
pub fn new(conf: &'static Conf) -> Self {
46+
Self {
47+
in_integration_tests: conf.redundant_test_prefix_in_integration_tests,
48+
}
49+
}
50+
}
51+
52+
impl_lint_pass!(RedundantTestPrefix => [REDUNDANT_TEST_PREFIX]);
53+
54+
impl LateLintPass<'_> for RedundantTestPrefix {
55+
fn check_fn(
56+
&mut self,
57+
cx: &LateContext<'_>,
58+
fn_kind: FnKind<'_>,
59+
_: &FnDecl<'_>,
60+
body: &Body<'_>,
61+
span: Span,
62+
_: LocalDefId,
63+
) {
64+
// Skip the lint if the function is not within a node with `#[cfg(test)]` attribute,
65+
// which is true for integration tests. If the lint is enabled for integration tests,
66+
// via configuration value, ignore this check.
67+
if !(self.in_integration_tests || is_in_cfg_test(cx.tcx, body.id().hir_id)) {
68+
return;
69+
}
70+
71+
if is_in_test_function(cx.tcx, body.id().hir_id) && has_redundant_test_prefix(fn_kind) {
72+
span_lint_and_help(
73+
cx,
74+
REDUNDANT_TEST_PREFIX,
75+
span,
76+
"redundant `test_` prefix in test function name",
77+
None,
78+
"consider removing the `test_` prefix",
79+
);
80+
}
81+
}
82+
}
83+
84+
/// Checks if the function has redundant `test_` prefix in its name.
85+
fn has_redundant_test_prefix(fn_kind: FnKind<'_>) -> bool {
86+
matches!(fn_kind, FnKind::ItemFn(ident, ..) if ident.name.as_str().starts_with("test_"))
87+
}

tests/ui/redundant_test_prefix.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#![allow(dead_code)]
2+
#![warn(clippy::redundant_test_prefix)]
3+
4+
fn main() {
5+
// test code goes here
6+
}
7+
8+
#[cfg(test)]
9+
mod tests {
10+
use super::*;
11+
12+
#[test]
13+
fn test_main() {
14+
main();
15+
}
16+
}
17+
18+
mod tests_no_annotations {
19+
use super::*;
20+
21+
#[test]
22+
fn test_main() {
23+
main();
24+
}
25+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: redundant `test_` prefix in test function name
2+
--> tests/ui/redundant_test_prefix.rs:13:5
3+
|
4+
LL | / fn test_main() {
5+
LL | | main();
6+
LL | | }
7+
| |_____^
8+
|
9+
= help: consider removing the `test_` prefix
10+
= note: `-D clippy::redundant-test-prefix` implied by `-D warnings`
11+
= help: to override `-D warnings` add `#[allow(clippy::redundant_test_prefix)]`
12+
13+
error: aborting due to 1 previous error
14+

0 commit comments

Comments
 (0)