Skip to content

Commit 24aa783

Browse files
committed
refactor: redundant_test_prefix rename option
1 parent e48de8a commit 24aa783

14 files changed

+64
-60
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6215,6 +6215,8 @@ Released 2018-09-13
62156215
[`msrv`]: https://doc.rust-lang.org/clippy/lint_configuration.html#msrv
62166216
[`pass-by-value-size-limit`]: https://doc.rust-lang.org/clippy/lint_configuration.html#pass-by-value-size-limit
62176217
[`pub-underscore-fields-behavior`]: https://doc.rust-lang.org/clippy/lint_configuration.html#pub-underscore-fields-behavior
6218+
[`redundant-test-prefix-check-outside-cfg-test`]: https://doc.rust-lang.org/clippy/lint_configuration.html#redundant-test-prefix-check-outside-cfg-test
6219+
[`redundant-test-prefix-custom-suffix`]: https://doc.rust-lang.org/clippy/lint_configuration.html#redundant-test-prefix-custom-suffix
62186220
[`semicolon-inside-block-ignore-singleline`]: https://doc.rust-lang.org/clippy/lint_configuration.html#semicolon-inside-block-ignore-singleline
62196221
[`semicolon-outside-block-ignore-multiline`]: https://doc.rust-lang.org/clippy/lint_configuration.html#semicolon-outside-block-ignore-multiline
62206222
[`single-char-binding-names-threshold`]: https://doc.rust-lang.org/clippy/lint_configuration.html#single-char-binding-names-threshold
@@ -6236,6 +6238,4 @@ Released 2018-09-13
62366238
[`verbose-bit-mask-threshold`]: https://doc.rust-lang.org/clippy/lint_configuration.html#verbose-bit-mask-threshold
62376239
[`warn-on-all-wildcard-imports`]: https://doc.rust-lang.org/clippy/lint_configuration.html#warn-on-all-wildcard-imports
62386240
[`warn-unsafe-macro-metavars-in-private-macros`]: https://doc.rust-lang.org/clippy/lint_configuration.html#warn-unsafe-macro-metavars-in-private-macros
6239-
[`redundant-test-prefix-in-integration-tests`]: https://doc.rust-lang.org/clippy/lint_configuration.html#redundant-test-prefix-in-integration-tests
6240-
[`redundant-test-prefix-custom-suffix`]: https://doc.rust-lang.org/clippy/lint_configuration.html#redundant-test-prefix-custom-suffix
62416241
<!-- end autogenerated links to configuration documentation -->

