-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
base: master
Are you sure you want to change the base?
fix(or_fun_call): respect MSRV for unwrap_or_default suggestion #14885
Conversation
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)
r? @Alexendoo rustbot has assigned @Alexendoo. Use |
This comment has been minimized.
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)
…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
This comment has been minimized.
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
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this 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) { |
There was a problem hiding this comment.
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.
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(); |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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
changelog: [
unwrap_or_default
]: respect MSRV by not suggesting the method when MSRV is below 1.16Fixes #14876