@@ -4,15 +4,15 @@ use clippy_utils::source::snippet;
44use clippy_utils:: sugg:: Sugg ;
55use clippy_utils:: ty:: { is_type_diagnostic_item, needs_ordered_drop} ;
66use 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} ;
88use if_chain:: if_chain;
99use rustc_ast:: ast:: LitKind ;
1010use rustc_errors:: Applicability ;
11- use rustc_hir:: LangItem :: { OptionNone , PollPending } ;
11+ use rustc_hir:: LangItem :: { self , OptionSome , OptionNone , PollPending , PollReady , ResultOk , ResultErr } ;
1212use rustc_hir:: { Arm , Expr , ExprKind , Node , Pat , PatKind , QPath , UnOp } ;
1313use rustc_lint:: LateContext ;
1414use rustc_middle:: ty:: { self , subst:: GenericArgKind , DefIdTree , Ty } ;
15- use rustc_span:: { sym, Symbol , def_id :: DefId } ;
15+ use rustc_span:: { sym, Symbol } ;
1616
1717pub ( super ) fn check < ' tcx > ( cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) {
1818 if let Some ( higher:: WhileLet { let_pat, let_expr, .. } ) = higher:: WhileLet :: hir ( expr) {
@@ -75,9 +75,9 @@ fn find_sugg_for_if_let<'tcx>(
7575 ( "is_some()" , op_ty)
7676 } else if Some ( id) == lang_items. poll_ready_variant ( ) {
7777 ( "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 ) ) ) {
7979 ( "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 ) ) ) {
8181 ( "is_ipv6()" , op_ty)
8282 } else {
8383 return ;
@@ -174,7 +174,6 @@ fn find_sugg_for_if_let<'tcx>(
174174
175175pub ( super ) fn check_match < ' tcx > ( cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > , op : & Expr < ' _ > , arms : & [ Arm < ' _ > ] ) {
176176 if arms. len ( ) == 2 {
177- let lang_items = cx. tcx . lang_items ( ) ;
178177 let node_pair = ( & arms[ 0 ] . pat . kind , & arms[ 1 ] . pat . kind ) ;
179178
180179 let found_good_method = match node_pair {
@@ -188,10 +187,8 @@ pub(super) fn check_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, op
188187 arms,
189188 path_left,
190189 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 ) ,
195192 "is_ok()" ,
196193 "is_err()" ,
197194 )
@@ -201,10 +198,8 @@ pub(super) fn check_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, op
201198 arms,
202199 path_left,
203200 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 ) ) ,
208203 "is_ipv4()" ,
209204 "is_ipv6()" ,
210205 )
@@ -224,10 +219,8 @@ pub(super) fn check_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, op
224219 arms,
225220 path_left,
226221 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 ) ,
231224 "is_some()" ,
232225 "is_none()" ,
233226 )
@@ -237,10 +230,8 @@ pub(super) fn check_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, op
237230 arms,
238231 path_left,
239232 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 ) ,
244235 "is_ready()" ,
245236 "is_pending()" ,
246237 )
@@ -278,21 +269,17 @@ pub(super) fn check_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, op
278269
279270#[ derive( Clone , Copy ) ]
280271enum Item {
281- Lang ( Option < DefId > ) ,
272+ Lang ( LangItem ) ,
282273 Diag ( Symbol , Symbol ) ,
283274}
284275
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 {
286277 let Some ( id) = cx. typeck_results ( ) . qpath_res ( path, pat. hir_id ) . opt_def_id ( ) else { return false } ;
287278
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-
293279 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
296283 } ,
297284 Item :: Diag ( expected_ty, expected_variant) => {
298285 let ty = cx. typeck_results ( ) . pat_ty ( pat) ;
@@ -316,9 +303,7 @@ fn find_good_method_for_match<'a>(
316303 arms : & [ Arm < ' _ > ] ,
317304 path_left : & QPath < ' _ > ,
318305 path_right : & QPath < ' _ > ,
319- expected_path_left : & [ & str ] ,
320306 expected_item_left : Item ,
321- expected_path_right : & [ & str ] ,
322307 expected_item_right : Item ,
323308 should_be_left : & ' a str ,
324309 should_be_right : & ' a str ,
@@ -327,15 +312,15 @@ fn find_good_method_for_match<'a>(
327312 let pat_right = arms[ 1 ] . pat ;
328313
329314 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)
331316 ) && (
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)
333318 ) {
334319 ( & arms[ 0 ] . body . kind , & arms[ 1 ] . body . kind )
335320 } 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)
337322 ) && (
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)
339324 ) {
340325 ( & arms[ 1 ] . body . kind , & arms[ 0 ] . body . kind )
341326 } else {
0 commit comments