Skip to content

Commit 9ae6e24

Browse files
committed
Add MSRV check for Result::unwrap_or_default()
This change ensures that the `unwrap_or_default` suggestion is only made for `Result` types when the MSRV is at least 1.16, since `Result::unwrap_or_default()` was stabilized in that version. The suggestion for `Option::unwrap_or_default()` remains available for all MSRVs. - Add `RESULT_UNWRAP_OR_DEFAULT` constant to `msrv_aliases!` macro - Update `check_unwrap_or_default` to only check MSRV for `Result` types - Add test cases for both `Option` and `Result` with different MSRVs Fixes #12973
1 parent 90a5f53 commit 9ae6e24

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,8 @@ helper.txt
4646

4747
# mdbook generated output
4848
/book/book
49+
.cursorrules
50+
.cursor/rules/data-flow-analysis.mdc
51+
.giga/specifications.json
52+
.cursor/rules
53+
.cursor/rules/lint-implementation-patterns.mdc

clippy_lints/src/methods/or_fun_call.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ pub(super) fn check<'tcx>(
4545
method_span: Span,
4646
msrv: Msrv,
4747
) -> bool {
48-
// Don't suggest unwrap_or_default if MSRV is less than 1.16
49-
if !msrv.meets(cx, msrvs::STR_REPEAT) {
48+
// Only check MSRV for Result type
49+
let receiver_ty = cx.typeck_results().expr_ty_adjusted(receiver).peel_refs();
50+
if is_type_diagnostic_item(cx, receiver_ty, sym::Result) && !msrv.meets(cx, msrvs::RESULT_UNWRAP_OR_DEFAULT) {
5051
return false;
5152
}
5253

clippy_utils/src/msrvs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ msrv_aliases! {
7676
1,24,0 { IS_ASCII_DIGIT }
7777
1,18,0 { HASH_MAP_RETAIN, HASH_SET_RETAIN }
7878
1,17,0 { FIELD_INIT_SHORTHAND, STATIC_IN_CONST, EXPECT_ERR }
79-
1,16,0 { STR_REPEAT }
79+
1,16,0 { STR_REPEAT, RESULT_UNWRAP_OR_DEFAULT }
8080
1,15,0 { MAYBE_BOUND_IN_WHERE }
8181
1,13,0 { QUESTION_MARK_OPERATOR }
8282
}

tests/ui/or_fun_call.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,4 +409,34 @@ fn fn_call_in_nested_expr() {
409409
//~^ or_fun_call
410410
}
411411

412+
mod msrv_tests {
413+
#[clippy::msrv = "1.15"]
414+
fn test_option_below_msrv() {
415+
let opt = Some(1);
416+
let _ = opt.unwrap_or(0);
417+
//~^ unwrap_or_default
418+
}
419+
420+
#[clippy::msrv = "1.15"]
421+
fn test_result_below_msrv() {
422+
let res: Result<i32, ()> = Ok(1);
423+
let _ = res.unwrap_or(0);
424+
//~^ or_fun_call
425+
}
426+
427+
#[clippy::msrv = "1.16"]
428+
fn test_option_above_msrv() {
429+
let opt = Some(1);
430+
let _ = opt.unwrap_or(0);
431+
//~^ unwrap_or_default
432+
}
433+
434+
#[clippy::msrv = "1.16"]
435+
fn test_result_above_msrv() {
436+
let res: Result<i32, ()> = Ok(1);
437+
let _ = res.unwrap_or(0);
438+
//~^ unwrap_or_default
439+
}
440+
}
441+
412442
fn main() {}

0 commit comments

Comments
 (0)