Skip to content

Commit f6ce62a

Browse files
committed
test: added actual ui-toml tests
1 parent a4d8766 commit f6ce62a

File tree

16 files changed

+271
-32
lines changed

16 files changed

+271
-32
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6216,7 +6216,9 @@ Released 2018-09-13
62166216
[`standard-macro-braces`]: https://doc.rust-lang.org/clippy/lint_configuration.html#standard-macro-braces
62176217
[`struct-field-name-threshold`]: https://doc.rust-lang.org/clippy/lint_configuration.html#struct-field-name-threshold
62186218
[`suppress-restriction-lint-in-const`]: https://doc.rust-lang.org/clippy/lint_configuration.html#suppress-restriction-lint-in-const
6219-
[`test-without-fail-case`]: https://doc.rust-lang.org/clippy/lint_configuration.html#test-without-fail-case
6219+
[`test-without-fail-case-fallible-paths`]: https://doc.rust-lang.org/clippy/lint_configuration.html#test-without-fail-case-fallible-paths
6220+
[`test-without-fail-case-include-indexing-as-fallible`]: https://doc.rust-lang.org/clippy/lint_configuration.html#test-without-fail-case-include-indexing-as-fallible
6221+
[`test-without-fail-case-non-fallible-paths`]: https://doc.rust-lang.org/clippy/lint_configuration.html#test-without-fail-case-non-fallible-paths
62206222
[`too-large-for-stack`]: https://doc.rust-lang.org/clippy/lint_configuration.html#too-large-for-stack
62216223
[`too-many-arguments-threshold`]: https://doc.rust-lang.org/clippy/lint_configuration.html#too-many-arguments-threshold
62226224
[`too-many-lines-threshold`]: https://doc.rust-lang.org/clippy/lint_configuration.html#too-many-lines-threshold

