Skip to content

fix(or_fun_call): respect MSRV for unwrap_or_default suggestion #14885

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

poglesbyg
Copy link

@poglesbyg poglesbyg commented May 24, 2025

changelog: [unwrap_or_default]: respect MSRV by not suggesting the method when MSRV is below 1.16

Fixes #14876

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 rust-lang#14876 (unwrap_or_default MSRV issue)
@rustbot
Copy link
Collaborator

rustbot commented May 24, 2025

r? @Alexendoo

rustbot has assigned @Alexendoo.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label May 24, 2025
@rustbot

This comment has been minimized.

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
4. Adds `#[allow(clippy::too_many_arguments)]` to handle the linter warning

Fixes rust-lang#14876 (unwrap_or_default MSRV issue)
@poglesbyg poglesbyg closed this May 24, 2025
@poglesbyg poglesbyg reopened this May 24, 2025
…lang#14876

Your branch is ahead of 'origin/Result--unwrap_or_default()-suggested-for-MSRV-<-1.16-rust-lang#14876' by 1 commit.
  (use "git push" to publish your local commits)

Changes to be committed:
	modified:   clippy_lints/src/methods/or_fun_call.rs
@rustbot

This comment has been minimized.

…lang#14876

Your branch is up to date with 'origin/Result--unwrap_or_default()-suggested-for-MSRV-<-1.16-rust-lang#14876'.

Changes to be committed:
	modified:   clippy_lints/src/methods/or_fun_call.rs
@rustbot

This comment has been minimized.

Copy link
Contributor

@samueltardieu samueltardieu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only Result::unwrap_or_default() should be filtered out below the MSRV, as Option::unwrap_or_default() which exists since Rust 1.0.0 and is always fine.

Also, you must add tests, with both Option and Result, with both MSRV (see the #[clippy::msrv] attribute).

) -> bool {
// Don't suggest unwrap_or_default if MSRV is less than 1.16
if !msrv.meets(cx, msrvs::STR_REPEAT) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why STR_REPEAT is this is about unwrap_or_default()? You should introduce a RESULT_UNWRAP_OR_DEFAULT and check for this.

Comment on lines +68 to +69
if let ty::FnDef(def_id, substs) = fun_ty.kind() {
let output_ty = cx.tcx.fn_sig(def_id).instantiate(cx.tcx, substs).skip_binder().output();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the gratuitous name change? What was wrong with args, since there are the generic arguments of the FnDef?

@@ -26,11 +28,13 @@ pub(super) fn check<'tcx>(
name: Symbol,
receiver: &'tcx hir::Expr<'_>,
args: &'tcx [hir::Expr<'_>],
msrv: Msrv,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the lint now checks MSRV, it must be added to the list in the clippy_conf/src/conf.rs and the tests reblessed to include the new metadata.

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 rust-lang#12973
@rustbot
Copy link
Collaborator

rustbot commented May 31, 2025

⚠️ Warning ⚠️

  • There are issue links (such as #123) in the commit messages of the following commits.
    Please remove them as they will spam the issue with references to the commit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Result::unwrap_or_default() suggested for MSRV < 1.16
4 participants