@@ -4,15 +4,15 @@ use clippy_utils::source::snippet;
4
4
use clippy_utils:: sugg:: Sugg ;
5
5
use clippy_utils:: ty:: { is_type_diagnostic_item, needs_ordered_drop} ;
6
6
use clippy_utils:: visitors:: any_temporaries_need_ordered_drop;
7
- use clippy_utils:: { higher, is_lang_ctor, is_trait_method, match_def_path , paths } ;
7
+ use clippy_utils:: { higher, is_lang_ctor, is_trait_method} ;
8
8
use if_chain:: if_chain;
9
9
use rustc_ast:: ast:: LitKind ;
10
10
use rustc_errors:: Applicability ;
11
- use rustc_hir:: LangItem :: { OptionNone , PollPending } ;
11
+ use rustc_hir:: LangItem :: { self , OptionSome , OptionNone , PollPending , PollReady , ResultOk , ResultErr } ;
12
12
use rustc_hir:: { Arm , Expr , ExprKind , Node , Pat , PatKind , QPath , UnOp } ;
13
13
use rustc_lint:: LateContext ;
14
14
use rustc_middle:: ty:: { self , subst:: GenericArgKind , DefIdTree , Ty } ;
15
- use rustc_span:: { sym, Symbol , def_id :: DefId } ;
15
+ use rustc_span:: { sym, Symbol } ;
16
16
17
17
pub ( super ) fn check < ' tcx > ( cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) {
18
18
if let Some ( higher:: WhileLet { let_pat, let_expr, .. } ) = higher:: WhileLet :: hir ( expr) {
@@ -75,9 +75,9 @@ fn find_sugg_for_if_let<'tcx>(
75
75
( "is_some()" , op_ty)
76
76
} else if Some ( id) == lang_items. poll_ready_variant ( ) {
77
77
( "is_ready()" , op_ty)
78
- } else if is_pat_variant ( cx, check_pat, qpath, & paths :: IPADDR_V4 , Item :: Diag ( sym ! ( IpAddr ) , sym ! ( V4 ) ) ) {
78
+ } else if is_pat_variant ( cx, check_pat, qpath, Item :: Diag ( sym:: IpAddr , sym ! ( V4 ) ) ) {
79
79
( "is_ipv4()" , op_ty)
80
- } else if is_pat_variant ( cx, check_pat, qpath, & paths :: IPADDR_V6 , Item :: Diag ( sym ! ( IpAddr ) , sym ! ( V6 ) ) ) {
80
+ } else if is_pat_variant ( cx, check_pat, qpath, Item :: Diag ( sym:: IpAddr , sym ! ( V6 ) ) ) {
81
81
( "is_ipv6()" , op_ty)
82
82
} else {
83
83
return ;
@@ -174,7 +174,6 @@ fn find_sugg_for_if_let<'tcx>(
174
174
175
175
pub ( super ) fn check_match < ' tcx > ( cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > , op : & Expr < ' _ > , arms : & [ Arm < ' _ > ] ) {
176
176
if arms. len ( ) == 2 {
177
- let lang_items = cx. tcx . lang_items ( ) ;
178
177
let node_pair = ( & arms[ 0 ] . pat . kind , & arms[ 1 ] . pat . kind ) ;
179
178
180
179
let found_good_method = match node_pair {
@@ -188,10 +187,8 @@ pub(super) fn check_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, op
188
187
arms,
189
188
path_left,
190
189
path_right,
191
- & paths:: RESULT_OK ,
192
- Item :: Lang ( lang_items. result_ok_variant ( ) ) ,
193
- & paths:: RESULT_ERR ,
194
- Item :: Lang ( lang_items. result_err_variant ( ) ) ,
190
+ Item :: Lang ( ResultOk ) ,
191
+ Item :: Lang ( ResultErr ) ,
195
192
"is_ok()" ,
196
193
"is_err()" ,
197
194
)
@@ -201,10 +198,8 @@ pub(super) fn check_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, op
201
198
arms,
202
199
path_left,
203
200
path_right,
204
- & paths:: IPADDR_V4 ,
205
- Item :: Diag ( sym ! ( IpAddr ) , sym ! ( V4 ) ) ,
206
- & paths:: IPADDR_V6 ,
207
- Item :: Diag ( sym ! ( IpAddr ) , sym ! ( V6 ) ) ,
201
+ Item :: Diag ( sym:: IpAddr , sym ! ( V4 ) ) ,
202
+ Item :: Diag ( sym:: IpAddr , sym ! ( V6 ) ) ,
208
203
"is_ipv4()" ,
209
204
"is_ipv6()" ,
210
205
)
@@ -224,10 +219,8 @@ pub(super) fn check_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, op
224
219
arms,
225
220
path_left,
226
221
path_right,
227
- & paths:: OPTION_SOME ,
228
- Item :: Lang ( lang_items. option_some_variant ( ) ) ,
229
- & paths:: OPTION_NONE ,
230
- Item :: Lang ( lang_items. option_none_variant ( ) ) ,
222
+ Item :: Lang ( OptionSome ) ,
223
+ Item :: Lang ( OptionNone ) ,
231
224
"is_some()" ,
232
225
"is_none()" ,
233
226
)
@@ -237,10 +230,8 @@ pub(super) fn check_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, op
237
230
arms,
238
231
path_left,
239
232
path_right,
240
- & paths:: POLL_READY ,
241
- Item :: Lang ( lang_items. poll_ready_variant ( ) ) ,
242
- & paths:: POLL_PENDING ,
243
- Item :: Lang ( lang_items. poll_pending_variant ( ) ) ,
233
+ Item :: Lang ( PollReady ) ,
234
+ Item :: Lang ( PollPending ) ,
244
235
"is_ready()" ,
245
236
"is_pending()" ,
246
237
)
@@ -278,21 +269,17 @@ pub(super) fn check_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, op
278
269
279
270
#[ derive( Clone , Copy ) ]
280
271
enum Item {
281
- Lang ( Option < DefId > ) ,
272
+ Lang ( LangItem ) ,
282
273
Diag ( Symbol , Symbol ) ,
283
274
}
284
275
285
- fn is_pat_variant ( cx : & LateContext < ' _ > , pat : & Pat < ' _ > , path : & QPath < ' _ > , expected_path : & [ & str ] , expected_item : Item ) -> bool {
276
+ fn is_pat_variant ( cx : & LateContext < ' _ > , pat : & Pat < ' _ > , path : & QPath < ' _ > , expected_item : Item ) -> bool {
286
277
let Some ( id) = cx. typeck_results ( ) . qpath_res ( path, pat. hir_id ) . opt_def_id ( ) else { return false } ;
287
278
288
- // TODO: Path matching can be removed when `IpAddr` is a diagnostic item.
289
- if match_def_path ( cx, id, expected_path) {
290
- return true
291
- }
292
-
293
279
match expected_item {
294
- Item :: Lang ( expected_id) => {
295
- Some ( cx. tcx . parent ( id) ) == expected_id
280
+ Item :: Lang ( expected_lang_item) => {
281
+ let expected_id = cx. tcx . lang_items ( ) . require ( expected_lang_item) . unwrap ( ) ;
282
+ cx. tcx . parent ( id) == expected_id
296
283
} ,
297
284
Item :: Diag ( expected_ty, expected_variant) => {
298
285
let ty = cx. typeck_results ( ) . pat_ty ( pat) ;
@@ -316,9 +303,7 @@ fn find_good_method_for_match<'a>(
316
303
arms : & [ Arm < ' _ > ] ,
317
304
path_left : & QPath < ' _ > ,
318
305
path_right : & QPath < ' _ > ,
319
- expected_path_left : & [ & str ] ,
320
306
expected_item_left : Item ,
321
- expected_path_right : & [ & str ] ,
322
307
expected_item_right : Item ,
323
308
should_be_left : & ' a str ,
324
309
should_be_right : & ' a str ,
@@ -327,15 +312,15 @@ fn find_good_method_for_match<'a>(
327
312
let pat_right = arms[ 1 ] . pat ;
328
313
329
314
let body_node_pair = if (
330
- is_pat_variant ( cx, pat_left, path_left, expected_path_left , expected_item_left)
315
+ is_pat_variant ( cx, pat_left, path_left, expected_item_left)
331
316
) && (
332
- is_pat_variant ( cx, pat_right, path_right, expected_path_right , expected_item_right)
317
+ is_pat_variant ( cx, pat_right, path_right, expected_item_right)
333
318
) {
334
319
( & arms[ 0 ] . body . kind , & arms[ 1 ] . body . kind )
335
320
} else if (
336
- is_pat_variant ( cx, pat_left, path_left, expected_path_right , expected_item_right)
321
+ is_pat_variant ( cx, pat_left, path_left, expected_item_right)
337
322
) && (
338
- is_pat_variant ( cx, pat_right, path_right, expected_path_left , expected_item_left)
323
+ is_pat_variant ( cx, pat_right, path_right, expected_item_left)
339
324
) {
340
325
( & arms[ 1 ] . body . kind , & arms[ 0 ] . body . kind )
341
326
} else {
0 commit comments