Skip to content

Commit b528cc9

Browse files
committed
Auto merge of rust-lang#10391 - ldm0:ldm0_fix_unwrap_in_tests, r=xFrednet
Fix test function checker in `unwrap_used`, `expect_used` After rust-lang#9686 , `unwrap` and `expect` in integration tests and raw test functions won't be allowed. fixes rust-lang#10011 fixes rust-lang#10238 fixes rust-lang#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 [rust-lang#10391](rust-lang/rust-clippy#10391) <!-- changelog_checked -->
2 parents 659112c + 84ceca8 commit b528cc9

File tree

7 files changed

+38
-14
lines changed

7 files changed

+38
-14
lines changed

book/src/lint_configuration.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -472,31 +472,31 @@ The maximum size of a file included via `include_bytes!()` or `include_str!()`,
472472

473473

474474
### allow-expect-in-tests
475-
Whether `expect` should be allowed within `#[cfg(test)]`
475+
Whether `expect` should be allowed in test functions or `#[cfg(test)]`
476476

477477
**Default Value:** `false` (`bool`)
478478

479479
* [expect_used](https://rust-lang.github.io/rust-clippy/master/index.html#expect_used)
480480

481481

482482
### allow-unwrap-in-tests
483-
Whether `unwrap` should be allowed in test cfg
483+
Whether `unwrap` should be allowed in test functions or `#[cfg(test)]`
484484

485485
**Default Value:** `false` (`bool`)
486486

487487
* [unwrap_used](https://rust-lang.github.io/rust-clippy/master/index.html#unwrap_used)
488488

489489

490490
### allow-dbg-in-tests
491-
Whether `dbg!` should be allowed in test functions
491+
Whether `dbg!` should be allowed in test functions or `#[cfg(test)]`
492492

493493
**Default Value:** `false` (`bool`)
494494

495495
* [dbg_macro](https://rust-lang.github.io/rust-clippy/master/index.html#dbg_macro)
496496

497497

498498
### allow-print-in-tests
499-
Whether print macros (ex. `println!`) should be allowed in test functions
499+
Whether print macros (ex. `println!`) should be allowed in test functions or `#[cfg(test)]`
500500

501501
**Default Value:** `false` (`bool`)
502502

clippy_lints/src/methods/expect_used.rs

+2-2
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

+2-2
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

+4-4
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

+12
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

+6
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

+8-2
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)