Skip to content

Commit 3a1f8cd

Browse files
committed
Auto merge of #10391 - ldm0:ldm0_fix_unwrap_in_tests, r=xFrednet
Fix test function checker in `unwrap_used`, `expect_used` After #9686 , `unwrap` and `expect` in integration tests and raw test functions won't be allowed. fixes #10011 fixes #10238 fixes #10264 --- changelog: Fix: [`expect_used`], [`unwrap_used`], [`dbg_macro`], [`print_stdout`], [`print_stderr`]: No longer lint in test functions, if the related configuration is set [#10391](#10391) <!-- changelog_checked -->
2 parents 659112c + 79a9024 commit 3a1f8cd

File tree

6 files changed

+34
-10
lines changed

6 files changed

+34
-10
lines changed

clippy_lints/src/methods/expect_used.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
2-
use clippy_utils::is_in_cfg_test;
32
use clippy_utils::ty::is_type_diagnostic_item;
3+
use clippy_utils::{is_in_cfg_test, is_in_test_function};
44
use rustc_hir as hir;
55
use rustc_lint::LateContext;
66
use rustc_span::sym;
@@ -27,7 +27,7 @@ pub(super) fn check(
2727

2828
let method = if is_err { "expect_err" } else { "expect" };
2929

30-
if allow_expect_in_tests && is_in_cfg_test(cx.tcx, expr.hir_id) {
30+
if allow_expect_in_tests && (is_in_test_function(cx.tcx, expr.hir_id) || is_in_cfg_test(cx.tcx, expr.hir_id)) {
3131
return;
3232
}
3333

clippy_lints/src/methods/unwrap_used.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
22
use clippy_utils::ty::is_type_diagnostic_item;
3-
use clippy_utils::{is_in_cfg_test, is_lint_allowed};
3+
use clippy_utils::{is_in_cfg_test, is_in_test_function, is_lint_allowed};
44
use rustc_hir as hir;
55
use rustc_lint::LateContext;
66
use rustc_span::sym;
@@ -27,7 +27,7 @@ pub(super) fn check(
2727

2828
let method_suffix = if is_err { "_err" } else { "" };
2929

30-
if allow_unwrap_in_tests && is_in_cfg_test(cx.tcx, expr.hir_id) {
30+
if allow_unwrap_in_tests && (is_in_test_function(cx.tcx, expr.hir_id) || is_in_cfg_test(cx.tcx, expr.hir_id)) {
3131
return;
3232
}
3333

clippy_lints/src/utils/conf.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -419,19 +419,19 @@ define_Conf! {
419419
(max_include_file_size: u64 = 1_000_000),
420420
/// Lint: EXPECT_USED.
421421
///
422-
/// Whether `expect` should be allowed within `#[cfg(test)]`
422+
/// Whether `expect` should be allowed in test functions or `#[cfg(test)]`
423423
(allow_expect_in_tests: bool = false),
424424
/// Lint: UNWRAP_USED.
425425
///
426-
/// Whether `unwrap` should be allowed in test cfg
426+
/// Whether `unwrap` should be allowed in test functions or `#[cfg(test)]`
427427
(allow_unwrap_in_tests: bool = false),
428428
/// Lint: DBG_MACRO.
429429
///
430-
/// Whether `dbg!` should be allowed in test functions
430+
/// Whether `dbg!` should be allowed in test functions or `#[cfg(test)]`
431431
(allow_dbg_in_tests: bool = false),
432432
/// Lint: PRINT_STDOUT, PRINT_STDERR.
433433
///
434-
/// Whether print macros (ex. `println!`) should be allowed in test functions
434+
/// Whether print macros (ex. `println!`) should be allowed in test functions or `#[cfg(test)]`
435435
(allow_print_in_tests: bool = false),
436436
/// Lint: RESULT_LARGE_ERR.
437437
///

tests/ui-toml/expect_used/expect_used.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ fn main() {
1616
expect_result();
1717
}
1818

19+
#[test]
20+
fn test_expect_option() {
21+
let opt = Some(0);
22+
let _ = opt.expect("");
23+
}
24+
25+
#[test]
26+
fn test_expect_result() {
27+
let res: Result<u8, ()> = Ok(0);
28+
let _ = res.expect("");
29+
}
30+
1931
#[cfg(test)]
2032
mod issue9612 {
2133
// should not lint in `#[cfg(test)]` modules

tests/ui-toml/unwrap_used/unwrap_used.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ fn main() {
6666
}
6767
}
6868

69+
#[test]
70+
fn test() {
71+
let boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]);
72+
let _ = boxed_slice.get(1).unwrap();
73+
}
74+
6975
#[cfg(test)]
7076
mod issue9612 {
7177
// should not lint in `#[cfg(test)]` modules

tests/ui-toml/unwrap_used/unwrap_used.stderr

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,16 @@ LL | let _ = some_vec.get_mut(0..1).unwrap().to_vec();
188188
= help: if you don't want to handle the `None` case gracefully, consider using `expect()` to provide a better panic message
189189

190190
error: called `.get().unwrap()` on a slice. Using `[]` is more clear and more concise
191-
--> $DIR/unwrap_used.rs:84:17
191+
--> $DIR/unwrap_used.rs:72:13
192+
|
193+
LL | let _ = boxed_slice.get(1).unwrap();
194+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&boxed_slice[1]`
195+
196+
error: called `.get().unwrap()` on a slice. Using `[]` is more clear and more concise
197+
--> $DIR/unwrap_used.rs:90:17
192198
|
193199
LL | let _ = Box::new([0]).get(1).unwrap();
194200
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&Box::new([0])[1]`
195201

196-
error: aborting due to 27 previous errors
202+
error: aborting due to 28 previous errors
197203

0 commit comments

Comments
 (0)