@@ -6,7 +6,7 @@ use clippy_utils::source::snippet_with_applicability;
66use clippy_utils:: ty:: is_copy;
77use if_chain:: if_chain;
88use rustc_errors:: Applicability ;
9- use rustc_hir:: { BorrowKind , Expr , ExprKind } ;
9+ use rustc_hir:: { BorrowKind , Expr , ExprKind , Mutability } ;
1010use rustc_lint:: { LateContext , LateLintPass } ;
1111use rustc_middle:: ty:: { self , Ty } ;
1212use rustc_session:: { declare_tool_lint, impl_lint_pass} ;
@@ -49,10 +49,10 @@ impl<'tcx> LateLintPass<'tcx> for UselessVec {
4949 if_chain ! {
5050 if let ty:: Ref ( _, ty, _) = cx. typeck_results( ) . expr_ty_adjusted( expr) . kind( ) ;
5151 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;
5353 if let Some ( vec_args) = higher:: vec_macro( cx, addressee) ;
5454 then {
55- self . check_vec_macro( cx, & vec_args, expr. span) ;
55+ self . check_vec_macro( cx, & vec_args, mutability , expr. span) ;
5656 }
5757 }
5858
@@ -70,14 +70,20 @@ impl<'tcx> LateLintPass<'tcx> for UselessVec {
7070 . ctxt( )
7171 . outer_expn_data( )
7272 . call_site;
73- self . check_vec_macro( cx, & vec_args, span) ;
73+ self . check_vec_macro( cx, & vec_args, Mutability :: Not , span) ;
7474 }
7575 }
7676 }
7777}
7878
7979impl 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+ ) {
8187 let mut applicability = Applicability :: MachineApplicable ;
8288 let snippet = match * vec_args {
8389 higher:: VecArgs :: Repeat ( elem, len) => {
@@ -87,11 +93,22 @@ impl UselessVec {
8793 return ;
8894 }
8995
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+ }
95112 } else {
96113 return ;
97114 }
@@ -104,9 +121,22 @@ impl UselessVec {
104121 }
105122 let span = args[ 0 ] . span . to ( last. span ) ;
106123
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+ }
108135 } else {
109- "&[]" . into ( )
136+ match mutability {
137+ Mutability :: Mut => "&mut []" . into ( ) ,
138+ Mutability :: Not => "&[]" . into ( ) ,
139+ }
110140 }
111141 } ,
112142 } ;
0 commit comments