Skip to content

Commit 53ccd0e

Browse files
committed
feat: add indexing analysis as well
1 parent bfb0666 commit 53ccd0e

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

clippy_config/src/types.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -140,18 +140,19 @@ pub(crate) const DEFAULT_NONFALLIBLE_PATHS: &[&str] =
140140

141141
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
142142
pub struct TestWithoutFailCaseBehaviour {
143-
/// Indexing into a slice is fallible, and thus the a test that is doing that
144-
/// can fail. If set true, the lint consider indexing into a slice a failable case
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
145146
/// and won't lint tests that has some sort of indexing. This analysis still done
146147
/// in a interfunctional manner. Meaning that any indexing opeartion done inside of
147148
/// a function that the test calls will still mark the test fallible.
148149
///
149-
/// By default this is set to `false`. That is becuase from a usability perspective,
150+
/// By default this is set to `false`. That is because from a usability perspective,
150151
/// indexing an array is not the intended way to fail a test. So setting this `true`
151152
/// reduces false positives but makes the analysis more focused on possible byproducts
152153
/// of a test. That is the set of operations to get the point we assert something rather
153154
/// than the existance of asserting that thing.
154-
pub include_slice_indexing: bool,
155+
pub include_indexing: bool,
155156
/// List of full paths of macros and functions, that can fail. If a test, or a function
156157
/// that the test calls contains any one of these macros, we will mark the test fallible.
157158
/// By default this macros are defined as `assert!`, `assert_eq!`, `panic!`.
@@ -163,20 +164,10 @@ pub struct TestWithoutFailCaseBehaviour {
163164
pub non_fallible_paths: Vec<String>,
164165
}
165166

166-
impl TestWithoutFailCaseBehaviour {
167-
pub fn new(include_slice_indexing: bool, fallible_paths: Vec<String>, non_fallible_paths: Vec<String>) -> Self {
168-
Self {
169-
include_slice_indexing,
170-
fallible_paths,
171-
non_fallible_paths,
172-
}
173-
}
174-
}
175-
176167
impl Default for TestWithoutFailCaseBehaviour {
177168
fn default() -> Self {
178169
Self {
179-
include_slice_indexing: false,
170+
include_indexing: false,
180171
fallible_paths: DEFAULT_FALLIBLE_PATHS.iter().map(|s| s.to_string()).collect(),
181172
non_fallible_paths: DEFAULT_NONFALLIBLE_PATHS.iter().map(|a| a.to_string()).collect(),
182173
}

clippy_lints/src/test_without_fail_case.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl TestWithoutFailCase {
5858
pub fn new(conf: &Conf) -> Self {
5959
Self {
6060
config: SearchConfig {
61-
indexing_into_slice_fallible: conf.test_without_fail_case.include_slice_indexing,
61+
indexing_fallible: conf.test_without_fail_case.include_indexing,
6262
fallible_paths: conf.test_without_fail_case.fallible_paths.iter().cloned().collect(),
6363
non_fallible_paths: conf.test_without_fail_case.non_fallible_paths.iter().cloned().collect(),
6464
},
@@ -95,8 +95,8 @@ impl<'tcx> LateLintPass<'tcx> for TestWithoutFailCase {
9595
/// Set of options that user provivdes through configs, to modify the lint behaviour
9696
/// according to their repo.
9797
struct SearchConfig {
98-
/// If search should consider indexing into slice as fallible.
99-
indexing_into_slice_fallible: bool,
98+
/// If search should consider indexing as fallible.
99+
indexing_fallible: bool,
100100
/// Set of paths that are marked as fallible.
101101
fallible_paths: FxHashSet<String>,
102102
/// Set of paths that are marked as non fallible.
@@ -187,6 +187,15 @@ impl<'tcx> Visitor<'tcx> for SearchFailIntraFunction<'_, 'tcx> {
187187
}
188188

189189
match expr.kind {
190+
ExprKind::Index(slice_expr, index_expr, _) => {
191+
// If indexing into slices is considered fallible, we treat it as a potential failure point
192+
if self.search_config.indexing_fallible {
193+
self.fail_found = true;
194+
return;
195+
}
196+
self.visit_expr(slice_expr);
197+
self.visit_expr(index_expr);
198+
},
190199
ExprKind::Call(callee, args) => {
191200
if let ExprKind::Path(ref qpath) = callee.kind {
192201
if let Res::Def(_, def_id) = self.cx.qpath_res(qpath, callee.hir_id) {

0 commit comments

Comments
 (0)