-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
poglesbyg
wants to merge
5
commits into
rust-lang:master
Choose a base branch
from
poglesbyg:Result--unwrap_or_default()-suggested-for-MSRV-<-1.16-#14876
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+53
−7
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
56df855
fix(or_fun_call): respect MSRV for unwrap_or_default suggestion
poglesbyg 73d9ce6
fix(or_fun_call): respect MSRV for unwrap_or_default suggestion
poglesbyg 9476746
On branch Result--unwrap_or_default()-suggested-for-MSRV-<-1.16-#14876
poglesbyg 90a5f53
On branch Result--unwrap_or_default()-suggested-for-MSRV-<-1.16-#14876
poglesbyg 9ae6e24
Add MSRV check for Result::unwrap_or_default()
poglesbyg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
#![allow(clippy::too_many_arguments)] | ||
use std::ops::ControlFlow; | ||
|
||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::eager_or_lazy::switch_to_lazy_eval; | ||
use clippy_utils::msrvs::{self, Msrv}; | ||
use clippy_utils::source::snippet_with_context; | ||
use clippy_utils::ty::{expr_type_is_certain, implements_trait, is_type_diagnostic_item}; | ||
use clippy_utils::visitors::for_each_expr; | ||
|
@@ -26,11 +28,13 @@ pub(super) fn check<'tcx>( | |
name: Symbol, | ||
receiver: &'tcx hir::Expr<'_>, | ||
args: &'tcx [hir::Expr<'_>], | ||
msrv: Msrv, | ||
) { | ||
/// Checks for `unwrap_or(T::new())`, `unwrap_or(T::default())`, | ||
/// `or_insert(T::new())` or `or_insert(T::default())`. | ||
/// Similarly checks for `unwrap_or_else(T::new)`, `unwrap_or_else(T::default)`, | ||
/// `or_insert_with(T::new)` or `or_insert_with(T::default)`. | ||
#[allow(clippy::too_many_arguments)] | ||
fn check_unwrap_or_default( | ||
cx: &LateContext<'_>, | ||
name: Symbol, | ||
|
@@ -39,7 +43,14 @@ pub(super) fn check<'tcx>( | |
call_expr: Option<&hir::Expr<'_>>, | ||
span: Span, | ||
method_span: Span, | ||
msrv: Msrv, | ||
) -> bool { | ||
// Only check MSRV for Result type | ||
let receiver_ty = cx.typeck_results().expr_ty_adjusted(receiver).peel_refs(); | ||
if is_type_diagnostic_item(cx, receiver_ty, sym::Result) && !msrv.meets(cx, msrvs::RESULT_UNWRAP_OR_DEFAULT) { | ||
return false; | ||
} | ||
|
||
if !expr_type_is_certain(cx, receiver) { | ||
return false; | ||
} | ||
|
@@ -55,8 +66,8 @@ pub(super) fn check<'tcx>( | |
|
||
let output_type_implements_default = |fun| { | ||
let fun_ty = cx.typeck_results().expr_ty(fun); | ||
if let ty::FnDef(def_id, args) = fun_ty.kind() { | ||
let output_ty = cx.tcx.fn_sig(def_id).instantiate(cx.tcx, args).skip_binder().output(); | ||
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(); | ||
Comment on lines
+69
to
+70
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the gratuitous name change? What was wrong with |
||
cx.tcx | ||
.get_diagnostic_item(sym::Default) | ||
.is_some_and(|default_trait_id| implements_trait(cx, output_ty, default_trait_id, &[])) | ||
|
@@ -215,11 +226,11 @@ pub(super) fn check<'tcx>( | |
}; | ||
(!inner_fun_has_args | ||
&& !is_nested_expr | ||
&& check_unwrap_or_default(cx, name, receiver, fun, Some(ex), expr.span, method_span)) | ||
&& check_unwrap_or_default(cx, name, receiver, fun, Some(ex), expr.span, method_span, msrv)) | ||
|| check_or_fn_call(cx, name, method_span, receiver, arg, None, expr.span, fun_span) | ||
}, | ||
hir::ExprKind::Path(..) | hir::ExprKind::Closure(..) if !is_nested_expr => { | ||
check_unwrap_or_default(cx, name, receiver, ex, None, expr.span, method_span) | ||
check_unwrap_or_default(cx, name, receiver, ex, None, expr.span, method_span, msrv) | ||
}, | ||
hir::ExprKind::Index(..) | hir::ExprKind::MethodCall(..) => { | ||
check_or_fn_call(cx, name, method_span, receiver, arg, None, expr.span, None) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.