|
| 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use clippy_utils::higher::VecArgs; |
| 3 | +use clippy_utils::source::snippet; |
| 4 | +use clippy_utils::visitors::for_each_expr; |
| 5 | +use rustc_ast::LitKind; |
| 6 | +use rustc_errors::Applicability; |
| 7 | +use rustc_hir::{ExprKind, Node}; |
| 8 | +use rustc_lint::{LateContext, LateLintPass}; |
| 9 | +use rustc_middle::ty::{self, ConstKind, Ty}; |
| 10 | +use rustc_session::declare_lint_pass; |
| 11 | +use rustc_span::Span; |
| 12 | + |
| 13 | +declare_clippy_lint! { |
| 14 | + /// ### What it does |
| 15 | + /// Checks for array or vec initializations which call a function or method, |
| 16 | + /// but which have a repeat count of zero. |
| 17 | + /// |
| 18 | + /// ### Why is this bad? |
| 19 | + /// Such an initialization, despite having a repeat length of 0, will still call the inner function. |
| 20 | + /// This may not be obvious and as such there may be unintended side effects in code. |
| 21 | + /// |
| 22 | + /// ### Example |
| 23 | + /// ```no_run |
| 24 | + /// fn side_effect() -> i32 { |
| 25 | + /// println!("side effect"); |
| 26 | + /// 10 |
| 27 | + /// } |
| 28 | + /// let a = [side_effect(); 0]; |
| 29 | + /// ``` |
| 30 | + /// Use instead: |
| 31 | + /// ```no_run |
| 32 | + /// fn side_effect() -> i32 { |
| 33 | + /// println!("side effect"); |
| 34 | + /// 10 |
| 35 | + /// } |
| 36 | + /// side_effect(); |
| 37 | + /// let a: [i32; 0] = []; |
| 38 | + /// ``` |
| 39 | + #[clippy::version = "1.75.0"] |
| 40 | + pub ZERO_REPEAT_SIDE_EFFECTS, |
| 41 | + suspicious, |
| 42 | + "usage of zero-sized initializations of arrays or vecs causing side effects" |
| 43 | +} |
| 44 | + |
| 45 | +declare_lint_pass!(ZeroRepeatSideEffects => [ZERO_REPEAT_SIDE_EFFECTS]); |
| 46 | + |
| 47 | +impl LateLintPass<'_> for ZeroRepeatSideEffects { |
| 48 | + fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'_ rustc_hir::Expr<'_>) { |
| 49 | + if let Some(args) = VecArgs::hir(cx, expr) |
| 50 | + && let VecArgs::Repeat(inner_expr, len) = args |
| 51 | + && let ExprKind::Lit(l) = len.kind |
| 52 | + && let LitKind::Int(i, _) = l.node |
| 53 | + && i.0 == 0 |
| 54 | + { |
| 55 | + inner_check(cx, expr, inner_expr, true); |
| 56 | + } else if let ExprKind::Repeat(inner_expr, _) = expr.kind |
| 57 | + && let ty::Array(_, cst) = cx.typeck_results().expr_ty(expr).kind() |
| 58 | + && let ConstKind::Value(ty::ValTree::Leaf(element_count)) = cst.kind() |
| 59 | + && let Ok(element_count) = element_count.try_to_target_usize(cx.tcx) |
| 60 | + && element_count == 0 |
| 61 | + { |
| 62 | + inner_check(cx, expr, inner_expr, false); |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +fn inner_check(cx: &LateContext<'_>, expr: &'_ rustc_hir::Expr<'_>, inner_expr: &'_ rustc_hir::Expr<'_>, is_vec: bool) { |
| 68 | + // check if expr is a call or has a call inside it |
| 69 | + if for_each_expr(inner_expr, |x| { |
| 70 | + if let ExprKind::Call(_, _) | ExprKind::MethodCall(_, _, _, _) = x.kind { |
| 71 | + std::ops::ControlFlow::Break(()) |
| 72 | + } else { |
| 73 | + std::ops::ControlFlow::Continue(()) |
| 74 | + } |
| 75 | + }) |
| 76 | + .is_some() |
| 77 | + { |
| 78 | + let parent_hir_node = cx.tcx.parent_hir_node(expr.hir_id); |
| 79 | + let return_type = cx.typeck_results().expr_ty(expr); |
| 80 | + |
| 81 | + if let Node::Local(l) = parent_hir_node { |
| 82 | + array_span_lint( |
| 83 | + cx, |
| 84 | + l.span, |
| 85 | + inner_expr.span, |
| 86 | + l.pat.span, |
| 87 | + Some(return_type), |
| 88 | + is_vec, |
| 89 | + false, |
| 90 | + ); |
| 91 | + } else if let Node::Expr(x) = parent_hir_node |
| 92 | + && let ExprKind::Assign(l, _, _) = x.kind |
| 93 | + { |
| 94 | + array_span_lint(cx, x.span, inner_expr.span, l.span, Some(return_type), is_vec, true); |
| 95 | + } else { |
| 96 | + span_lint_and_sugg( |
| 97 | + cx, |
| 98 | + ZERO_REPEAT_SIDE_EFFECTS, |
| 99 | + expr.span.source_callsite(), |
| 100 | + "function or method calls as the initial value in zero-sized array initializers may cause side effects", |
| 101 | + "consider using", |
| 102 | + format!( |
| 103 | + "{{ {}; {}[] as {return_type} }}", |
| 104 | + snippet(cx, inner_expr.span.source_callsite(), ".."), |
| 105 | + if is_vec { "vec!" } else { "" }, |
| 106 | + ), |
| 107 | + Applicability::Unspecified, |
| 108 | + ); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +fn array_span_lint( |
| 114 | + cx: &LateContext<'_>, |
| 115 | + expr_span: Span, |
| 116 | + func_call_span: Span, |
| 117 | + variable_name_span: Span, |
| 118 | + expr_ty: Option<Ty<'_>>, |
| 119 | + is_vec: bool, |
| 120 | + is_assign: bool, |
| 121 | +) { |
| 122 | + let has_ty = expr_ty.is_some(); |
| 123 | + |
| 124 | + span_lint_and_sugg( |
| 125 | + cx, |
| 126 | + ZERO_REPEAT_SIDE_EFFECTS, |
| 127 | + expr_span.source_callsite(), |
| 128 | + "function or method calls as the initial value in zero-sized array initializers may cause side effects", |
| 129 | + "consider using", |
| 130 | + format!( |
| 131 | + "{}; {}{}{} = {}[]{}{}", |
| 132 | + snippet(cx, func_call_span.source_callsite(), ".."), |
| 133 | + if has_ty && !is_assign { "let " } else { "" }, |
| 134 | + snippet(cx, variable_name_span.source_callsite(), ".."), |
| 135 | + if let Some(ty) = expr_ty |
| 136 | + && !is_assign |
| 137 | + { |
| 138 | + format!(": {ty}") |
| 139 | + } else { |
| 140 | + String::new() |
| 141 | + }, |
| 142 | + if is_vec { "vec!" } else { "" }, |
| 143 | + if let Some(ty) = expr_ty |
| 144 | + && is_assign |
| 145 | + { |
| 146 | + format!(" as {ty}") |
| 147 | + } else { |
| 148 | + String::new() |
| 149 | + }, |
| 150 | + if is_assign { "" } else { ";" } |
| 151 | + ), |
| 152 | + Applicability::Unspecified, |
| 153 | + ); |
| 154 | +} |
0 commit comments