@@ -10,10 +10,10 @@ use rustc_middle::hir::nested_filter;
10
10
use rustc_middle:: ty:: { Ty , TypeckResults } ;
11
11
use rustc_session:: declare_lint_pass;
12
12
use rustc_span:: Span ;
13
- use rustc_span:: symbol:: sym;
14
13
15
14
use clippy_utils:: diagnostics:: span_lint_and_then;
16
15
use clippy_utils:: source:: { IntoSpan , SpanRangeExt , snippet} ;
16
+ use clippy_utils:: sym;
17
17
use clippy_utils:: ty:: is_type_diagnostic_item;
18
18
19
19
declare_clippy_lint ! {
@@ -326,37 +326,40 @@ impl<'tcx> Visitor<'tcx> for ImplicitHasherConstructorVisitor<'_, '_, 'tcx> {
326
326
fn visit_expr ( & mut self , e : & ' tcx Expr < ' _ > ) {
327
327
if let ExprKind :: Call ( fun, args) = e. kind
328
328
&& let ExprKind :: Path ( QPath :: TypeRelative ( ty, method) ) = fun. kind
329
+ && matches ! ( method. ident. name, sym:: new | sym:: with_capacity)
329
330
&& let TyKind :: Path ( QPath :: Resolved ( None , ty_path) ) = ty. kind
330
331
&& let Some ( ty_did) = ty_path. res . opt_def_id ( )
331
332
{
332
333
if self . target . ty ( ) != self . maybe_typeck_results . unwrap ( ) . expr_ty ( e) {
333
334
return ;
334
335
}
335
336
336
- if self . cx . tcx . is_diagnostic_item ( sym :: HashMap , ty_did ) {
337
- if method . ident . name == sym:: new {
337
+ match ( self . cx . tcx . get_diagnostic_name ( ty_did ) , method . ident . name ) {
338
+ ( Some ( sym :: HashMap ) , sym:: new) => {
338
339
self . suggestions . insert ( e. span , "HashMap::default()" . to_string ( ) ) ;
339
- } else if method. ident . name . as_str ( ) == "with_capacity" {
340
+ } ,
341
+ ( Some ( sym:: HashMap ) , sym:: with_capacity) => {
340
342
self . suggestions . insert (
341
343
e. span ,
342
344
format ! (
343
345
"HashMap::with_capacity_and_hasher({}, Default::default())" ,
344
346
snippet( self . cx, args[ 0 ] . span, "capacity" ) ,
345
347
) ,
346
348
) ;
347
- }
348
- } else if self . cx . tcx . is_diagnostic_item ( sym:: HashSet , ty_did) {
349
- if method. ident . name == sym:: new {
349
+ } ,
350
+ ( Some ( sym:: HashSet ) , sym:: new) => {
350
351
self . suggestions . insert ( e. span , "HashSet::default()" . to_string ( ) ) ;
351
- } else if method. ident . name . as_str ( ) == "with_capacity" {
352
+ } ,
353
+ ( Some ( sym:: HashSet ) , sym:: with_capacity) => {
352
354
self . suggestions . insert (
353
355
e. span ,
354
356
format ! (
355
357
"HashSet::with_capacity_and_hasher({}, Default::default())" ,
356
358
snippet( self . cx, args[ 0 ] . span, "capacity" ) ,
357
359
) ,
358
360
) ;
359
- }
361
+ } ,
362
+ _ => { } ,
360
363
}
361
364
}
362
365
0 commit comments