book/src/lint_configuration.md

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,34 @@ exported visibility, or whether they are marked as "pub".
764764
* [`pub_underscore_fields`](https://rust-lang.github.io/rust-clippy/master/index.html#pub_underscore_fields)
765765

766766

767+
## `redundant-test-prefix-check-outside-cfg-test`
768+
Whether to include functions outside of `#[cfg(test)]` in the linting process or not.
769+
770+
This option allows running the lint against the integration tests: test functions located
771+
there are not inside a node marked with `#[cfg(test)]` annotation (although they are
772+
still marked using `#[test]` annotation and thus can have redundant "test_" prefix).
773+
774+
**Default Value:** `false`
775+
776+
---
777+
**Affected lints:**
778+
* [`redundant_test_prefix`](https://rust-lang.github.io/rust-clippy/master/index.html#redundant_test_prefix)
779+
780+
781+
## `redundant-test-prefix-custom-suffix`
782+
What suffix to use to avoid function name collisions when `test_` prefix is removed.
783+
784+
If set to `"_works"`, the lint will suggest renaming `test_foo` to `foo_works`.
785+
Suffix is added only when there is a collision with an existing function name,
786+
otherwise just `test_` prefix is removed (and no suffix added).
787+
788+
**Default Value:** `"_works"`
789+
790+
---
791+
**Affected lints:**
792+
* [`redundant_test_prefix`](https://rust-lang.github.io/rust-clippy/master/index.html#redundant_test_prefix)
793+
794+
767795
## `semicolon-inside-block-ignore-singleline`
768796
Whether to lint only if it's multiline.
769797

@@ -982,27 +1010,3 @@ Whether to also emit warnings for unsafe blocks with metavariable expansions in
9821010
---
9831011
**Affected lints:**
9841012
* [`macro_metavars_in_unsafe`](https://rust-lang.github.io/rust-clippy/master/index.html#macro_metavars_in_unsafe)
985-
986-
987-
## `redundant-test-prefix-in-integration-tests`
988-
Whether to include integration tests in the linting process or not.
989-
990-
**Default Value:** `false`
991-
992-
---
993-
**Affected lints:**
994-
* [`redundant_test_prefix`](https://rust-lang.github.io/rust-clippy/master/index.html#redundant_test_prefix)
995-
996-
997-
## `redundant-test-prefix-custom-suffix`
998-
What suffix to use to avoid function name collisions when `test_` prefix is removed.
999-
1000-
If set to `"_works"`, the lint will suggest renaming `test_foo` to `foo_works`.
1001-
Suffix is added only when there is a collision with an existing function name,
1002-
otherwise just `test_` prefix is removed (and no suffix added).
1003-
1004-
**Default Value:** `"_works"`
1005-
1006-
---
1007-
**Affected lints:**
1008-
* [`redundant_test_prefix`](https://rust-lang.github.io/rust-clippy/master/index.html#redundant_test_prefix)

clippy_lints/src/redundant_test_prefix.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ declare_clippy_lint! {
4242
}
4343

4444
pub struct RedundantTestPrefix {
45-
in_integration_tests: bool,
45+
check_outside_test_cfg: bool,
4646
custom_sufix: String,
4747
}
4848

4949
impl RedundantTestPrefix {
5050
pub fn new(conf: &'static Conf) -> Self {
5151
Self {
52-
in_integration_tests: conf.redundant_test_prefix_in_integration_tests,
52+
check_outside_test_cfg: conf.redundant_test_prefix_check_outside_cfg_test,
5353
custom_sufix: conf.redundant_test_prefix_custom_suffix.clone(),
5454
}
5555
}
@@ -72,10 +72,10 @@ impl<'tcx> LateLintPass<'tcx> for RedundantTestPrefix {
7272
return;
7373
};
7474

75-
// Skip the lint if the function is not within a node with `#[cfg(test)]` attribute,
76-
// which is true for integration tests. If the lint is enabled for integration tests,
77-
// via configuration value, ignore this check.
78-
if !(self.in_integration_tests || is_in_cfg_test(cx.tcx, body.id().hir_id)) {
75+
// Skip the lint if the function is not within a node marked with `#[cfg(test)]` attribute.
76+
// Continue if the function is inside the node marked with `#[cfg(test)]` or lint is enforced
77+
// via configuration (most likely to include integration tests in lint's scope).
78+
if !(self.check_outside_test_cfg || is_in_cfg_test(cx.tcx, body.id().hir_id)) {
7979
return;
8080
}
8181

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-check-outside-cfg-test = true

tests/ui-toml/redundant_test_prefix/custom_suffix_tests/clippy.toml

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
redundant-test-prefix-in-integration-tests = false
1+
redundant-test-prefix-check-outside-cfg-test = false

tests/ui-toml/redundant_test_prefix/in_integration_tests/clippy.toml

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
redundant-test-prefix-check-outside-cfg-test = true

tests/ui-toml/redundant_test_prefix/redundant_test_prefix.suffix_tests.fixed renamed to tests/ui-toml/redundant_test_prefix/redundant_test_prefix.custom_suffix.fixed

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
//@revisions: default integ_tests suffix_tests
1+
//@revisions: default outside_cfg_test custom_suffix
22
//@[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
3+
//@[outside_cfg_test] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/redundant_test_prefix/outside_cfg_test
4+
//@[custom_suffix] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/redundant_test_prefix/custom_suffix
55
//@compile-flags: --test
66
#![allow(dead_code)]
77
#![warn(clippy::redundant_test_prefix)]
@@ -13,15 +13,15 @@ mod tests_no_annotations {
1313

1414
#[test]
1515
fn has_annotation() {
16-
//~[integ_tests]^ redundant_test_prefix
16+
//~[outside_cfg_test]^ redundant_test_prefix
1717
}
1818

1919
fn no_annotation() {}
2020
}
2121

2222
#[test]
2323
fn main_module_has_annotation() {
24-
//~[integ_tests]^ redundant_test_prefix
24+
//~[outside_cfg_test]^ redundant_test_prefix
2525
}
2626

2727
fn test_main_module_no_annotation() {}
@@ -43,7 +43,7 @@ fn bar() {}
4343

4444
#[test]
4545
fn bar_ok() {
46-
//~[suffix_tests]^ redundant_test_prefix
46+
//~[custom_suffix]^ redundant_test_prefix
4747

4848
todo!()
4949
// Has prefix, has `#[test]` attribute.
File renamed without changes.

0 commit comments

Comments
 (0)