book/src/lint_configuration.md

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -831,10 +831,50 @@ if no suggestion can be made.
831831
* [`indexing_slicing`](https://rust-lang.github.io/rust-clippy/master/index.html#indexing_slicing)
832832

833833

834-
## `test-without-fail-case`
835-
Lint tests to understand whether it can fail or not.
834+
## `test-without-fail-case-fallible-paths`
835+
List of full paths of macros and functions, that can fail. If a test, or a function
836+
that the test calls contains a call to any one of these, lint will mark the test fallible.
836837

837-
**Default Value:** `{ include_indexing_as_fallible = false, fallible_paths = ["core::panic", "core::assert", "core::assert_eq", "core::assert_ne"], non_fallible_paths = ["std::print", "std::println", "std::dbg", "std::eprint", "std::eprintln"] }`
838+
By default this macros are defined as `assert!`, `assert_eq!`, `panic!`.
839+
840+
**Default Value:** `["core::panic", "core::assert", "core::assert_eq", "core::assert_ne"]`
841+
842+
---
843+
**Affected lints:**
844+
* [`test_without_fail_case`](https://rust-lang.github.io/rust-clippy/master/index.html#test_without_fail_case)
845+
846+
847+
## `test-without-fail-case-include-indexing-as-fallible`
848+
Whether to consider indexing as a fallible operation while assesing if a test can fail.
849+
Indexing is fallible, and thus the a test that is doing that can fail but it is likely
850+
that tests that fail this way were not intended.
851+
852+
If set true, the lint will consider indexing into a slice a failable case
853+
and won't lint tests that has some sort of indexing. This analysis still done
854+
in a interprocedural manner. Meaning that any indexing opeartion done inside of
855+
a function that the test calls will still result the test getting marked fallible.
856+
857+
By default this is set to `false`. That is because from a usability perspective,
858+
indexing an array is not the intended way to fail a test. So setting this `true`
859+
reduces false positives but makes the analysis more focused on possible byproducts
860+
of a test. That is the set of operations to get the point we assert something rather
861+
than the existance of asserting that thing.
862+
863+
**Default Value:** `false`
864+
865+
---
866+
**Affected lints:**
867+
* [`test_without_fail_case`](https://rust-lang.github.io/rust-clippy/master/index.html#test_without_fail_case)
868+
869+
870+
## `test-without-fail-case-non-fallible-paths`
871+
List of full paths of macros and functions, that we want to mark as "not going to fail".
872+
This allows us to make the lint more focused on actual short comings of our test suite
873+
by marking common routines non-fallible, even though they are fallible.
874+
875+
By default this list contains: `println!`, `print!`, `dbg!`.
876+
877+
**Default Value:** `["std::print", "std::println", "std::dbg", "std::eprint", "std::eprintln"]`
838878

839879
---
840880
**Affected lints:**

clippy_config/src/conf.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -639,14 +639,8 @@ define_Conf! {
639639
///
640640
/// By default this macros are defined as `assert!`, `assert_eq!`, `panic!`.
641641
#[lints(test_without_fail_case)]
642-
test_without_fail_case_fallible_paths: Vec<String> = Vec::new(),
643-
/// List of full paths of macros and functions, that we want to mark as "not going to fail".
644-
/// This allows us to make the lint more focused on actual short comings of our test suite
645-
/// by marking common routines non-fallible, even though they are fallible.
646-
///
647-
/// By default this list contains: `println!`, `print!`, `dbg!`.
648-
#[lints(test_without_fail_case)]
649-
test_without_fail_case_non_fallible_paths: Vec<String> = Vec::new(),
642+
test_without_fail_case_fallible_paths: Vec<String> =
643+
DEFAULT_FALLIBLE_PATHS.iter().map(ToString::to_string).collect(),
650644
/// Whether to consider indexing as a fallible operation while assesing if a test can fail.
651645
/// Indexing is fallible, and thus the a test that is doing that can fail but it is likely
652646
/// that tests that fail this way were not intended.
@@ -663,6 +657,14 @@ define_Conf! {
663657
/// than the existance of asserting that thing.
664658
#[lints(test_without_fail_case)]
665659
test_without_fail_case_include_indexing_as_fallible: bool = false,
660+
/// List of full paths of macros and functions, that we want to mark as "not going to fail".
661+
/// This allows us to make the lint more focused on actual short comings of our test suite
662+
/// by marking common routines non-fallible, even though they are fallible.
663+
///
664+
/// By default this list contains: `println!`, `print!`, `dbg!`.
665+
#[lints(test_without_fail_case)]
666+
test_without_fail_case_non_fallible_paths: Vec<String> =
667+
DEFAULT_NONFALLIBLE_PATHS.iter().map(ToString::to_string).collect(),
666668
/// The maximum size of objects (in bytes) that will be linted. Larger objects are ok on the heap
667669
#[lints(boxed_local, useless_vec)]
668670
too_large_for_stack: u64 = 200,
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#[macro_export]
2+
macro_rules! fallible_macro {
3+
( $x:expr ) => {{
4+
let _ = $x;
5+
panic!("a");
6+
}};
7+
}
8+
9+
#[macro_export]
10+
macro_rules! non_fallible_macro {
11+
( $x:expr ) => {{
12+
let _ = $x;
13+
}};
14+
}

tests/ui-toml/test_without_fail_case/default/clippy.toml

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test-without-fail-case-fallible-paths = ["test_macro::non_fallible_macro"]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test-without-fail-case-include-indexing-as-fallible = true

tests/ui-toml/test_without_fail_case/indexing_fallible/clippy.toml

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/ui-toml/test_without_fail_case/macro_fallible/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+
test-without-fail-case-non-fallible-paths = ["test_macro::fallible_macro"]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
error: this function marked with `#[test]` cannot fail and will always succeed
2+
--> tests/ui-toml/test_without_fail_case/test_without_fail_case.rs:15:1
3+
|
4+
LL | / fn test_custom_macro_fallible() {
5+
LL | | println!("a")
6+
LL | | }
7+
| |_^
8+
|
9+
= note: consider adding assertions or panics to test failure cases
10+
= note: `-D clippy::test-without-fail-case` implied by `-D warnings`
11+
= help: to override `-D warnings` add `#[allow(clippy::test_without_fail_case)]`
12+
13+
error: this function marked with `#[test]` cannot fail and will always succeed
14+
--> tests/ui-toml/test_without_fail_case/test_without_fail_case.rs:21:1
15+
|
16+
LL | / fn test_indexing_fallible() {
17+
LL | | let a = [1, 2, 3];
18+
LL | | let _ = a[0];
19+
LL | | }
20+
| |_^
21+
|
22+
= note: consider adding assertions or panics to test failure cases
23+
24+
error: this function marked with `#[test]` cannot fail and will always succeed
25+
--> tests/ui-toml/test_without_fail_case/test_without_fail_case.rs:27:1
26+
|
27+
LL | / fn func_a() {
28+
LL | | let _ = 1;
29+
LL | | }
30+
| |_^
31+
|
32+
= note: consider adding assertions or panics to test failure cases
33+
34+
error: this function marked with `#[test]` cannot fail and will always succeed
35+
--> tests/ui-toml/test_without_fail_case/test_without_fail_case.rs:32:1
36+
|
37+
LL | / fn should_not_lint_if_defined_as_fallible() {
38+
LL | | non_fallible_macro!(1);
39+
LL | | }
40+
| |_^
41+
|
42+
= note: consider adding assertions or panics to test failure cases
43+
44+
error: aborting due to 4 previous errors
45+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
error: this function marked with `#[test]` cannot fail and will always succeed
2+
--> tests/ui-toml/test_without_fail_case/test_without_fail_case.rs:15:1
3+
|
4+
LL | / fn test_custom_macro_fallible() {
5+
LL | | println!("a")
6+
LL | | }
7+
| |_^
8+
|
9+
= note: consider adding assertions or panics to test failure cases
10+
= note: `-D clippy::test-without-fail-case` implied by `-D warnings`
11+
= help: to override `-D warnings` add `#[allow(clippy::test_without_fail_case)]`
12+
13+
error: this function marked with `#[test]` cannot fail and will always succeed
14+
--> tests/ui-toml/test_without_fail_case/test_without_fail_case.rs:21:1
15+
|
16+
LL | / fn test_indexing_fallible() {
17+
LL | | let a = [1, 2, 3];
18+
LL | | let _ = a[0];
19+
LL | | }
20+
| |_^
21+
|
22+
= note: consider adding assertions or panics to test failure cases
23+
24+
error: this function marked with `#[test]` cannot fail and will always succeed
25+
--> tests/ui-toml/test_without_fail_case/test_without_fail_case.rs:27:1
26+
|
27+
LL | / fn func_a() {
28+
LL | | let _ = 1;
29+
LL | | }
30+
| |_^
31+
|
32+
= note: consider adding assertions or panics to test failure cases
33+
34+
error: aborting due to 3 previous errors
35+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
error: this function marked with `#[test]` cannot fail and will always succeed
2+
--> tests/ui-toml/test_without_fail_case/test_without_fail_case.rs:15:1
3+
|
4+
LL | / fn test_custom_macro_fallible() {
5+
LL | | println!("a")
6+
LL | | }
7+
| |_^
8+
|
9+
= note: consider adding assertions or panics to test failure cases
10+
= note: `-D clippy::test-without-fail-case` implied by `-D warnings`
11+
= help: to override `-D warnings` add `#[allow(clippy::test_without_fail_case)]`
12+
13+
error: this function marked with `#[test]` cannot fail and will always succeed
14+
--> tests/ui-toml/test_without_fail_case/test_without_fail_case.rs:27:1
15+
|
16+
LL | / fn func_a() {
17+
LL | | let _ = 1;
18+
LL | | }
19+
| |_^
20+
|
21+
= note: consider adding assertions or panics to test failure cases
22+
23+
error: this function marked with `#[test]` cannot fail and will always succeed
24+
--> tests/ui-toml/test_without_fail_case/test_without_fail_case.rs:32:1
25+
|
26+
LL | / fn should_not_lint_if_defined_as_fallible() {
27+
LL | | non_fallible_macro!(1);
28+
LL | | }
29+
| |_^
30+
|
31+
= note: consider adding assertions or panics to test failure cases
32+
33+
error: aborting due to 3 previous errors
34+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
error: this function marked with `#[test]` cannot fail and will always succeed
2+
--> tests/ui-toml/test_without_fail_case/test_without_fail_case.rs:21:1
3+
|
4+
LL | / fn test_indexing_fallible() {
5+
LL | | let a = [1, 2, 3];
6+
LL | | let _ = a[0];
7+
LL | | }
8+
| |_^
9+
|
10+
= note: consider adding assertions or panics to test failure cases
11+
= note: `-D clippy::test-without-fail-case` implied by `-D warnings`
12+
= help: to override `-D warnings` add `#[allow(clippy::test_without_fail_case)]`
13+
14+
error: this function marked with `#[test]` cannot fail and will always succeed
15+
--> tests/ui-toml/test_without_fail_case/test_without_fail_case.rs:27:1
16+
|
17+
LL | / fn func_a() {
18+
LL | | let _ = 1;
19+
LL | | }
20+
| |_^
21+
|
22+
= note: consider adding assertions or panics to test failure cases
23+
24+
error: this function marked with `#[test]` cannot fail and will always succeed
25+
--> tests/ui-toml/test_without_fail_case/test_without_fail_case.rs:32:1
26+
|
27+
LL | / fn should_not_lint_if_defined_as_fallible() {
28+
LL | | non_fallible_macro!(1);
29+
LL | | }
30+
| |_^
31+
|
32+
= note: consider adding assertions or panics to test failure cases
33+
34+
error: this function marked with `#[test]` cannot fail and will always succeed
35+
--> tests/ui-toml/test_without_fail_case/test_without_fail_case.rs:37:1
36+
|
37+
LL | / fn should_lint_if_defined_as_non_fallible() {
38+
LL | | fallible_macro!(1);
39+
LL | | }
40+
| |_^
41+
|
42+
= note: consider adding assertions or panics to test failure cases
43+
44+
error: aborting due to 4 previous errors
45+
Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,41 @@
1+
//@aux-build:test_macro.rs
2+
//@compile-flags: --test
13
//@revisions: default fail_macro no_fail_macro index_fail
24
//@[default] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/test_without_fail_case/default
3-
//@[fail_macro] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/test_without_fail_case/macro_fallible
4-
//@[no_fail_macro] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/test_without_fail_case/macro_non_fallible
5-
//@[index_fail] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/test_without_fail_case/indexing_fallible
6-
5+
//@[fail_macro] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/test_without_fail_case/fail_macro
6+
//@[no_fail_macro] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/test_without_fail_case/no_fail_macro
7+
//@[index_fail] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/test_without_fail_case/index_fail
78
#![allow(unused)]
89
#![allow(clippy::tests_outside_test_module)]
9-
#![allow(clippy::unnecessary_literal_unwrap)]
1010
#![warn(clippy::test_without_fail_case)]
11-
12-
macro_rules! test {
13-
( $( $x:expr ),* ) => {{
14-
let _ = $x;
15-
panic!("a");
16-
}};
17-
}
11+
use test_macro::{fallible_macro, non_fallible_macro};
1812

1913
// Should not lint because of the clippy.toml file that adds `test` as fallible.
2014
#[test]
2115
fn test_custom_macro_fallible() {
22-
test![1];
16+
println!("a")
2317
}
2418

25-
// Should lint because of the clippy.toml file makes indexing fallible.
19+
// Should not lint unless the clippy.toml file makes indexing fallible.
2620
#[test]
2721
fn test_indexing_fallible() {
28-
let a = vec![1, 2, 3];
22+
let a = [1, 2, 3];
2923
let _ = a[0];
3024
}
3125

26+
#[test]
27+
fn func_a() {
28+
let _ = 1;
29+
}
30+
31+
#[test]
32+
fn should_not_lint_if_defined_as_fallible() {
33+
non_fallible_macro!(1);
34+
}
35+
36+
#[test]
37+
fn should_lint_if_defined_as_non_fallible() {
38+
fallible_macro!(1);
39+
}
40+
3241
fn main() {}

tests/ui/test_without_fail_case.stderr

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
error: this function marked with `#[test]` cannot fail and will always succeed
2+
--> tests/ui/test_without_fail_case.rs:23:1
3+
|
4+
LL | / fn test_without_fail() {
5+
LL | | let x = 5;
6+
LL | | let y = x + 2;
7+
LL | | println!("y: {}", y);
8+
LL | | }
9+
| |_^
10+
|
11+
= note: consider adding assertions or panics to test failure cases
12+
= note: `-D clippy::test-without-fail-case` implied by `-D warnings`
13+
= help: to override `-D warnings` add `#[allow(clippy::test_without_fail_case)]`
14+
115
error: this function marked with `#[test]` cannot fail and will always succeed
216
--> tests/ui/test_without_fail_case.rs:78:1
317
|
@@ -9,8 +23,6 @@ LL | | }
923
| |_^
1024
|
1125
= note: consider adding assertions or panics to test failure cases
12-
= note: `-D clippy::test-without-fail-case` implied by `-D warnings`
13-
= help: to override `-D warnings` add `#[allow(clippy::test_without_fail_case)]`
1426

15-
error: aborting due to 1 previous error
27+
error: aborting due to 2 previous errors
1628

0 commit comments

Comments
 (0)