1
1
#![ allow( rustc:: default_hash_types) ]
2
2
3
3
mod box_vec;
4
+ mod redundant_allocation;
5
+ mod utils;
4
6
5
7
use std:: borrow:: Cow ;
6
8
use std:: cmp:: Ordering ;
@@ -13,8 +15,8 @@ use rustc_hir as hir;
13
15
use rustc_hir:: intravisit:: { walk_body, walk_expr, walk_ty, FnKind , NestedVisitorMap , Visitor } ;
14
16
use rustc_hir:: {
15
17
BinOpKind , Block , Body , Expr , ExprKind , FnDecl , FnRetTy , FnSig , GenericArg , GenericBounds , GenericParamKind , HirId ,
16
- ImplItem , ImplItemKind , Item , ItemKind , LangItem , Lifetime , Lit , Local , MatchSource , MutTy , Mutability , Node ,
17
- QPath , Stmt , StmtKind , SyntheticTyParamKind , TraitFn , TraitItem , TraitItemKind , TyKind , UnOp ,
18
+ ImplItem , ImplItemKind , Item , ItemKind , Lifetime , Lit , Local , MatchSource , MutTy , Mutability , Node , QPath , Stmt ,
19
+ StmtKind , SyntheticTyParamKind , TraitFn , TraitItem , TraitItemKind , TyKind , UnOp ,
18
20
} ;
19
21
use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
20
22
use rustc_middle:: hir:: map:: Map ;
@@ -35,10 +37,10 @@ use crate::utils::paths;
35
37
use crate :: utils:: sugg:: Sugg ;
36
38
use crate :: utils:: {
37
39
clip, comparisons, differing_macro_contexts, get_qpath_generic_tys, higher, in_constant, indent_of, int_bits,
38
- is_hir_ty_cfg_dependant, is_ty_param_diagnostic_item, is_ty_param_lang_item , is_type_diagnostic_item ,
39
- last_path_segment , match_def_path , match_path, meets_msrv, method_chain_args, multispan_sugg,
40
- numeric_literal :: NumericLiteral , reindent_multiline , sext, snippet, snippet_opt, snippet_with_applicability,
41
- snippet_with_macro_callsite , span_lint , span_lint_and_help , span_lint_and_sugg, span_lint_and_then, unsext,
40
+ is_hir_ty_cfg_dependant, is_ty_param_diagnostic_item, is_type_diagnostic_item , last_path_segment , match_def_path ,
41
+ match_path, meets_msrv, method_chain_args, multispan_sugg, numeric_literal :: NumericLiteral , reindent_multiline ,
42
+ sext, snippet, snippet_opt, snippet_with_applicability, snippet_with_macro_callsite , span_lint , span_lint_and_help ,
43
+ span_lint_and_sugg, span_lint_and_then, unsext,
42
44
} ;
43
45
44
46
declare_clippy_lint ! {
@@ -301,23 +303,6 @@ fn match_buffer_type(cx: &LateContext<'_>, qpath: &QPath<'_>) -> Option<&'static
301
303
}
302
304
}
303
305
304
- fn match_borrows_parameter ( _cx : & LateContext < ' _ > , qpath : & QPath < ' _ > ) -> Option < Span > {
305
- let last = last_path_segment ( qpath) ;
306
- if_chain ! {
307
- if let Some ( ref params) = last. args;
308
- if !params. parenthesized;
309
- if let Some ( ty) = params. args. iter( ) . find_map( |arg| match arg {
310
- GenericArg :: Type ( ty) => Some ( ty) ,
311
- _ => None ,
312
- } ) ;
313
- if let TyKind :: Rptr ( ..) = ty. kind;
314
- then {
315
- return Some ( ty. span) ;
316
- }
317
- }
318
- None
319
- }
320
-
321
306
impl Types {
322
307
pub fn new ( vec_box_size_threshold : u64 ) -> Self {
323
308
Self { vec_box_size_threshold }
@@ -349,58 +334,8 @@ impl Types {
349
334
let res = cx. qpath_res ( qpath, hir_id) ;
350
335
if let Some ( def_id) = res. opt_def_id ( ) {
351
336
box_vec:: check ( cx, hir_ty, qpath, def_id) ;
352
- if Some ( def_id) == cx. tcx . lang_items ( ) . owned_box ( ) {
353
- if let Some ( span) = match_borrows_parameter ( cx, qpath) {
354
- let mut applicability = Applicability :: MachineApplicable ;
355
- span_lint_and_sugg (
356
- cx,
357
- REDUNDANT_ALLOCATION ,
358
- hir_ty. span ,
359
- "usage of `Box<&T>`" ,
360
- "try" ,
361
- snippet_with_applicability ( cx, span, ".." , & mut applicability) . to_string ( ) ,
362
- applicability,
363
- ) ;
364
- return ; // don't recurse into the type
365
- }
366
- } else if cx. tcx . is_diagnostic_item ( sym:: Rc , def_id) {
367
- if let Some ( ty) = is_ty_param_diagnostic_item ( cx, qpath, sym:: Rc ) {
368
- let mut applicability = Applicability :: MachineApplicable ;
369
- span_lint_and_sugg (
370
- cx,
371
- REDUNDANT_ALLOCATION ,
372
- hir_ty. span ,
373
- "usage of `Rc<Rc<T>>`" ,
374
- "try" ,
375
- snippet_with_applicability ( cx, ty. span , ".." , & mut applicability) . to_string ( ) ,
376
- applicability,
377
- ) ;
378
- return ; // don't recurse into the type
379
- }
380
- if let Some ( ty) = is_ty_param_lang_item ( cx, qpath, LangItem :: OwnedBox ) {
381
- let qpath = match & ty. kind {
382
- TyKind :: Path ( qpath) => qpath,
383
- _ => return ,
384
- } ;
385
- let inner_span = match get_qpath_generic_tys ( qpath) . next ( ) {
386
- Some ( ty) => ty. span ,
387
- None => return ,
388
- } ;
389
- let mut applicability = Applicability :: MachineApplicable ;
390
- span_lint_and_sugg (
391
- cx,
392
- REDUNDANT_ALLOCATION ,
393
- hir_ty. span ,
394
- "usage of `Rc<Box<T>>`" ,
395
- "try" ,
396
- format ! (
397
- "Rc<{}>" ,
398
- snippet_with_applicability( cx, inner_span, ".." , & mut applicability)
399
- ) ,
400
- applicability,
401
- ) ;
402
- return ; // don't recurse into the type
403
- }
337
+ redundant_allocation:: check ( cx, hir_ty, qpath, def_id) ;
338
+ if cx. tcx . is_diagnostic_item ( sym:: Rc , def_id) {
404
339
if let Some ( alternate) = match_buffer_type ( cx, qpath) {
405
340
span_lint_and_sugg (
406
341
cx,
@@ -437,19 +372,6 @@ impl Types {
437
372
) ;
438
373
return ; // don't recurse into the type
439
374
}
440
- if let Some ( span) = match_borrows_parameter ( cx, qpath) {
441
- let mut applicability = Applicability :: MachineApplicable ;
442
- span_lint_and_sugg (
443
- cx,
444
- REDUNDANT_ALLOCATION ,
445
- hir_ty. span ,
446
- "usage of `Rc<&T>`" ,
447
- "try" ,
448
- snippet_with_applicability ( cx, span, ".." , & mut applicability) . to_string ( ) ,
449
- applicability,
450
- ) ;
451
- return ; // don't recurse into the type
452
- }
453
375
} else if cx. tcx . is_diagnostic_item ( sym:: Arc , def_id) {
454
376
if let Some ( alternate) = match_buffer_type ( cx, qpath) {
455
377
span_lint_and_sugg (
0 commit comments