Skip to content

Commit d665e0e

Browse files
committed
refactor: redundant_test_prefix rename option
1 parent 0fec847 commit d665e0e

File tree

14 files changed

+64
-60
lines changed

14 files changed

+64
-60
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6439,6 +6439,8 @@ Released 2018-09-13
64396439
[`msrv`]: https://doc.rust-lang.org/clippy/lint_configuration.html#msrv
64406440
[`pass-by-value-size-limit`]: https://doc.rust-lang.org/clippy/lint_configuration.html#pass-by-value-size-limit
64416441
[`pub-underscore-fields-behavior`]: https://doc.rust-lang.org/clippy/lint_configuration.html#pub-underscore-fields-behavior
6442+
[`redundant-test-prefix-check-outside-cfg-test`]: https://doc.rust-lang.org/clippy/lint_configuration.html#redundant-test-prefix-check-outside-cfg-test
6443+
[`redundant-test-prefix-custom-suffix`]: https://doc.rust-lang.org/clippy/lint_configuration.html#redundant-test-prefix-custom-suffix
64426444
[`semicolon-inside-block-ignore-singleline`]: https://doc.rust-lang.org/clippy/lint_configuration.html#semicolon-inside-block-ignore-singleline
64436445
[`semicolon-outside-block-ignore-multiline`]: https://doc.rust-lang.org/clippy/lint_configuration.html#semicolon-outside-block-ignore-multiline
64446446
[`single-char-binding-names-threshold`]: https://doc.rust-lang.org/clippy/lint_configuration.html#single-char-binding-names-threshold
@@ -6460,6 +6462,4 @@ Released 2018-09-13
64606462
[`verbose-bit-mask-threshold`]: https://doc.rust-lang.org/clippy/lint_configuration.html#verbose-bit-mask-threshold
64616463
[`warn-on-all-wildcard-imports`]: https://doc.rust-lang.org/clippy/lint_configuration.html#warn-on-all-wildcard-imports
64626464
[`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
6464-
[`redundant-test-prefix-custom-suffix`]: https://doc.rust-lang.org/clippy/lint_configuration.html#redundant-test-prefix-custom-suffix
64656465
<!-- 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
@@ -869,6 +869,34 @@ exported visibility, or whether they are marked as "pub".
869869
* [`pub_underscore_fields`](https://rust-lang.github.io/rust-clippy/master/index.html#pub_underscore_fields)
870870

871871

872+
## `redundant-test-prefix-check-outside-cfg-test`
873+
Whether to include functions outside of `#[cfg(test)]` in the linting process or not.
874+
875+
This option allows running the lint against the integration tests: test functions located
876+
there are not inside a node marked with `#[cfg(test)]` annotation (although they are
877+
still marked using `#[test]` annotation and thus can have redundant "test_" prefix).
878+
879+
**Default Value:** `false`
880+
881+
---
882+
**Affected lints:**
883+
* [`redundant_test_prefix`](https://rust-lang.github.io/rust-clippy/master/index.html#redundant_test_prefix)
884+
885+
886+
## `redundant-test-prefix-custom-suffix`
887+
What suffix to use to avoid function name collisions when `test_` prefix is removed.
888+
889+
If set to `"_works"`, the lint will suggest renaming `test_foo` to `foo_works`.
890+
Suffix is added only when there is a collision with an existing function name,
891+
otherwise just `test_` prefix is removed (and no suffix added).
892+
893+
**Default Value:** `"_works"`
894+
895+
---
896+
**Affected lints:**
897+
* [`redundant_test_prefix`](https://rust-lang.github.io/rust-clippy/master/index.html#redundant_test_prefix)
898+
899+
872900
## `semicolon-inside-block-ignore-singleline`
873901
Whether to lint only if it's multiline.
874902

@@ -1088,27 +1116,3 @@ Whether to also emit warnings for unsafe blocks with metavariable expansions in
10881116
---
10891117
**Affected lints:**
10901118
* [`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)
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_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.

0 commit comments

Comments
 (0)