-
Notifications
You must be signed in to change notification settings - Fork 2k
Add Vec<T> into Arc/Rc<[T]> lint #16790
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
landaire
wants to merge
9
commits into
rust-lang:master
Choose a base branch
from
landaire-contrib:push-ywunxyxovqyn
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6edadc5
Add Vec<T> into Arc/Rc<[T]> lint
landaire 578e8d2
make Vec<T> into {wrapper}<[T]> lint an autofix lint
landaire 24fcb66
address unfixiable lints by moving them to `vec_to_rc_slice_unfixable…
landaire 77d1c54
add support for rewriting Arc<[T]> into Arc<Box<[T]>> for more fixabl…
landaire 4db5b40
fix var name failure
landaire 6f2e208
fix accidentally rolled back changes + update lint text
landaire 41904d2
don't focus on into_boxed_slice() in error message
landaire bf4b9f7
reduce work done before applying lint + reduce control flow
landaire a6eec8f
make intent of `inner_slice_span` more clear/better code locality
landaire File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| use clippy_utils::diagnostics::span_lint_and_then; | ||
| use clippy_utils::res::{MaybeDef, MaybeResPath, MaybeTypeckRes}; | ||
| use clippy_utils::source::snippet_with_context; | ||
| use clippy_utils::{qpath_generic_tys, sym}; | ||
| use rustc_errors::Applicability; | ||
| use rustc_hir::{Expr, ExprKind, Node, TyKind}; | ||
| use rustc_lint::{LateContext, LateLintPass}; | ||
| use rustc_middle::ty::{self, Ty}; | ||
| use rustc_session::declare_lint_pass; | ||
| use rustc_span::Span; | ||
|
|
||
| declare_clippy_lint! { | ||
| /// ### What it does | ||
| /// Detects conversions from `Vec<T>` to `Arc<[T]>` or `Rc<[T]>` via `.into()`, | ||
| /// `Arc::from()`, `Rc::from()`, or `From::from()`. | ||
| /// | ||
| /// ### Why is this bad? | ||
| /// `Arc<[T]>` and `Rc<[T]>` store the reference count and slice data in a single | ||
| /// contiguous allocation. Because `Vec<T>` uses a separate heap allocation with a | ||
| /// different layout, converting `Vec<T>` into `Arc<[T]>` or `Rc<[T]>` must allocate | ||
| /// a new block and **copy** all elements. For large vectors this copy can be expensive. | ||
| /// | ||
| /// Using `Arc<Box<[T]>>` (or `Rc<Box<[T]>>`) avoids the copy: | ||
| /// `Vec::into_boxed_slice()` can reuse the existing allocation (shrinking if needed), | ||
| /// and wrapping the resulting `Box<[T]>` in an `Arc`/`Rc` is a cheap pointer-sized | ||
| /// allocation. | ||
| /// | ||
| /// The trade-off is one extra level of indirection when accessing the data. | ||
| /// | ||
| /// ### Example | ||
| /// ```no_run | ||
| /// # use std::sync::Arc; | ||
| /// let v: Vec<u8> = vec![1, 2, 3]; | ||
| /// let a: Arc<[u8]> = v.into(); | ||
| /// ``` | ||
| /// Use instead: | ||
| /// ```no_run | ||
| /// # use std::sync::Arc; | ||
| /// let v: Vec<u8> = vec![1, 2, 3]; | ||
| /// let a: Arc<Box<[u8]>> = Arc::new(v.into_boxed_slice()); | ||
| /// ``` | ||
| #[clippy::version = "1.97.0"] | ||
| pub VEC_TO_RC_SLICE, | ||
| perf, | ||
| "converting `Vec<T>` to `Arc<[T]>` or `Rc<[T]>` copies all elements to a new allocation" | ||
| } | ||
|
|
||
| declare_lint_pass!(VecToRcSlice => [VEC_TO_RC_SLICE]); | ||
|
|
||
| /// Checks if `ty` is `Vec<T>` for some `T`. | ||
| fn is_vec(cx: &LateContext<'_>, ty: Ty<'_>) -> bool { | ||
| if let ty::Adt(adt, _) = ty.kind() { | ||
| cx.tcx.is_diagnostic_item(sym::Vec, adt.did()) | ||
| } else { | ||
| false | ||
| } | ||
| } | ||
|
|
||
| /// If `ty` is `Arc<[T]>` or `Rc<[T]>`, returns the wrapper name (`"Arc"` or `"Rc"`). | ||
| fn rc_slice_wrapper(cx: &LateContext<'_>, ty: Ty<'_>) -> Option<&'static str> { | ||
| if let ty::Adt(adt, args) = ty.kind() | ||
| && let Some(inner) = args.types().next() | ||
| && inner.is_slice() | ||
| { | ||
| match cx.tcx.get_diagnostic_name(adt.did()) { | ||
| Some(sym::Arc) => Some("Arc"), | ||
| Some(sym::Rc) => Some("Rc"), | ||
| _ => None, | ||
| } | ||
| } else { | ||
| None | ||
| } | ||
| } | ||
|
|
||
| /// If the expression is the init of a `let` statement with a type annotation like | ||
| /// `Arc<[T]>` or `Rc<[T]>`, returns the span of the inner slice type (e.g. `[T]`). | ||
| fn let_ty_inner_slice_span(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<Span> { | ||
| if let Node::LetStmt(let_stmt) = cx.tcx.parent_hir_node(expr.hir_id) | ||
| && let Some(hir_ty) = let_stmt.ty | ||
| && let TyKind::Path(ref qpath) = hir_ty.kind | ||
| && let Some(def_id) = hir_ty.basic_res().opt_def_id() | ||
| && matches!(cx.tcx.get_diagnostic_name(def_id), Some(sym::Arc | sym::Rc)) | ||
| && let Some(inner_ty) = qpath_generic_tys(qpath).next() | ||
| && matches!(inner_ty.kind, TyKind::Slice(_)) | ||
| { | ||
| Some(inner_ty.span) | ||
| } else { | ||
| None | ||
| } | ||
| } | ||
|
|
||
| impl<'tcx> LateLintPass<'tcx> for VecToRcSlice { | ||
| fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) { | ||
| if e.span.from_expansion() { | ||
| return; | ||
| } | ||
|
|
||
| match e.kind { | ||
| // `vec_expr.into()` | ||
| ExprKind::MethodCall(name, recv, [], _) => { | ||
| if name.ident.name == sym::into | ||
| && cx.ty_based_def(e).opt_parent(cx).is_diag_item(cx, sym::Into) | ||
| && is_vec(cx, cx.typeck_results().expr_ty(recv)) | ||
| && let Some(wrapper) = rc_slice_wrapper(cx, cx.typeck_results().expr_ty(e)) | ||
| { | ||
| emit_lint(cx, e, recv, wrapper); | ||
| } | ||
| }, | ||
|
|
||
| // `Arc::from(vec)` / `Rc::from(vec)` / `From::from(vec)` | ||
| ExprKind::Call(path, [arg]) => { | ||
| if let ExprKind::Path(ref qpath) = path.kind | ||
| && let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id() | ||
| && cx.tcx.is_diagnostic_item(sym::from_fn, def_id) | ||
| && is_vec(cx, cx.typeck_results().expr_ty(arg)) | ||
| && let Some(wrapper) = rc_slice_wrapper(cx, cx.typeck_results().expr_ty(e)) | ||
| { | ||
| emit_lint(cx, e, arg, wrapper); | ||
| } | ||
| }, | ||
|
|
||
| _ => {}, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn emit_lint(cx: &LateContext<'_>, expr: &Expr<'_>, vec_expr: &Expr<'_>, wrapper: &str) { | ||
| span_lint_and_then( | ||
| cx, | ||
| VEC_TO_RC_SLICE, | ||
| expr.span, | ||
| format!("converting a `Vec<T>` to `{wrapper}<[T]>` copies all elements to a new allocation"), | ||
| |diag| { | ||
| let mut app = Applicability::MaybeIncorrect; | ||
| let ctxt = expr.span.ctxt(); | ||
|
|
||
| // Common case: we have a `Vec` that we can call `into_boxed_slice()` on. Generate the snippet | ||
| // which does so with the appropriate span to identify the Vec and constructs the target (wrapper) | ||
| // type from it too. | ||
| // | ||
| // e.g. `Rc::from(vec)` -> `Rc::new(vec.into_boxed_slice())` | ||
| let vec_snippet = snippet_with_context(cx, vec_expr.span, ctxt, "<vec>", &mut app).0; | ||
| let expr_sugg = format!("{wrapper}::new({vec_snippet}.into_boxed_slice())"); | ||
| let mut sugg = vec![(expr.span, expr_sugg)]; | ||
|
|
||
| // if `expr` is part of a let stmt with a type ascription, also fix the latter as part of the | ||
| // suggestion. | ||
| let inner_slice_span = let_ty_inner_slice_span(cx, expr); | ||
| if let Some(ty_span) = inner_slice_span { | ||
| let slice_snippet = snippet_with_context(cx, ty_span, ctxt, "_", &mut app).0; | ||
| sugg.push((ty_span, format!("Box<{slice_snippet}>"))); | ||
| } | ||
|
ada4a marked this conversation as resolved.
|
||
|
|
||
| diag.multipart_suggestion(format!("convert target type to `{wrapper}<Box<[T]>>`"), sugg, app); | ||
| }, | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| #![warn(clippy::vec_to_rc_slice)] | ||
|
|
||
| use std::rc::Rc; | ||
| use std::sync::Arc; | ||
|
|
||
| fn main() { | ||
| // Should lint: Vec<T>.into() -> Arc<[T]> | ||
| let v: Vec<u8> = vec![1, 2, 3]; | ||
| let _a: Arc<Box<[u8]>> = Arc::new(v.into_boxed_slice()); | ||
| //~^ vec_to_rc_slice | ||
|
|
||
| // Should lint: Arc::from(vec) | ||
| let v: Vec<u8> = vec![1, 2, 3]; | ||
| let _a: Arc<Box<[u8]>> = Arc::new(v.into_boxed_slice()); | ||
| //~^ vec_to_rc_slice | ||
|
|
||
| // Should lint: From::from(vec) when target is Arc<[T]> | ||
| let v: Vec<u8> = vec![1, 2, 3]; | ||
| let _a: Arc<Box<[u8]>> = Arc::new(v.into_boxed_slice()); | ||
| //~^ vec_to_rc_slice | ||
|
|
||
| // Should lint: fully qualified Arc from | ||
| let v: Vec<u8> = vec![1, 2, 3]; | ||
| let _a = Arc::new(v.into_boxed_slice()); | ||
| //~^ vec_to_rc_slice | ||
|
|
||
| // Should lint: Vec<T>.into() -> Rc<[T]> | ||
| let v: Vec<u8> = vec![1, 2, 3]; | ||
| let _a: Rc<Box<[u8]>> = Rc::new(v.into_boxed_slice()); | ||
| //~^ vec_to_rc_slice | ||
|
|
||
| // Should lint: Rc::from(vec) | ||
| let v: Vec<u8> = vec![1, 2, 3]; | ||
| let _a: Rc<Box<[u8]>> = Rc::new(v.into_boxed_slice()); | ||
| //~^ vec_to_rc_slice | ||
|
|
||
| // Should lint: From::from(vec) when target is Rc<[T]> | ||
| let v: Vec<u8> = vec![1, 2, 3]; | ||
| let _a: Rc<Box<[u8]>> = Rc::new(v.into_boxed_slice()); | ||
| //~^ vec_to_rc_slice | ||
|
|
||
| // Should lint: fully qualified Rc from | ||
| let v: Vec<u8> = vec![1, 2, 3]; | ||
| let _a = Rc::new(v.into_boxed_slice()); | ||
| //~^ vec_to_rc_slice | ||
|
|
||
| // Should NOT lint: Vec<T>.into() -> something else | ||
| let v: Vec<u8> = vec![1, 2, 3]; | ||
| let _b: Box<[u8]> = v.into(); | ||
|
|
||
| // Should NOT lint: non-Vec into Arc<[T]> | ||
| let _c: Arc<[u8]> = Arc::from([1u8, 2, 3].as_slice()); | ||
|
|
||
| // Should NOT lint: Vec into Arc (not Arc<[T]>) | ||
| let v: Vec<u8> = vec![1, 2, 3]; | ||
| let _d: Arc<Vec<u8>> = Arc::new(v); | ||
|
|
||
| // Should NOT lint: the recommended pattern | ||
| let v: Vec<u8> = vec![1, 2, 3]; | ||
| let _e: Arc<Box<[u8]>> = Arc::new(v.into_boxed_slice()); | ||
|
|
||
| // Should NOT lint: Rc recommended pattern | ||
| let v: Vec<u8> = vec![1, 2, 3]; | ||
| let _f: Rc<Box<[u8]>> = Rc::new(v.into_boxed_slice()); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| #![warn(clippy::vec_to_rc_slice)] | ||
|
|
||
| use std::rc::Rc; | ||
| use std::sync::Arc; | ||
|
|
||
| fn main() { | ||
| // Should lint: Vec<T>.into() -> Arc<[T]> | ||
| let v: Vec<u8> = vec![1, 2, 3]; | ||
| let _a: Arc<[u8]> = v.into(); | ||
| //~^ vec_to_rc_slice | ||
|
|
||
| // Should lint: Arc::from(vec) | ||
| let v: Vec<u8> = vec![1, 2, 3]; | ||
| let _a: Arc<[u8]> = Arc::from(v); | ||
| //~^ vec_to_rc_slice | ||
|
|
||
| // Should lint: From::from(vec) when target is Arc<[T]> | ||
| let v: Vec<u8> = vec![1, 2, 3]; | ||
| let _a: Arc<[u8]> = From::from(v); | ||
| //~^ vec_to_rc_slice | ||
|
|
||
| // Should lint: fully qualified Arc from | ||
| let v: Vec<u8> = vec![1, 2, 3]; | ||
| let _a = <Arc<[u8]> as From<Vec<u8>>>::from(v); | ||
| //~^ vec_to_rc_slice | ||
|
|
||
| // Should lint: Vec<T>.into() -> Rc<[T]> | ||
| let v: Vec<u8> = vec![1, 2, 3]; | ||
| let _a: Rc<[u8]> = v.into(); | ||
| //~^ vec_to_rc_slice | ||
|
|
||
| // Should lint: Rc::from(vec) | ||
| let v: Vec<u8> = vec![1, 2, 3]; | ||
| let _a: Rc<[u8]> = Rc::from(v); | ||
| //~^ vec_to_rc_slice | ||
|
|
||
| // Should lint: From::from(vec) when target is Rc<[T]> | ||
| let v: Vec<u8> = vec![1, 2, 3]; | ||
| let _a: Rc<[u8]> = From::from(v); | ||
| //~^ vec_to_rc_slice | ||
|
|
||
| // Should lint: fully qualified Rc from | ||
| let v: Vec<u8> = vec![1, 2, 3]; | ||
| let _a = <Rc<[u8]> as From<Vec<u8>>>::from(v); | ||
| //~^ vec_to_rc_slice | ||
|
|
||
| // Should NOT lint: Vec<T>.into() -> something else | ||
| let v: Vec<u8> = vec![1, 2, 3]; | ||
| let _b: Box<[u8]> = v.into(); | ||
|
|
||
| // Should NOT lint: non-Vec into Arc<[T]> | ||
| let _c: Arc<[u8]> = Arc::from([1u8, 2, 3].as_slice()); | ||
|
|
||
| // Should NOT lint: Vec into Arc (not Arc<[T]>) | ||
| let v: Vec<u8> = vec![1, 2, 3]; | ||
| let _d: Arc<Vec<u8>> = Arc::new(v); | ||
|
|
||
| // Should NOT lint: the recommended pattern | ||
| let v: Vec<u8> = vec![1, 2, 3]; | ||
| let _e: Arc<Box<[u8]>> = Arc::new(v.into_boxed_slice()); | ||
|
|
||
| // Should NOT lint: Rc recommended pattern | ||
| let v: Vec<u8> = vec![1, 2, 3]; | ||
| let _f: Rc<Box<[u8]>> = Rc::new(v.into_boxed_slice()); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.