@@ -2,7 +2,7 @@ use rustc_errors::Applicability;
2
2
use rustc_hir:: def:: Res ;
3
3
use rustc_hir:: { Arm , Expr , ExprKind , HirId , LangItem , MatchSource , Pat , PatKind , QPath } ;
4
4
use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
5
- use rustc_middle:: ty:: GenericArgKind ;
5
+ use rustc_middle:: ty:: { self , GenericArgKind } ;
6
6
use rustc_session:: declare_lint_pass;
7
7
use rustc_span:: sym;
8
8
@@ -148,6 +148,42 @@ fn handle<'tcx>(cx: &LateContext<'tcx>, if_let_or_match: IfLetOrMatch<'tcx>, exp
148
148
&& is_default_equivalent ( cx, body_none)
149
149
&& let Some ( receiver) = Sugg :: hir_opt ( cx, condition) . map ( Sugg :: maybe_par)
150
150
{
151
+ // We check if the expression is not a None variant
152
+ if let Some ( none_def_id) = cx. tcx . lang_items ( ) . option_none_variant ( ) {
153
+ if let ExprKind :: Path ( QPath :: Resolved ( _, path) ) = & condition. kind {
154
+ if let Some ( def_id) = path. res . opt_def_id ( ) {
155
+ if cx. tcx . parent ( def_id) == none_def_id {
156
+ return span_lint_and_sugg (
157
+ cx,
158
+ MANUAL_UNWRAP_OR_DEFAULT ,
159
+ expr. span ,
160
+ format ! ( "{expr_name} can be simplified with `.unwrap_or_default()`" ) ,
161
+ "replace it with" ,
162
+ format ! ( "{receiver}::</* Type */>.unwrap_or_default()" ) ,
163
+ Applicability :: HasPlaceholders ,
164
+ ) ;
165
+ }
166
+ }
167
+ }
168
+ }
169
+
170
+ // We check if the expression is not a method or function with a unspecified return type
171
+ if let ExprKind :: MethodCall ( _, expr, _, _) = & condition. kind {
172
+ if let ty:: Adt ( _, substs) = cx. typeck_results ( ) . expr_ty ( expr) . kind ( ) {
173
+ if let Some ( ok_type) = substs. first ( ) {
174
+ return span_lint_and_sugg (
175
+ cx,
176
+ MANUAL_UNWRAP_OR_DEFAULT ,
177
+ expr. span ,
178
+ format ! ( "{expr_name} can be simplified with `.unwrap_or_default()`" ) ,
179
+ format ! ( "explicit the type {ok_type} and replace your expression with" ) ,
180
+ format ! ( "{receiver}.unwrap_or_default()" ) ,
181
+ Applicability :: Unspecified ,
182
+ ) ;
183
+ }
184
+ }
185
+ }
186
+
151
187
// Machine applicable only if there are no comments present
152
188
let applicability = if span_contains_comment ( cx. sess ( ) . source_map ( ) , expr. span ) {
153
189
Applicability :: MaybeIncorrect
0 commit comments