@@ -6,7 +6,7 @@ use clippy_utils::source::snippet_with_applicability;
6
6
use clippy_utils:: ty:: is_copy;
7
7
use if_chain:: if_chain;
8
8
use rustc_errors:: Applicability ;
9
- use rustc_hir:: { BorrowKind , Expr , ExprKind } ;
9
+ use rustc_hir:: { BorrowKind , Expr , ExprKind , Mutability } ;
10
10
use rustc_lint:: { LateContext , LateLintPass } ;
11
11
use rustc_middle:: ty:: { self , Ty } ;
12
12
use rustc_session:: { declare_tool_lint, impl_lint_pass} ;
@@ -49,10 +49,10 @@ impl<'tcx> LateLintPass<'tcx> for UselessVec {
49
49
if_chain ! {
50
50
if let ty:: Ref ( _, ty, _) = cx. typeck_results( ) . expr_ty_adjusted( expr) . kind( ) ;
51
51
if let ty:: Slice ( ..) = ty. kind( ) ;
52
- if let ExprKind :: AddrOf ( BorrowKind :: Ref , _ , ref addressee) = expr. kind;
52
+ if let ExprKind :: AddrOf ( BorrowKind :: Ref , mutability , ref addressee) = expr. kind;
53
53
if let Some ( vec_args) = higher:: vec_macro( cx, addressee) ;
54
54
then {
55
- self . check_vec_macro( cx, & vec_args, expr. span) ;
55
+ self . check_vec_macro( cx, & vec_args, mutability , expr. span) ;
56
56
}
57
57
}
58
58
@@ -70,14 +70,20 @@ impl<'tcx> LateLintPass<'tcx> for UselessVec {
70
70
. ctxt( )
71
71
. outer_expn_data( )
72
72
. call_site;
73
- self . check_vec_macro( cx, & vec_args, span) ;
73
+ self . check_vec_macro( cx, & vec_args, Mutability :: Not , span) ;
74
74
}
75
75
}
76
76
}
77
77
}
78
78
79
79
impl UselessVec {
80
- fn check_vec_macro < ' tcx > ( self , cx : & LateContext < ' tcx > , vec_args : & higher:: VecArgs < ' tcx > , span : Span ) {
80
+ fn check_vec_macro < ' tcx > (
81
+ self ,
82
+ cx : & LateContext < ' tcx > ,
83
+ vec_args : & higher:: VecArgs < ' tcx > ,
84
+ mutability : Mutability ,
85
+ span : Span ,
86
+ ) {
81
87
let mut applicability = Applicability :: MachineApplicable ;
82
88
let snippet = match * vec_args {
83
89
higher:: VecArgs :: Repeat ( elem, len) => {
@@ -87,11 +93,22 @@ impl UselessVec {
87
93
return ;
88
94
}
89
95
90
- format ! (
91
- "&[{}; {}]" ,
92
- snippet_with_applicability( cx, elem. span, "elem" , & mut applicability) ,
93
- snippet_with_applicability( cx, len. span, "len" , & mut applicability)
94
- )
96
+ match mutability {
97
+ Mutability :: Mut => {
98
+ format ! (
99
+ "&mut [{}; {}]" ,
100
+ snippet_with_applicability( cx, elem. span, "elem" , & mut applicability) ,
101
+ snippet_with_applicability( cx, len. span, "len" , & mut applicability)
102
+ )
103
+ } ,
104
+ Mutability :: Not => {
105
+ format ! (
106
+ "&[{}; {}]" ,
107
+ snippet_with_applicability( cx, elem. span, "elem" , & mut applicability) ,
108
+ snippet_with_applicability( cx, len. span, "len" , & mut applicability)
109
+ )
110
+ } ,
111
+ }
95
112
} else {
96
113
return ;
97
114
}
@@ -104,9 +121,22 @@ impl UselessVec {
104
121
}
105
122
let span = args[ 0 ] . span . to ( last. span ) ;
106
123
107
- format ! ( "&[{}]" , snippet_with_applicability( cx, span, ".." , & mut applicability) )
124
+ match mutability {
125
+ Mutability :: Mut => {
126
+ format ! (
127
+ "&mut [{}]" ,
128
+ snippet_with_applicability( cx, span, ".." , & mut applicability)
129
+ )
130
+ } ,
131
+ Mutability :: Not => {
132
+ format ! ( "&[{}]" , snippet_with_applicability( cx, span, ".." , & mut applicability) )
133
+ } ,
134
+ }
108
135
} else {
109
- "&[]" . into ( )
136
+ match mutability {
137
+ Mutability :: Mut => "&mut []" . into ( ) ,
138
+ Mutability :: Not => "&[]" . into ( ) ,
139
+ }
110
140
}
111
141
} ,
112
142
} ;
0 commit comments