Skip to content

Commit 7c2cf06

Browse files
committed
refactor: redundant_test_prefix make suffix configurable
1 parent ac7cbf3 commit 7c2cf06

12 files changed

+246
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6461,4 +6461,5 @@ Released 2018-09-13
64616461
[`warn-on-all-wildcard-imports`]: https://doc.rust-lang.org/clippy/lint_configuration.html#warn-on-all-wildcard-imports
64626462
[`warn-unsafe-macro-metavars-in-private-macros`]: https://doc.rust-lang.org/clippy/lint_configuration.html#warn-unsafe-macro-metavars-in-private-macros
64636463
[`redundant-test-prefix-in-integration-tests`]: https://doc.rust-lang.org/clippy/lint_configuration.html#redundant-test-prefix-in-integration-tests
6464+
[`redundant-test-prefix-custom-suffix`]: https://doc.rust-lang.org/clippy/lint_configuration.html#redundant-test-prefix-custom-suffix
64646465
<!-- end autogenerated links to configuration documentation -->

book/src/lint_configuration.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,3 +1098,17 @@ Whether to include integration tests in the linting process or not.
10981098
---
10991099
**Affected lints:**
11001100
* [`redundant_test_prefix`](https://rust-lang.github.io/rust-clippy/master/index.html#redundant_test_prefix)
1101+
1102+
1103+
## `redundant-test-prefix-custom-suffix`
1104+
What suffix to use to avoid function name collisions when `test_` prefix is removed.
1105+
1106+
If set to `"_works"`, the lint will suggest renaming `test_foo` to `foo_works`.
1107+
Suffix is added only when there is a collision with an existing function name,
1108+
otherwise just `test_` prefix is removed (and no suffix added).
1109+
1110+
**Default Value:** `"_works"`
1111+
1112+
---
1113+
**Affected lints:**
1114+
* [`redundant_test_prefix`](https://rust-lang.github.io/rust-clippy/master/index.html#redundant_test_prefix)

clippy_config/src/conf.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,13 @@ define_Conf! {
849849
/// Whether to include integration tests in the linting process or not.
850850
#[lints(redundant_test_prefix)]
851851
redundant_test_prefix_in_integration_tests: bool = false,
852+
/// What suffix to use to avoid function name collisions when `test_` prefix is removed.
853+
///
854+
/// If set to `"_works"`, the lint will suggest renaming `test_foo` to `foo_works`.
855+
/// Suffix is added only when there is a collision with an existing function name,
856+
/// otherwise just `test_` prefix is removed (and no suffix added).
857+
#[lints(redundant_test_prefix)]
858+
redundant_test_prefix_custom_suffix: String = String::from("_works"),
852859
}
853860

854861
/// Search for the configuration file.

clippy_lints/src/redundant_test_prefix.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,14 @@ declare_clippy_lint! {
4343

4444
pub struct RedundantTestPrefix {
4545
in_integration_tests: bool,
46+
custom_sufix: String,
4647
}
4748

4849
impl RedundantTestPrefix {
4950
pub fn new(conf: &'static Conf) -> Self {
5051
Self {
5152
in_integration_tests: conf.redundant_test_prefix_in_integration_tests,
53+
custom_sufix: conf.redundant_test_prefix_custom_suffix.clone(),
5254
}
5355
}
5456
}
@@ -83,7 +85,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantTestPrefix {
8385
// If `non_prefixed` conflicts with another function in the same module/scope,
8486
// add extra suffix to avoid name conflict.
8587
if name_conflicts(cx, body, &non_prefixed) {
86-
non_prefixed.push_str("_works");
88+
non_prefixed.push_str(&self.custom_sufix);
8789
help_msg = "consider removing the `test_` prefix (suffix avoids name conflict)";
8890
}
8991
span_lint_and_sugg(
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
redundant-test-prefix-custom-suffix = "_ok"
2+
redundant-test-prefix-in-integration-tests = true
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//@revisions: default integ_tests suffix_tests
2+
//@[default] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/redundant_test_prefix/default
3+
//@[integ_tests] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/redundant_test_prefix/in_integration_tests
4+
//@[suffix_tests] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/redundant_test_prefix/custom_suffix_tests
5+
//@compile-flags: --test
6+
#![allow(dead_code)]
7+
#![warn(clippy::redundant_test_prefix)]
8+
9+
fn main() {}
10+
11+
mod tests_no_annotations {
12+
use super::*;
13+
14+
#[test]
15+
fn test_has_annotation() {
16+
//~[integ_tests]^ redundant_test_prefix
17+
}
18+
19+
fn no_annotation() {}
20+
}
21+
22+
#[test]
23+
fn test_main_module_has_annotation() {
24+
//~[integ_tests]^ redundant_test_prefix
25+
}
26+
27+
fn test_main_module_no_annotation() {}
28+
29+
fn foo() {}
30+
31+
#[cfg(test)]
32+
#[test]
33+
fn foo_works() {
34+
//~^ ERROR: redundant `test_` prefix in test function name
35+
//~| HELP: consider removing the `test_` prefix (suffix avoids name conflict)
36+
37+
todo!()
38+
// Has prefix, has `#[test]` attribute, within a `#[cfg(test)]`.
39+
// Collision with existing function, so suffix is added.
40+
}
41+
42+
fn bar() {}
43+
44+
#[test]
45+
fn test_bar() {
46+
//~[suffix_tests]^ redundant_test_prefix
47+
48+
todo!()
49+
// Has prefix, has `#[test]` attribute.
50+
// NOT within a `#[cfg(test)]`, but the lint is enabled for integration tests.
51+
// Collision with existing function, so suffix is added.
52+
}
53+
54+
#[cfg(test)]
55+
mod tests {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: redundant `test_` prefix in test function name
2+
--> tests/ui-toml/redundant_test_prefix/redundant_test_prefix.rs:33:4
3+
|
4+
LL | fn test_foo() {
5+
| ^^^^^^^^ help: consider removing the `test_` prefix (suffix avoids name conflict): `foo_works`
6+
|
7+
= note: `-D clippy::redundant-test-prefix` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::redundant_test_prefix)]`
9+
10+
error: aborting due to 1 previous error
11+

tests/ui-toml/redundant_test_prefix/redundant_test_prefix.integ_tests.fixed

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
//@revisions: default integ_tests
1+
//@revisions: default integ_tests suffix_tests
22
//@[default] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/redundant_test_prefix/default
33
//@[integ_tests] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/redundant_test_prefix/in_integration_tests
4+
//@[suffix_tests] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/redundant_test_prefix/custom_suffix_tests
45
//@compile-flags: --test
56
#![allow(dead_code)]
67
#![warn(clippy::redundant_test_prefix)]
@@ -25,5 +26,30 @@ fn main_module_has_annotation() {
2526

2627
fn test_main_module_no_annotation() {}
2728

29+
fn foo() {}
30+
31+
#[cfg(test)]
32+
#[test]
33+
fn foo_works() {
34+
//~^ ERROR: redundant `test_` prefix in test function name
35+
//~| HELP: consider removing the `test_` prefix (suffix avoids name conflict)
36+
37+
todo!()
38+
// Has prefix, has `#[test]` attribute, within a `#[cfg(test)]`.
39+
// Collision with existing function, so suffix is added.
40+
}
41+
42+
fn bar() {}
43+
44+
#[test]
45+
fn bar_works() {
46+
//~[suffix_tests]^ redundant_test_prefix
47+
48+
todo!()
49+
// Has prefix, has `#[test]` attribute.
50+
// NOT within a `#[cfg(test)]`, but the lint is enabled for integration tests.
51+
// Collision with existing function, so suffix is added.
52+
}
53+
2854
#[cfg(test)]
2955
mod tests {}
Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: redundant `test_` prefix in test function name
2-
--> tests/ui-toml/redundant_test_prefix/redundant_test_prefix.rs:14:8
2+
--> tests/ui-toml/redundant_test_prefix/redundant_test_prefix.rs:15:8
33
|
44
LL | fn test_has_annotation() {
55
| ^^^^^^^^^^^^^^^^^^^ help: consider removing the `test_` prefix: `has_annotation`
@@ -8,10 +8,22 @@ LL | fn test_has_annotation() {
88
= help: to override `-D warnings` add `#[allow(clippy::redundant_test_prefix)]`
99

1010
error: redundant `test_` prefix in test function name
11-
--> tests/ui-toml/redundant_test_prefix/redundant_test_prefix.rs:22:4
11+
--> tests/ui-toml/redundant_test_prefix/redundant_test_prefix.rs:23:4
1212
|
1313
LL | fn test_main_module_has_annotation() {
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `test_` prefix: `main_module_has_annotation`
1515

16-
error: aborting due to 2 previous errors
16+
error: redundant `test_` prefix in test function name
17+
--> tests/ui-toml/redundant_test_prefix/redundant_test_prefix.rs:33:4
18+
|
19+
LL | fn test_foo() {
20+
| ^^^^^^^^ help: consider removing the `test_` prefix (suffix avoids name conflict): `foo_works`
21+
22+
error: redundant `test_` prefix in test function name
23+
--> tests/ui-toml/redundant_test_prefix/redundant_test_prefix.rs:45:4
24+
|
25+
LL | fn test_bar() {
26+
| ^^^^^^^^ help: consider removing the `test_` prefix (suffix avoids name conflict): `bar_works`
27+
28+
error: aborting due to 4 previous errors
1729

tests/ui-toml/redundant_test_prefix/redundant_test_prefix.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
//@revisions: default integ_tests
1+
//@revisions: default integ_tests suffix_tests
22
//@[default] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/redundant_test_prefix/default
33
//@[integ_tests] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/redundant_test_prefix/in_integration_tests
4+
//@[suffix_tests] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/redundant_test_prefix/custom_suffix_tests
45
//@compile-flags: --test
56
#![allow(dead_code)]
67
#![warn(clippy::redundant_test_prefix)]
@@ -25,5 +26,30 @@ fn test_main_module_has_annotation() {
2526

2627
fn test_main_module_no_annotation() {}
2728

29+
fn foo() {}
30+
31+
#[cfg(test)]
32+
#[test]
33+
fn test_foo() {
34+
//~^ ERROR: redundant `test_` prefix in test function name
35+
//~| HELP: consider removing the `test_` prefix (suffix avoids name conflict)
36+
37+
todo!()
38+
// Has prefix, has `#[test]` attribute, within a `#[cfg(test)]`.
39+
// Collision with existing function, so suffix is added.
40+
}
41+
42+
fn bar() {}
43+
44+
#[test]
45+
fn test_bar() {
46+
//~[suffix_tests]^ redundant_test_prefix
47+
48+
todo!()
49+
// Has prefix, has `#[test]` attribute.
50+
// NOT within a `#[cfg(test)]`, but the lint is enabled for integration tests.
51+
// Collision with existing function, so suffix is added.
52+
}
53+
2854
#[cfg(test)]
2955
mod tests {}

0 commit comments

Comments
 (0)