-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix: manual_unwrap_or_default
suggests error when expression is a None variant
#12688
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ use rustc_errors::Applicability; | |
use rustc_hir::def::Res; | ||
use rustc_hir::{Arm, Expr, ExprKind, HirId, LangItem, MatchSource, Pat, PatKind, QPath}; | ||
use rustc_lint::{LateContext, LateLintPass, LintContext}; | ||
use rustc_middle::ty::GenericArgKind; | ||
use rustc_middle::ty::{self, GenericArgKind}; | ||
use rustc_session::declare_lint_pass; | ||
use rustc_span::sym; | ||
|
||
|
@@ -148,6 +148,42 @@ fn handle<'tcx>(cx: &LateContext<'tcx>, if_let_or_match: IfLetOrMatch<'tcx>, exp | |
&& is_default_equivalent(cx, body_none) | ||
&& let Some(receiver) = Sugg::hir_opt(cx, condition).map(Sugg::maybe_par) | ||
{ | ||
// We check if the expression is not a None variant | ||
if let Some(none_def_id) = cx.tcx.lang_items().option_none_variant() { | ||
if let ExprKind::Path(QPath::Resolved(_, path)) = &condition.kind { | ||
if let Some(def_id) = path.res.opt_def_id() { | ||
if cx.tcx.parent(def_id) == none_def_id { | ||
return span_lint_and_sugg( | ||
cx, | ||
MANUAL_UNWRAP_OR_DEFAULT, | ||
expr.span, | ||
format!("{expr_name} can be simplified with `.unwrap_or_default()`"), | ||
"replace it with", | ||
format!("{receiver}::</* Type */>.unwrap_or_default()"), | ||
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. You can print any if let ty::Adt(_, args) = cx.typeck_results().expr_ty(expr).kind()
&& let Some(arg) = args.first()
&& let Some(arg_ty) = arg.as_type()
{
format!("{receiver}::<{}>.unwrap_or_default()", arg_ty)
} |
||
Applicability::HasPlaceholders, | ||
); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// We check if the expression is not a method or function with a unspecified return type | ||
if let ExprKind::MethodCall(_, expr, _, _) = &condition.kind { | ||
if let ty::Adt(_, substs) = cx.typeck_results().expr_ty(expr).kind() { | ||
if let Some(ok_type) = substs.first() { | ||
return span_lint_and_sugg( | ||
cx, | ||
MANUAL_UNWRAP_OR_DEFAULT, | ||
expr.span, | ||
format!("{expr_name} can be simplified with `.unwrap_or_default()`"), | ||
format!("explicit the type {ok_type} and replace your expression with"), | ||
format!("{receiver}.unwrap_or_default()"), | ||
Applicability::Unspecified, | ||
); | ||
} | ||
} | ||
} | ||
Comment on lines
+170
to
+185
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. What are you trying to catch with this? This looks like it will just catch any scrutinee that happens to be a method call. 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. I catch all methods and functions that return a generic type that can't be inferred from it's arguments (e.g. 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. We have |
||
|
||
// Machine applicable only if there are no comments present | ||
let applicability = if span_contains_comment(cx.sess().source_map(), expr.span) { | ||
Applicability::MaybeIncorrect | ||
|
This file was deleted.
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.
This is equivalent to
path_res(cx, condition).opt_def_id().is_some_and(|id| Some(cx.tcx.parent(id)) == cx.tcx.lang_items().option_none_variant())