|
| 1 | +use clippy_utils::diagnostics::span_lint_and_help; |
| 2 | +use clippy_utils::ty::{is_type_diagnostic_item, is_type_lang_item}; |
| 3 | +use clippy_utils::{SpanlessEq, 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 | + /// let mut original: Vec::<i32> = Vec::with_capacity(20); |
| 28 | + /// original.extend([1, 2, 3, 4, 5]); |
| 29 | + /// |
| 30 | + /// let (ptr, mut len, cap) = original.into_raw_parts(); |
| 31 | + /// |
| 32 | + /// // Pretend we added three more integers: |
| 33 | + /// len = 8; |
| 34 | + /// |
| 35 | + /// // But I forgot the capacity was separate from the length: |
| 36 | + /// let reconstructed = unsafe { Vec::from_raw_parts(ptr, len, len) }; |
| 37 | + /// ``` |
| 38 | + /// |
| 39 | + /// Use instead: |
| 40 | + /// ```no_run |
| 41 | + /// // Correction to the last line of the given example code: |
| 42 | + /// let reconstructed = unsafe { Vec::from_raw_parts(ptr, len, cap) }; |
| 43 | + /// ``` |
| 44 | + #[clippy::version = "1.91.0"] |
| 45 | + pub SAME_LENGTH_AND_CAPACITY, |
| 46 | + pedantic, |
| 47 | + "`from_raw_parts` with same length and capacity" |
| 48 | +} |
| 49 | +declare_lint_pass!(SameLengthAndCapacity => [SAME_LENGTH_AND_CAPACITY]); |
| 50 | + |
| 51 | +impl<'tcx> LateLintPass<'tcx> for SameLengthAndCapacity { |
| 52 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { |
| 53 | + if let ExprKind::Call(path_expr, args) = expr.kind |
| 54 | + && let ExprKind::Path(QPath::TypeRelative(ty, fn_path)) = path_expr.kind |
| 55 | + && is_type_diagnostic_item(cx, cx.typeck_results().node_type(ty.hir_id), rustc_sym::Vec) |
| 56 | + && fn_path.ident.name == sym::from_raw_parts |
| 57 | + && SpanlessEq::new(cx).eq_expr(&args[1], &args[2]) |
| 58 | + { |
| 59 | + span_lint_and_help( |
| 60 | + cx, |
| 61 | + SAME_LENGTH_AND_CAPACITY, |
| 62 | + expr.span, |
| 63 | + "usage of `Vec::from_raw_parts` with the same expression for length and capacity", |
| 64 | + None, |
| 65 | + "if the length and capacity are the same, you most likely went through a boxed slice; consider reconstructing the `Vec` using a `Box` instead, e.g. `Box::from(slice::from_raw_parts(...)).into_vec()`", |
| 66 | + ); |
| 67 | + } else if let ExprKind::Call(path_expr, args) = expr.kind |
| 68 | + && let ExprKind::Path(QPath::TypeRelative(ty, fn_path)) = path_expr.kind |
| 69 | + && is_type_lang_item(cx, cx.typeck_results().node_type(ty.hir_id), LangItem::String) |
| 70 | + && fn_path.ident.name == sym::from_raw_parts |
| 71 | + && SpanlessEq::new(cx).eq_expr(&args[1], &args[2]) |
| 72 | + { |
| 73 | + span_lint_and_help( |
| 74 | + cx, |
| 75 | + SAME_LENGTH_AND_CAPACITY, |
| 76 | + expr.span, |
| 77 | + "usage of `String::from_raw_parts` with the same expression for length and capacity", |
| 78 | + None, |
| 79 | + "if the length and capacity are the same, you most likely went through a boxed `str`; consider reconstructing the `String` using `String::from` instead, e.g. `String::from(str::from_utf8_unchecked(slice::from_raw_parts(...)))`", |
| 80 | + ); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments