]> git.lizzy.rs Git - rust.git/blobdiff - src/tools/clippy/clippy_lints/src/vec.rs
Rollup merge of #85760 - ChrisDenton:path-doc-platform-specific, r=m-ou-se
[rust.git] / src / tools / clippy / clippy_lints / src / vec.rs
index 1af9583887fbdfc1e71b1715b347768a8b4a8bbb..1d5b7c98d31416acc3dd78c667c3b614f29f2def 100644 (file)
@@ -1,12 +1,12 @@
-use crate::consts::{constant, Constant};
 use crate::rustc_target::abi::LayoutOf;
+use clippy_utils::consts::{constant, Constant};
 use clippy_utils::diagnostics::span_lint_and_sugg;
 use clippy_utils::higher;
 use clippy_utils::source::snippet_with_applicability;
 use clippy_utils::ty::is_copy;
 use if_chain::if_chain;
 use rustc_errors::Applicability;
-use rustc_hir::{BorrowKind, Expr, ExprKind};
+use rustc_hir::{BorrowKind, Expr, ExprKind, Mutability};
 use rustc_lint::{LateContext, LateLintPass};
 use rustc_middle::ty::{self, Ty};
 use rustc_session::{declare_tool_lint, impl_lint_pass};
@@ -49,10 +49,10 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
         if_chain! {
             if let ty::Ref(_, ty, _) = cx.typeck_results().expr_ty_adjusted(expr).kind();
             if let ty::Slice(..) = ty.kind();
-            if let ExprKind::AddrOf(BorrowKind::Ref, _, ref addressee) = expr.kind;
+            if let ExprKind::AddrOf(BorrowKind::Ref, mutability, addressee) = expr.kind;
             if let Some(vec_args) = higher::vec_macro(cx, addressee);
             then {
-                self.check_vec_macro(cx, &vec_args, expr.span);
+                self.check_vec_macro(cx, &vec_args, mutability, expr.span);
             }
         }
 
@@ -70,14 +70,20 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
                     .ctxt()
                     .outer_expn_data()
                     .call_site;
-                self.check_vec_macro(cx, &vec_args, span);
+                self.check_vec_macro(cx, &vec_args, Mutability::Not, span);
             }
         }
     }
 }
 
 impl UselessVec {
-    fn check_vec_macro<'tcx>(self, cx: &LateContext<'tcx>, vec_args: &higher::VecArgs<'tcx>, span: Span) {
+    fn check_vec_macro<'tcx>(
+        self,
+        cx: &LateContext<'tcx>,
+        vec_args: &higher::VecArgs<'tcx>,
+        mutability: Mutability,
+        span: Span,
+    ) {
         let mut applicability = Applicability::MachineApplicable;
         let snippet = match *vec_args {
             higher::VecArgs::Repeat(elem, len) => {
@@ -87,11 +93,22 @@ fn check_vec_macro<'tcx>(self, cx: &LateContext<'tcx>, vec_args: &higher::VecArg
                         return;
                     }
 
-                    format!(
-                        "&[{}; {}]",
-                        snippet_with_applicability(cx, elem.span, "elem", &mut applicability),
-                        snippet_with_applicability(cx, len.span, "len", &mut applicability)
-                    )
+                    match mutability {
+                        Mutability::Mut => {
+                            format!(
+                                "&mut [{}; {}]",
+                                snippet_with_applicability(cx, elem.span, "elem", &mut applicability),
+                                snippet_with_applicability(cx, len.span, "len", &mut applicability)
+                            )
+                        },
+                        Mutability::Not => {
+                            format!(
+                                "&[{}; {}]",
+                                snippet_with_applicability(cx, elem.span, "elem", &mut applicability),
+                                snippet_with_applicability(cx, len.span, "len", &mut applicability)
+                            )
+                        },
+                    }
                 } else {
                     return;
                 }
@@ -104,9 +121,22 @@ fn check_vec_macro<'tcx>(self, cx: &LateContext<'tcx>, vec_args: &higher::VecArg
                     }
                     let span = args[0].span.to(last.span);
 
-                    format!("&[{}]", snippet_with_applicability(cx, span, "..", &mut applicability))
+                    match mutability {
+                        Mutability::Mut => {
+                            format!(
+                                "&mut [{}]",
+                                snippet_with_applicability(cx, span, "..", &mut applicability)
+                            )
+                        },
+                        Mutability::Not => {
+                            format!("&[{}]", snippet_with_applicability(cx, span, "..", &mut applicability))
+                        },
+                    }
                 } else {
-                    "&[]".into()
+                    match mutability {
+                        Mutability::Mut => "&mut []".into(),
+                        Mutability::Not => "&[]".into(),
+                    }
                 }
             },
         };