|
1 | 1 | use crate::ClippyConfiguration; |
2 | 2 | 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}; |
7 | 4 | use rustc_errors::Applicability; |
8 | 5 | use rustc_session::Session; |
9 | 6 | use rustc_span::edit_distance::edit_distance; |
@@ -49,6 +46,12 @@ const DEFAULT_ALLOWED_IDENTS_BELOW_MIN_CHARS: &[&str] = &["i", "j", "x", "y", "z |
49 | 46 | const DEFAULT_ALLOWED_PREFIXES: &[&str] = &["to", "as", "into", "from", "try_into", "try_from"]; |
50 | 47 | const DEFAULT_ALLOWED_TRAITS_WITH_RENAMED_PARAMS: &[&str] = |
51 | 48 | &["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"]; |
52 | 55 |
|
53 | 56 | /// Conf with parse errors |
54 | 57 | #[derive(Default)] |
@@ -631,9 +634,35 @@ define_Conf! { |
631 | 634 | /// if no suggestion can be made. |
632 | 635 | #[lints(indexing_slicing)] |
633 | 636 | 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. |
635 | 664 | #[lints(test_without_fail_case)] |
636 | | - test_without_fail_case: TestWithoutFailCaseBehaviour = TestWithoutFailCaseBehaviour::default(), |
| 665 | + test_without_fail_case_include_indexing_as_fallible: bool = false, |
637 | 666 | /// The maximum size of objects (in bytes) that will be linted. Larger objects are ok on the heap |
638 | 667 | #[lints(boxed_local, useless_vec)] |
639 | 668 | too_large_for_stack: u64 = 200, |
@@ -731,11 +760,11 @@ fn deserialize(file: &SourceFile) -> TryConf { |
731 | 760 | match toml::de::Deserializer::new(file.src.as_ref().unwrap()).deserialize_map(ConfVisitor(file)) { |
732 | 761 | Ok(mut conf) => { |
733 | 762 | 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, |
735 | 764 | DEFAULT_FALLIBLE_PATHS, |
736 | 765 | ); |
737 | 766 | 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, |
739 | 768 | DEFAULT_NONFALLIBLE_PATHS, |
740 | 769 | ); |
741 | 770 | extend_vec_if_indicator_present(&mut conf.conf.disallowed_names, DEFAULT_DISALLOWED_NAMES); |
|
0 commit comments