Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a8a8fd7

Browse files
committedOct 23, 2022
Auto merge of rust-lang#9686 - kraktus:unwrap_in_test_cfg, r=flip1995
[`unwrap_used`], [`expect_used`] do not lint in `test` cfg changelog: [`unwrap_used`], [`expect_used`] do not lint in `test` cfg fix rust-lang/rust-clippy#9612 I've updated the doc and used `cfg` acronym, not sure if `conditional compiler flag` would have been better
2 parents 4f142aa + a0c82d2 commit a8a8fd7

File tree

6 files changed

+40
-22
lines changed

6 files changed

+40
-22
lines changed
 

‎clippy_lints/src/methods/expect_used.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
2-
use clippy_utils::is_in_test_function;
2+
use clippy_utils::is_in_cfg_test;
33
use clippy_utils::ty::is_type_diagnostic_item;
44
use rustc_hir as hir;
55
use rustc_lint::LateContext;
@@ -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_test_function(cx.tcx, expr.hir_id) {
30+
if allow_expect_in_tests && 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_test_function, is_lint_allowed};
3+
use clippy_utils::{is_in_cfg_test, 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_test_function(cx.tcx, expr.hir_id) {
30+
if allow_unwrap_in_tests && is_in_cfg_test(cx.tcx, expr.hir_id) {
3131
return;
3232
}
3333

‎clippy_lints/src/utils/conf.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -373,11 +373,11 @@ define_Conf! {
373373
(max_include_file_size: u64 = 1_000_000),
374374
/// Lint: EXPECT_USED.
375375
///
376-
/// Whether `expect` should be allowed in test functions
376+
/// Whether `expect` should be allowed within `#[cfg(test)]`
377377
(allow_expect_in_tests: bool = false),
378378
/// Lint: UNWRAP_USED.
379379
///
380-
/// Whether `unwrap` should be allowed in test functions
380+
/// Whether `unwrap` should be allowed in test cfg
381381
(allow_unwrap_in_tests: bool = false),
382382
/// Lint: DBG_MACRO.
383383
///

‎tests/ui-toml/expect_used/expect_used.rs

+14-9
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,19 @@ fn main() {
1616
expect_result();
1717
}
1818

19-
#[test]
20-
fn test_expect_option() {
21-
let opt = Some(0);
22-
let _ = opt.expect("");
23-
}
19+
#[cfg(test)]
20+
mod issue9612 {
21+
// should not lint in `#[cfg(test)]` modules
22+
#[test]
23+
fn test_fn() {
24+
let _a: u8 = 2.try_into().unwrap();
25+
let _a: u8 = 3.try_into().expect("");
2426

25-
#[test]
26-
fn test_expect_result() {
27-
let res: Result<u8, ()> = Ok(0);
28-
let _ = res.expect("");
27+
util();
28+
}
29+
30+
fn util() {
31+
let _a: u8 = 4.try_into().unwrap();
32+
let _a: u8 = 5.try_into().expect("");
33+
}
2934
}

‎tests/ui-toml/unwrap_used/unwrap_used.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,21 @@ 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();
69+
#[cfg(test)]
70+
mod issue9612 {
71+
// should not lint in `#[cfg(test)]` modules
72+
#[test]
73+
fn test_fn() {
74+
let _a: u8 = 2.try_into().unwrap();
75+
let _a: u8 = 3.try_into().expect("");
76+
77+
util();
78+
}
79+
80+
fn util() {
81+
let _a: u8 = 4.try_into().unwrap();
82+
let _a: u8 = 5.try_into().expect("");
83+
// should still warn
84+
let _ = Box::new([0]).get(1).unwrap();
85+
}
7386
}

‎tests/ui-toml/unwrap_used/unwrap_used.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,10 @@ 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:72:13
191+
--> $DIR/unwrap_used.rs:84:17
192192
|
193-
LL | let _ = boxed_slice.get(1).unwrap();
194-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&boxed_slice[1]`
193+
LL | let _ = Box::new([0]).get(1).unwrap();
194+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&Box::new([0])[1]`
195195

196196
error: aborting due to 27 previous errors
197197

0 commit comments

Comments
 (0)
Please sign in to comment.