|
| 1 | +use clippy_utils::diagnostics::span_lint_and_help; |
| 2 | +use clippy_utils::res::MaybeDef; |
| 3 | +use clippy_utils::{eq_expr_value, sym}; |
| 4 | +use rustc_hir::{Expr, ExprKind, LangItem, QPath}; |
| 5 | +use rustc_lint::{LateContext, LateLintPass}; |
| 6 | +use rustc_session::declare_lint_pass; |
| 7 | +use rustc_span::symbol::sym as rustc_sym; |
| 8 | + |
| 9 | +declare_clippy_lint! { |
| 10 | + /// ### What it does |
| 11 | + /// |
| 12 | + /// Checks for usages of `Vec::from_raw_parts` and `String::from_raw_parts` |
| 13 | + /// where the same expression is used for the length and the capacity. |
| 14 | + /// |
| 15 | + /// ### Why is this bad? |
| 16 | + /// |
| 17 | + /// If the same expression is being passed for the length and |
| 18 | + /// capacity, it is most likely a semantic error. In the case of a |
| 19 | + /// Vec, for example, the only way to end up with one that has |
| 20 | + /// the same length and capacity is by going through a boxed slice, |
| 21 | + /// e.g. `Box::from(some_vec)`, which shrinks the capacity to match |
| 22 | + /// the length. |
| 23 | + /// |
| 24 | + /// ### Example |
| 25 | + /// |
| 26 | + /// ```no_run |
| 27 | + /// #![feature(vec_into_raw_parts)] |
| 28 | + /// let mut original: Vec::<i32> = Vec::with_capacity(20); |
| 29 | + /// original.extend([1, 2, 3, 4, 5]); |
| 30 | + /// |
| 31 | + /// let (ptr, mut len, cap) = original.into_raw_parts(); |
| 32 | + /// |
| 33 | + /// // I will add three more integers: |
| 34 | + /// unsafe { |
| 35 | + /// let ptr = ptr as *mut i32; |
| 36 | + /// |
| 37 | + /// for i in 6..9 { |
| 38 | + /// *ptr.add(i - 1) = i as i32; |
| 39 | + /// len += 1; |
| 40 | + /// } |
| 41 | + /// } |
| 42 | + /// |
| 43 | + /// // But I forgot the capacity was separate from the length: |
| 44 | + /// let reconstructed = unsafe { Vec::from_raw_parts(ptr, len, len) }; |
| 45 | + /// ``` |
| 46 | + /// |
| 47 | + /// Use instead: |
| 48 | + /// ```no_run |
| 49 | + /// #![feature(vec_into_raw_parts)] |
| 50 | + /// let mut original: Vec::<i32> = Vec::with_capacity(20); |
| 51 | + /// original.extend([1, 2, 3, 4, 5]); |
| 52 | + /// |
| 53 | + /// let (ptr, mut len, cap) = original.into_raw_parts(); |
| 54 | + /// |
| 55 | + /// // I will add three more integers: |
| 56 | + /// unsafe { |
| 57 | + /// let ptr = ptr as *mut i32; |
| 58 | + /// |
| 59 | + /// for i in 6..9 { |
| 60 | + /// *ptr.add(i - 1) = i as i32; |
| 61 | + /// len += 1; |
| 62 | + /// } |
| 63 | + /// } |
| 64 | + /// |
| 65 | + /// // This time, leverage the previously saved capacity: |
| 66 | + /// let reconstructed = unsafe { Vec::from_raw_parts(ptr, len, cap) }; |
| 67 | + /// ``` |
| 68 | + #[clippy::version = "1.93.0"] |
| 69 | + pub SAME_LENGTH_AND_CAPACITY, |
| 70 | + pedantic, |
| 71 | + "`from_raw_parts` with same length and capacity" |
| 72 | +} |
| 73 | +declare_lint_pass!(SameLengthAndCapacity => [SAME_LENGTH_AND_CAPACITY]); |
| 74 | + |
| 75 | +impl<'tcx> LateLintPass<'tcx> for SameLengthAndCapacity { |
| 76 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { |
| 77 | + if let ExprKind::Call(path_expr, args) = expr.kind |
| 78 | + && let ExprKind::Path(QPath::TypeRelative(ty, fn_path)) = path_expr.kind |
| 79 | + && fn_path.ident.name == sym::from_raw_parts |
| 80 | + && args.len() >= 3 |
| 81 | + && eq_expr_value(cx, &args[1], &args[2]) |
| 82 | + { |
| 83 | + let middle_ty = cx.typeck_results().node_type(ty.hir_id); |
| 84 | + if middle_ty.is_diag_item(cx, rustc_sym::Vec) { |
| 85 | + span_lint_and_help( |
| 86 | + cx, |
| 87 | + SAME_LENGTH_AND_CAPACITY, |
| 88 | + expr.span, |
| 89 | + "usage of `Vec::from_raw_parts` with the same expression for length and capacity", |
| 90 | + None, |
| 91 | + "try `Box::from(slice::from_raw_parts(...)).into::<Vec<_>>()`", |
| 92 | + ); |
| 93 | + } else if middle_ty.is_lang_item(cx, LangItem::String) { |
| 94 | + span_lint_and_help( |
| 95 | + cx, |
| 96 | + SAME_LENGTH_AND_CAPACITY, |
| 97 | + expr.span, |
| 98 | + "usage of `String::from_raw_parts` with the same expression for length and capacity", |
| 99 | + None, |
| 100 | + "try `String::from(str::from_utf8_unchecked(slice::from_raw_parts(...)))`", |
| 101 | + ); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments