Skip to content

Commit 4667198

Browse files
committed
Auto merge of #8802 - smoelius:allow-expect-unwrap-in-tests, r=llogiq
Optionally allow `expect` and `unwrap` in tests This addresses #1015, except it makes the new behavior optional. The reason for the msrv-related changes is as follows. Rather than expand `check_methods` list of arguments, it seemed easier to make `check_methods` a method of `Methods`, so that `check_methods` could access `Methods`' fields. `check_methods` had an `msrv` parameter, which I consequently made a field of `Methods`. But, to avoid adding a lifetime parameter to `Methods`, I made the field type `Option<RustcVersion>` instead of the parameter's existing type, `Option<&RustcVersion>`. This seemed sensible since `RustcVersion` implements `Copy`. But this broke a lot of code that expected an `Option<&RustcVersion>` or `&Option<RustcVersion>`. I changed all of those occurrences to `Option<RustcVersion>`. IMHO, the code is better as a result of these changes, though. The msrv-related changes are in their own commit to (hopefully) ease review. Closes #1015 changelog: optionally allow `expect` and `unwrap` in tests r? `@llogiq`
2 parents 9c78883 + 597f61b commit 4667198

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+610
-250
lines changed

clippy_lints/src/approx_const.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,7 @@ impl ApproxConstant {
8787
let s = s.as_str();
8888
if s.parse::<f64>().is_ok() {
8989
for &(constant, name, min_digits, msrv) in &KNOWN_CONSTS {
90-
if is_approx_const(constant, s, min_digits)
91-
&& msrv.as_ref().map_or(true, |msrv| meets_msrv(self.msrv.as_ref(), msrv))
92-
{
90+
if is_approx_const(constant, s, min_digits) && msrv.map_or(true, |msrv| meets_msrv(self.msrv, msrv)) {
9391
span_lint_and_help(
9492
cx,
9593
APPROX_CONSTANT,

clippy_lints/src/attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ fn check_empty_line_after_outer_attr(cx: &EarlyContext<'_>, item: &rustc_ast::It
613613

614614
fn check_deprecated_cfg_attr(cx: &EarlyContext<'_>, attr: &Attribute, msrv: Option<RustcVersion>) {
615615
if_chain! {
616-
if meets_msrv(msrv.as_ref(), &msrvs::TOOL_ATTRIBUTES);
616+
if meets_msrv(msrv, msrvs::TOOL_ATTRIBUTES);
617617
// check cfg_attr
618618
if attr.has_name(sym::cfg_attr);
619619
if let Some(items) = attr.meta_item_list();

clippy_lints/src/borrow_as_ptr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl BorrowAsPtr {
5757

5858
impl<'tcx> LateLintPass<'tcx> for BorrowAsPtr {
5959
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
60-
if !meets_msrv(self.msrv.as_ref(), &msrvs::BORROW_AS_PTR) {
60+
if !meets_msrv(self.msrv, msrvs::BORROW_AS_PTR) {
6161
return;
6262
}
6363

clippy_lints/src/casts/cast_abs_to_unsigned.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ pub(super) fn check(
1616
cast_expr: &Expr<'_>,
1717
cast_from: Ty<'_>,
1818
cast_to: Ty<'_>,
19-
msrv: &Option<RustcVersion>,
19+
msrv: Option<RustcVersion>,
2020
) {
2121
if_chain! {
22-
if meets_msrv(msrv.as_ref(), &msrvs::UNSIGNED_ABS);
22+
if meets_msrv(msrv, msrvs::UNSIGNED_ABS);
2323
if cast_from.is_integral();
2424
if cast_to.is_integral();
2525
if cast_from.is_signed();

clippy_lints/src/casts/cast_lossless.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub(super) fn check(
1616
cast_op: &Expr<'_>,
1717
cast_from: Ty<'_>,
1818
cast_to: Ty<'_>,
19-
msrv: &Option<RustcVersion>,
19+
msrv: Option<RustcVersion>,
2020
) {
2121
if !should_lint(cx, expr, cast_from, cast_to, msrv) {
2222
return;
@@ -68,7 +68,7 @@ fn should_lint(
6868
expr: &Expr<'_>,
6969
cast_from: Ty<'_>,
7070
cast_to: Ty<'_>,
71-
msrv: &Option<RustcVersion>,
71+
msrv: Option<RustcVersion>,
7272
) -> bool {
7373
// Do not suggest using From in consts/statics until it is valid to do so (see #2267).
7474
if in_constant(cx, expr.hir_id) {
@@ -95,7 +95,7 @@ fn should_lint(
9595
};
9696
!is_isize_or_usize(cast_from) && from_nbits < to_nbits
9797
},
98-
(false, true) if matches!(cast_from.kind(), ty::Bool) && meets_msrv(msrv.as_ref(), &msrvs::FROM_BOOL) => true,
98+
(false, true) if matches!(cast_from.kind(), ty::Bool) && meets_msrv(msrv, msrvs::FROM_BOOL) => true,
9999
(_, _) => {
100100
matches!(cast_from.kind(), ty::Float(FloatTy::F32)) && matches!(cast_to.kind(), ty::Float(FloatTy::F64))
101101
},

clippy_lints/src/casts/cast_slice_different_sizes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ use rustc_semver::RustcVersion;
88

99
use super::CAST_SLICE_DIFFERENT_SIZES;
1010

11-
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, msrv: &Option<RustcVersion>) {
11+
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, msrv: Option<RustcVersion>) {
1212
// suggestion is invalid if `ptr::slice_from_raw_parts` does not exist
13-
if !meets_msrv(msrv.as_ref(), &msrvs::PTR_SLICE_RAW_PARTS) {
13+
if !meets_msrv(msrv, msrvs::PTR_SLICE_RAW_PARTS) {
1414
return;
1515
}
1616

clippy_lints/src/casts/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ impl_lint_pass!(Casts => [
532532
impl<'tcx> LateLintPass<'tcx> for Casts {
533533
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
534534
if !in_external_macro(cx.sess(), expr.span) {
535-
ptr_as_ptr::check(cx, expr, &self.msrv);
535+
ptr_as_ptr::check(cx, expr, self.msrv);
536536
}
537537

538538
if expr.span.from_expansion() {
@@ -562,18 +562,18 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
562562
cast_possible_wrap::check(cx, expr, cast_from, cast_to);
563563
cast_precision_loss::check(cx, expr, cast_from, cast_to);
564564
cast_sign_loss::check(cx, expr, cast_expr, cast_from, cast_to);
565-
cast_abs_to_unsigned::check(cx, expr, cast_expr, cast_from, cast_to, &self.msrv);
565+
cast_abs_to_unsigned::check(cx, expr, cast_expr, cast_from, cast_to, self.msrv);
566566
}
567-
cast_lossless::check(cx, expr, cast_expr, cast_from, cast_to, &self.msrv);
567+
cast_lossless::check(cx, expr, cast_expr, cast_from, cast_to, self.msrv);
568568
cast_enum_constructor::check(cx, expr, cast_expr, cast_from);
569569
}
570570
}
571571

572572
cast_ref_to_mut::check(cx, expr);
573573
cast_ptr_alignment::check(cx, expr);
574574
char_lit_as_u8::check(cx, expr);
575-
ptr_as_ptr::check(cx, expr, &self.msrv);
576-
cast_slice_different_sizes::check(cx, expr, &self.msrv);
575+
ptr_as_ptr::check(cx, expr, self.msrv);
576+
cast_slice_different_sizes::check(cx, expr, self.msrv);
577577
}
578578

579579
extract_msrv_attr!(LateContext);

clippy_lints/src/casts/ptr_as_ptr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use rustc_semver::RustcVersion;
1212

1313
use super::PTR_AS_PTR;
1414

15-
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, msrv: &Option<RustcVersion>) {
16-
if !meets_msrv(msrv.as_ref(), &msrvs::POINTER_CAST) {
15+
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, msrv: Option<RustcVersion>) {
16+
if !meets_msrv(msrv, msrvs::POINTER_CAST) {
1717
return;
1818
}
1919

clippy_lints/src/checked_conversions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl_lint_pass!(CheckedConversions => [CHECKED_CONVERSIONS]);
5757

5858
impl<'tcx> LateLintPass<'tcx> for CheckedConversions {
5959
fn check_expr(&mut self, cx: &LateContext<'_>, item: &Expr<'_>) {
60-
if !meets_msrv(self.msrv.as_ref(), &msrvs::TRY_FROM) {
60+
if !meets_msrv(self.msrv, msrvs::TRY_FROM) {
6161
return;
6262
}
6363

clippy_lints/src/from_over_into.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl_lint_pass!(FromOverInto => [FROM_OVER_INTO]);
5555

5656
impl<'tcx> LateLintPass<'tcx> for FromOverInto {
5757
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
58-
if !meets_msrv(self.msrv.as_ref(), &msrvs::RE_REBALANCING_COHERENCE) {
58+
if !meets_msrv(self.msrv, msrvs::RE_REBALANCING_COHERENCE) {
5959
return;
6060
}
6161

0 commit comments

Comments
 (0)