Skip to content

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
Member

@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).

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

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
Member

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?

name: Symbol,
receiver: &'tcx hir::Expr<'_>,
args: &'tcx [hir::Expr<'_>],
msrv: Msrv,
Copy link
Member

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.

@rustbot
Copy link
Collaborator

rustbot commented Aug 17, 2025

☔ The latest upstream changes (possibly #14896) made this pull request unmergeable. Please resolve the merge conflicts.

@ada4a
Copy link
Contributor

ada4a commented Sep 24, 2025

Hi @poglesbyg, do you still intend to work on this? If not, I could happily take over -- but no pressure^^

@poglesbyg
Copy link
Author

Please takeover. Thanks!

github-merge-queue bot pushed a commit that referenced this pull request Sep 24, 2025
…tion (#15756)

Supersedes #14885
Fixes #14876

changelog: [`or_fun_call`]: respect MSRV for `Result::unwrap_or_default`
suggestion

r? @samueltardieu since you reviewed the original PR, but do feel free
to reroll if you're no longer interested
@ada4a ada4a closed this Oct 11, 2025
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Oct 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Result::unwrap_or_default() suggested for MSRV < 1.16

5 participants