Skip to content

Commit a4d8766

Browse files
committed
tests: add ui-toml test structure
1 parent 53ccd0e commit a4d8766

File tree

9 files changed

+88
-69
lines changed

9 files changed

+88
-69
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6216,6 +6216,7 @@ 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
62196220
[`too-large-for-stack`]: https://doc.rust-lang.org/clippy/lint_configuration.html#too-large-for-stack
62206221
[`too-many-arguments-threshold`]: https://doc.rust-lang.org/clippy/lint_configuration.html#too-many-arguments-threshold
62216222
[`too-many-lines-threshold`]: https://doc.rust-lang.org/clippy/lint_configuration.html#too-many-lines-threshold

book/src/lint_configuration.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,16 @@ 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.
836+
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+
839+
---
840+
**Affected lints:**
841+
* [`test_without_fail_case`](https://rust-lang.github.io/rust-clippy/master/index.html#test_without_fail_case)
842+
843+
834844
## `too-large-for-stack`
835845
The maximum size of objects (in bytes) that will be linted. Larger objects are ok on the heap
836846

clippy_config/src/conf.rs

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
use crate::ClippyConfiguration;
22
use crate::msrvs::Msrv;
3-
use crate::types::{
4-
DEFAULT_FALLIBLE_PATHS, DEFAULT_NONFALLIBLE_PATHS, DisallowedPath, MacroMatcher, MatchLintBehaviour,
5-
PubUnderscoreFieldsBehaviour, Rename, TestWithoutFailCaseBehaviour,
6-
};
3+
use crate::types::{DisallowedPath, MacroMatcher, MatchLintBehaviour, PubUnderscoreFieldsBehaviour, Rename};
74
use rustc_errors::Applicability;
85
use rustc_session::Session;
96
use rustc_span::edit_distance::edit_distance;
@@ -49,6 +46,12 @@ const DEFAULT_ALLOWED_IDENTS_BELOW_MIN_CHARS: &[&str] = &["i", "j", "x", "y", "z
4946
const DEFAULT_ALLOWED_PREFIXES: &[&str] = &["to", "as", "into", "from", "try_into", "try_from"];
5047
const DEFAULT_ALLOWED_TRAITS_WITH_RENAMED_PARAMS: &[&str] =
5148
&["core::convert::From", "core::convert::TryFrom", "core::str::FromStr"];
49+
/// Default paths considered as fallible for `test_without_fail_case` lint.
50+
pub(crate) const DEFAULT_FALLIBLE_PATHS: &[&str] =
51+
&["core::panic", "core::assert", "core::assert_eq", "core::assert_ne"];
52+
/// Default paths considered as non-fallible for `test_without_fail_case` lint.
53+
pub(crate) const DEFAULT_NONFALLIBLE_PATHS: &[&str] =
54+
&["std::print", "std::println", "std::dbg", "std::eprint", "std::eprintln"];
5255

5356
/// Conf with parse errors
5457
#[derive(Default)]
@@ -631,9 +634,35 @@ define_Conf! {
631634
/// if no suggestion can be made.
632635
#[lints(indexing_slicing)]
633636
suppress_restriction_lint_in_const: bool = false,
634-
/// Lint tests to understand whether it can fail or not.
637+
/// List of full paths of macros and functions, that can fail. If a test, or a function
638+
/// that the test calls contains a call to any one of these, lint will mark the test fallible.
639+
///
640+
/// By default this macros are defined as `assert!`, `assert_eq!`, `panic!`.
641+
#[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(),
650+
/// Whether to consider indexing as a fallible operation while assesing if a test can fail.
651+
/// Indexing is fallible, and thus the a test that is doing that can fail but it is likely
652+
/// that tests that fail this way were not intended.
653+
///
654+
/// If set true, the lint will consider indexing into a slice a failable case
655+
/// and won't lint tests that has some sort of indexing. This analysis still done
656+
/// in a interprocedural manner. Meaning that any indexing opeartion done inside of
657+
/// a function that the test calls will still result the test getting marked fallible.
658+
///
659+
/// By default this is set to `false`. That is because from a usability perspective,
660+
/// indexing an array is not the intended way to fail a test. So setting this `true`
661+
/// reduces false positives but makes the analysis more focused on possible byproducts
662+
/// of a test. That is the set of operations to get the point we assert something rather
663+
/// than the existance of asserting that thing.
635664
#[lints(test_without_fail_case)]
636-
test_without_fail_case: TestWithoutFailCaseBehaviour = TestWithoutFailCaseBehaviour::default(),
665+
test_without_fail_case_include_indexing_as_fallible: bool = false,
637666
/// The maximum size of objects (in bytes) that will be linted. Larger objects are ok on the heap
638667
#[lints(boxed_local, useless_vec)]
639668
too_large_for_stack: u64 = 200,
@@ -731,11 +760,11 @@ fn deserialize(file: &SourceFile) -> TryConf {
731760
match toml::de::Deserializer::new(file.src.as_ref().unwrap()).deserialize_map(ConfVisitor(file)) {
732761
Ok(mut conf) => {
733762
extend_vec_if_indicator_present(
734-
&mut conf.conf.test_without_fail_case.fallible_paths,
763+
&mut conf.conf.test_without_fail_case_fallible_paths,
735764
DEFAULT_FALLIBLE_PATHS,
736765
);
737766
extend_vec_if_indicator_present(
738-
&mut conf.conf.test_without_fail_case.non_fallible_paths,
767+
&mut conf.conf.test_without_fail_case_non_fallible_paths,
739768
DEFAULT_NONFALLIBLE_PATHS,
740769
);
741770
extend_vec_if_indicator_present(&mut conf.conf.disallowed_names, DEFAULT_DISALLOWED_NAMES);

clippy_config/src/types.rs

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -130,46 +130,3 @@ pub enum PubUnderscoreFieldsBehaviour {
130130
PubliclyExported,
131131
AllPubFields,
132132
}
133-
134-
/// Default paths considered as fallible for `test_without_fail_case` lint.
135-
pub(crate) const DEFAULT_FALLIBLE_PATHS: &[&str] =
136-
&["core::panic", "core::assert", "core::assert_eq", "core::assert_ne"];
137-
/// Default paths considered as non-fallible for `test_without_fail_case` lint.
138-
pub(crate) const DEFAULT_NONFALLIBLE_PATHS: &[&str] =
139-
&["std::print", "std::println", "std::dbg", "std::eprint", "std::eprintln"];
140-
141-
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
142-
pub struct TestWithoutFailCaseBehaviour {
143-
/// Indexing is fallible, and thus the a test that is doing that can fail.
144-
///
145-
/// If set true, the lint consider indexing into a slice a failable case
146-
/// and won't lint tests that has some sort of indexing. This analysis still done
147-
/// in a interfunctional manner. Meaning that any indexing opeartion done inside of
148-
/// a function that the test calls will still mark the test fallible.
149-
///
150-
/// By default this is set to `false`. That is because from a usability perspective,
151-
/// indexing an array is not the intended way to fail a test. So setting this `true`
152-
/// reduces false positives but makes the analysis more focused on possible byproducts
153-
/// of a test. That is the set of operations to get the point we assert something rather
154-
/// than the existance of asserting that thing.
155-
pub include_indexing: bool,
156-
/// List of full paths of macros and functions, that can fail. If a test, or a function
157-
/// that the test calls contains any one of these macros, we will mark the test fallible.
158-
/// By default this macros are defined as `assert!`, `assert_eq!`, `panic!`.
159-
pub fallible_paths: Vec<String>,
160-
/// List of full paths of macros and functions, that we want to mark as not going to fail.
161-
/// This allows us to make the lint more focused on actual short comings of our test suite
162-
/// by marking common routines non-fallible, even though they are fallible.
163-
/// By default this list contains: `println!`, `print!`, `dbg!`.
164-
pub non_fallible_paths: Vec<String>,
165-
}
166-
167-
impl Default for TestWithoutFailCaseBehaviour {
168-
fn default() -> Self {
169-
Self {
170-
include_indexing: false,
171-
fallible_paths: DEFAULT_FALLIBLE_PATHS.iter().map(|s| s.to_string()).collect(),
172-
non_fallible_paths: DEFAULT_NONFALLIBLE_PATHS.iter().map(|a| a.to_string()).collect(),
173-
}
174-
}
175-
}

clippy_lints/src/test_without_fail_case.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ impl TestWithoutFailCase {
5858
pub fn new(conf: &Conf) -> Self {
5959
Self {
6060
config: SearchConfig {
61-
indexing_fallible: conf.test_without_fail_case.include_indexing,
62-
fallible_paths: conf.test_without_fail_case.fallible_paths.iter().cloned().collect(),
63-
non_fallible_paths: conf.test_without_fail_case.non_fallible_paths.iter().cloned().collect(),
61+
indexing_fallible: conf.test_without_fail_case_include_indexing_as_fallible,
62+
fallible_paths: conf.test_without_fail_case_fallible_paths.iter().cloned().collect(),
63+
non_fallible_paths: conf.test_without_fail_case_non_fallible_paths.iter().cloned().collect(),
6464
},
6565
}
6666
}
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 = false
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test-without-fail-case-fallible-paths = ["vec"]
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//@revisions: default fail_macro no_fail_macro index_fail
2+
//@[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+
7+
#![allow(unused)]
8+
#![allow(clippy::tests_outside_test_module)]
9+
#![allow(clippy::unnecessary_literal_unwrap)]
10+
#![warn(clippy::test_without_fail_case)]
11+
12+
macro_rules! test {
13+
( $( $x:expr ),* ) => {{
14+
let _ = $x;
15+
panic!("a");
16+
}};
17+
}
18+
19+
// Should not lint because of the clippy.toml file that adds `test` as fallible.
20+
#[test]
21+
fn test_custom_macro_fallible() {
22+
test![1];
23+
}
24+
25+
// Should lint because of the clippy.toml file makes indexing fallible.
26+
#[test]
27+
fn test_indexing_fallible() {
28+
let a = vec![1, 2, 3];
29+
let _ = a[0];
30+
}
31+
32+
fn main() {}
Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,3 @@
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-
151
error: this function marked with `#[test]` cannot fail and will always succeed
162
--> tests/ui/test_without_fail_case.rs:78:1
173
|
@@ -23,6 +9,8 @@ LL | | }
239
| |_^
2410
|
2511
= 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)]`
2614

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

0 commit comments

Comments
 (0)