Skip to content

Commit 56df855

Browse files
committed
fix(or_fun_call): respect MSRV for unwrap_or_default suggestion
The `unwrap_or_default()` method was introduced in Rust 1.16, but the lint was suggesting it even when the MSRV was set to 1.15 or lower. This change adds an MSRV check to ensure we only suggest `unwrap_or_default()` when the MSRV is at least 1.16. The fix: 1. Adds MSRV check in `check_unwrap_or_default` using `msrvs::STR_REPEAT` (Rust 1.16) 2. Adds MSRV parameter to the `check` function signature 3. Updates the call site to pass the MSRV parameter Fixes #14876 (unwrap_or_default MSRV issue)
1 parent 24a2a66 commit 56df855

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4412,7 +4412,7 @@ declare_clippy_lint! {
44124412
/// Checks for calls to `Read::bytes` on types which don't implement `BufRead`.
44134413
///
44144414
/// ### Why is this bad?
4415-
/// The default implementation calls `read` for each byte, which can be very inefficient for data thats not in memory, such as `File`.
4415+
/// The default implementation calls `read` for each byte, which can be very inefficient for data that's not in memory, such as `File`.
44164416
///
44174417
/// ### Example
44184418
/// ```no_run
@@ -4741,7 +4741,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
47414741
},
47424742
ExprKind::MethodCall(method_call, receiver, args, _) => {
47434743
let method_span = method_call.ident.span;
4744-
or_fun_call::check(cx, expr, method_span, method_call.ident.name, receiver, args);
4744+
or_fun_call::check(cx, expr, method_span, method_call.ident.name, receiver, args, self.msrv);
47454745
expect_fun_call::check(
47464746
cx,
47474747
&self.format_args,

clippy_lints/src/methods/or_fun_call.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::ops::ControlFlow;
22

33
use clippy_utils::diagnostics::span_lint_and_sugg;
44
use clippy_utils::eager_or_lazy::switch_to_lazy_eval;
5+
use clippy_utils::msrvs::{self, Msrv};
56
use clippy_utils::source::snippet_with_context;
67
use clippy_utils::ty::{expr_type_is_certain, implements_trait, is_type_diagnostic_item};
78
use clippy_utils::visitors::for_each_expr;
@@ -26,6 +27,7 @@ pub(super) fn check<'tcx>(
2627
name: Symbol,
2728
receiver: &'tcx hir::Expr<'_>,
2829
args: &'tcx [hir::Expr<'_>],
30+
msrv: Msrv,
2931
) {
3032
/// Checks for `unwrap_or(T::new())`, `unwrap_or(T::default())`,
3133
/// `or_insert(T::new())` or `or_insert(T::default())`.
@@ -39,7 +41,13 @@ pub(super) fn check<'tcx>(
3941
call_expr: Option<&hir::Expr<'_>>,
4042
span: Span,
4143
method_span: Span,
44+
msrv: Msrv,
4245
) -> bool {
46+
// Don't suggest unwrap_or_default if MSRV is less than 1.16
47+
if !msrv.meets(cx, msrvs::STR_REPEAT) {
48+
return false;
49+
}
50+
4351
if !expr_type_is_certain(cx, receiver) {
4452
return false;
4553
}
@@ -215,11 +223,11 @@ pub(super) fn check<'tcx>(
215223
};
216224
(!inner_fun_has_args
217225
&& !is_nested_expr
218-
&& check_unwrap_or_default(cx, name, receiver, fun, Some(ex), expr.span, method_span))
226+
&& check_unwrap_or_default(cx, name, receiver, fun, Some(ex), expr.span, method_span, msrv))
219227
|| check_or_fn_call(cx, name, method_span, receiver, arg, None, expr.span, fun_span)
220228
},
221229
hir::ExprKind::Path(..) | hir::ExprKind::Closure(..) if !is_nested_expr => {
222-
check_unwrap_or_default(cx, name, receiver, ex, None, expr.span, method_span)
230+
check_unwrap_or_default(cx, name, receiver, ex, None, expr.span, method_span, msrv)
223231
},
224232
hir::ExprKind::Index(..) | hir::ExprKind::MethodCall(..) => {
225233
check_or_fn_call(cx, name, method_span, receiver, arg, None, expr.span, None)

0 commit comments

Comments
 (0)