|
| 1 | +use rustc_hir::*; |
| 2 | +use rustc_lint::{LateContext, LateLintPass}; |
| 3 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 4 | +use clippy_utils::diagnostics::span_lint_and_help; |
| 5 | +use clippy_utils::macros::{MacroCall, root_macro_call_first_node}; |
| 6 | +use clippy_utils::{last_path_segment, match_def_path, paths}; |
| 7 | +use rustc_span::sym; |
| 8 | + |
| 9 | +declare_clippy_lint! { |
| 10 | + /// ### What it does |
| 11 | + /// Checks for `Arc::new` calls in vector initialization using the slice macro `vec![elem; len]` |
| 12 | + /// |
| 13 | + /// ### Why is this bad? |
| 14 | + /// This vector initialization creates `elem` once and clones it `len` times - doing so with `Arc` |
| 15 | + /// is a bit missleading, because the vector will contain references to the same pointer, although |
| 16 | + /// it can look like it'll contain different `Arc` instances. |
| 17 | + /// |
| 18 | + /// ### Example |
| 19 | + /// ```rust |
| 20 | + /// let v = vec![std::sync::Arc::new("some data".to_string()); 100]; |
| 21 | + /// ``` |
| 22 | + /// Use instead: |
| 23 | + /// ```rust |
| 24 | + /// let data = std::sync::Arc::new("some data".to_string()); |
| 25 | + /// let v = vec![data; 100]; |
| 26 | + /// ``` |
| 27 | + #[clippy::version = "1.62.0"] |
| 28 | + pub ARC_NEW_IN_VEC_FROM_SLICE, |
| 29 | + suspicious, |
| 30 | + "default lint description" |
| 31 | +} |
| 32 | +declare_lint_pass!(ArcNewInVecFromSlice => [ARC_NEW_IN_VEC_FROM_SLICE]); |
| 33 | + |
| 34 | +impl LateLintPass<'_> for ArcNewInVecFromSlice { |
| 35 | + fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { |
| 36 | + let Some(macro_call) = root_macro_call_first_node(cx, expr) else { return; }; |
| 37 | + if !macro_is_vec(cx, ¯o_call) { return; } |
| 38 | + |
| 39 | + if_chain! { |
| 40 | + if let ExprKind::Call(func, args) = expr.peel_drop_temps().kind; |
| 41 | + if let ExprKind::Path(QPath::Resolved(_ty, ref path)) = func.kind; |
| 42 | + if let Some(did) = path.res.opt_def_id(); |
| 43 | + then { |
| 44 | + if !(match_def_path(cx, did, &paths::VEC_FROM_ELEM) && first_arg_is_arc_new(args)) { return; } |
| 45 | + |
| 46 | + span_lint_and_help( |
| 47 | + cx, |
| 48 | + ARC_NEW_IN_VEC_FROM_SLICE, |
| 49 | + macro_call.span, |
| 50 | + "calling `Arc::new` in `vec![elem; len]`", |
| 51 | + None, |
| 52 | + "consider extracting `Arc` initialization to a variable", |
| 53 | + ); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +fn macro_is_vec(cx: &LateContext<'_>, macro_call: &MacroCall) -> bool { |
| 60 | + cx.tcx.is_diagnostic_item(sym::vec_macro, macro_call.def_id) |
| 61 | +} |
| 62 | + |
| 63 | +fn first_arg_is_arc_new(args: &[Expr<'_>]) -> bool { |
| 64 | + let Some(first_arg) = args.get(0) else { return false; }; |
| 65 | + if_chain! { |
| 66 | + if let ExprKind::Call(func, _args) = first_arg.kind; |
| 67 | + if let ExprKind::Path(ref func_path) = func.kind; |
| 68 | + if let ExprKind::Path(QPath::TypeRelative(ty, _)) = func.kind; |
| 69 | + if let TyKind::Path(ref ty_path) = ty.kind; |
| 70 | + let func_segment = last_path_segment(func_path); |
| 71 | + let ty_segment = last_path_segment(ty_path); |
| 72 | + |
| 73 | + then { |
| 74 | + return ty_segment.ident.name.as_str() == "Arc" && func_segment.ident.name.as_str() == "new"; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + false |
| 79 | +} |
0 commit comments