7 mod redundant_allocation;
13 use rustc_hir::intravisit::FnKind;
15 Body, FnDecl, FnRetTy, GenericArg, HirId, ImplItem, ImplItemKind, Item, ItemKind, Local, MutTy, QPath, TraitItem,
16 TraitItemKind, TyKind,
18 use rustc_lint::{LateContext, LateLintPass};
19 use rustc_session::{declare_tool_lint, impl_lint_pass};
20 use rustc_span::source_map::Span;
22 declare_clippy_lint! {
24 /// Checks for use of `Box<Vec<_>>` anywhere in the code.
25 /// Check the [Box documentation](https://doc.rust-lang.org/std/boxed/index.html) for more information.
27 /// ### Why is this bad?
28 /// `Vec` already keeps its contents in a separate area on
29 /// the heap. So if you `Box` it, you just add another level of indirection
30 /// without any benefit whatsoever.
35 /// values: Box<Vec<Foo>>,
48 "usage of `Box<Vec<T>>`, vector elements are already on the heap"
51 declare_clippy_lint! {
53 /// Checks for use of `Vec<Box<T>>` where T: Sized anywhere in the code.
54 /// Check the [Box documentation](https://doc.rust-lang.org/std/boxed/index.html) for more information.
56 /// ### Why is this bad?
57 /// `Vec` already keeps its contents in a separate area on
58 /// the heap. So if you `Box` its contents, you just add another level of indirection.
60 /// ### Known problems
61 /// Vec<Box<T: Sized>> makes sense if T is a large type (see [#3530](https://github.com/rust-lang/rust-clippy/issues/3530),
67 /// values: Vec<Box<i32>>,
80 "usage of `Vec<Box<T>>` where T: Sized, vector elements are already on the heap"
83 declare_clippy_lint! {
85 /// Checks for use of `Option<Option<_>>` in function signatures and type
88 /// ### Why is this bad?
89 /// `Option<_>` represents an optional value. `Option<Option<_>>`
90 /// represents an optional optional value which is logically the same thing as an optional
91 /// value but has an unneeded extra level of wrapping.
93 /// If you have a case where `Some(Some(_))`, `Some(None)` and `None` are distinct cases,
94 /// consider a custom `enum` instead, with clear names for each case.
98 /// fn get_data() -> Option<Option<u32>> {
106 /// pub enum Contents {
107 /// Data(Vec<u8>), // Was Some(Some(Vec<u8>))
108 /// NotYetFetched, // Was Some(None)
109 /// None, // Was None
112 /// fn get_data() -> Contents {
118 "usage of `Option<Option<T>>`"
121 declare_clippy_lint! {
123 /// Checks for usage of any `LinkedList`, suggesting to use a
124 /// `Vec` or a `VecDeque` (formerly called `RingBuf`).
126 /// ### Why is this bad?
129 /// > The TL;DR of `LinkedList` is that it's built on a massive amount of
130 /// pointers and indirection.
131 /// > It wastes memory, it has terrible cache locality, and is all-around slow.
133 /// > "only" amortized for push/pop, should be faster in the general case for
134 /// almost every possible
135 /// > workload, and isn't even amortized at all if you can predict the capacity
138 /// > `LinkedList`s are only really good if you're doing a lot of merging or
139 /// splitting of lists.
140 /// > This is because they can just mangle some pointers instead of actually
141 /// copying the data. Even
142 /// > if you're doing a lot of insertion in the middle of the list, `RingBuf`
143 /// can still be better
144 /// > because of how expensive it is to seek to the middle of a `LinkedList`.
146 /// ### Known problems
147 /// False positives – the instances where using a
148 /// `LinkedList` makes sense are few and far between, but they can still happen.
152 /// # use std::collections::LinkedList;
153 /// let x: LinkedList<usize> = LinkedList::new();
157 "usage of LinkedList, usually a vector is faster, or a more specialized data structure like a `VecDeque`"
160 declare_clippy_lint! {
162 /// Checks for use of `&Box<T>` anywhere in the code.
163 /// Check the [Box documentation](https://doc.rust-lang.org/std/boxed/index.html) for more information.
165 /// ### Why is this bad?
166 /// Any `&Box<T>` can also be a `&T`, which is more
171 /// fn foo(bar: &Box<T>) { ... }
177 /// fn foo(bar: &T) { ... }
181 "a borrow of a boxed type"
184 declare_clippy_lint! {
186 /// Checks for use of redundant allocations anywhere in the code.
188 /// ### Why is this bad?
189 /// Expressions such as `Rc<&T>`, `Rc<Rc<T>>`, `Rc<Arc<T>>`, `Rc<Box<T>>`, `Arc<&T>`, `Arc<Rc<T>>`,
190 /// `Arc<Arc<T>>`, `Arc<Box<T>>`, `Box<&T>`, `Box<Rc<T>>`, `Box<Arc<T>>`, `Box<Box<T>>`, add an unnecessary level of indirection.
194 /// # use std::rc::Rc;
195 /// fn foo(bar: Rc<&usize>) {}
201 /// fn foo(bar: &usize) {}
203 pub REDUNDANT_ALLOCATION,
205 "redundant allocation"
208 declare_clippy_lint! {
210 /// Checks for `Rc<T>` and `Arc<T>` when `T` is a mutable buffer type such as `String` or `Vec`.
212 /// ### Why is this bad?
213 /// Expressions such as `Rc<String>` usually have no advantage over `Rc<str>`, since
214 /// it is larger and involves an extra level of indirection, and doesn't implement `Borrow<str>`.
216 /// While mutating a buffer type would still be possible with `Rc::get_mut()`, it only
217 /// works if there are no additional references yet, which usually defeats the purpose of
218 /// enclosing it in a shared ownership type. Instead, additionally wrapping the inner
219 /// type with an interior mutable container (such as `RefCell` or `Mutex`) would normally
222 /// ### Known problems
223 /// This pattern can be desirable to avoid the overhead of a `RefCell` or `Mutex` for
224 /// cases where mutation only happens before there are any additional references.
228 /// # use std::rc::Rc;
229 /// fn foo(interned: Rc<String>) { ... }
235 /// fn foo(interned: Rc<str>) { ... }
239 "shared ownership of a buffer type"
242 declare_clippy_lint! {
244 /// Checks for types used in structs, parameters and `let`
245 /// declarations above a certain complexity threshold.
247 /// ### Why is this bad?
248 /// Too complex types make the code less readable. Consider
249 /// using a `type` definition to simplify them.
253 /// # use std::rc::Rc;
255 /// inner: Rc<Vec<Vec<Box<(u32, u32, u32, u32)>>>>,
260 "usage of very complex types that might be better factored into `type` definitions"
263 declare_clippy_lint! {
265 /// Checks for `Rc<Mutex<T>>`.
267 /// ### Why is this bad?
268 /// `Rc` is used in single thread and `Mutex` is used in multi thread.
269 /// Consider using `Rc<RefCell<T>>` in single thread or `Arc<Mutex<T>>` in multi thread.
271 /// ### Known problems
272 /// Sometimes combining generic types can lead to the requirement that a
273 /// type use Rc in conjunction with Mutex. We must consider those cases false positives, but
274 /// alas they are quite hard to rule out. Luckily they are also rare.
279 /// use std::sync::Mutex;
280 /// fn foo(interned: Rc<Mutex<i32>>) { ... }
287 /// use std::cell::RefCell
288 /// fn foo(interned: Rc<RefCell<i32>>) { ... }
292 "usage of `Rc<Mutex<T>>`"
296 vec_box_size_threshold: u64,
297 type_complexity_threshold: u64,
300 impl_lint_pass!(Types => [BOX_VEC, VEC_BOX, OPTION_OPTION, LINKEDLIST, BORROWED_BOX, REDUNDANT_ALLOCATION, RC_BUFFER, RC_MUTEX, TYPE_COMPLEXITY]);
302 impl<'tcx> LateLintPass<'tcx> for Types {
303 fn check_fn(&mut self, cx: &LateContext<'_>, _: FnKind<'_>, decl: &FnDecl<'_>, _: &Body<'_>, _: Span, id: HirId) {
304 let is_in_trait_impl = if let Some(hir::Node::Item(item)) = cx.tcx.hir().find(cx.tcx.hir().get_parent_item(id))
306 matches!(item.kind, ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }))
316 ..CheckTyContext::default()
321 fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
323 ItemKind::Static(ty, _, _) | ItemKind::Const(ty, _) => self.check_ty(cx, ty, CheckTyContext::default()),
324 // functions, enums, structs, impls and traits are covered
329 fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx ImplItem<'_>) {
331 ImplItemKind::Const(ty, _) | ImplItemKind::TyAlias(ty) => self.check_ty(
335 is_in_trait_impl: true,
336 ..CheckTyContext::default()
339 // methods are covered by check_fn
340 ImplItemKind::Fn(..) => (),
344 fn check_field_def(&mut self, cx: &LateContext<'_>, field: &hir::FieldDef<'_>) {
345 self.check_ty(cx, field.ty, CheckTyContext::default());
348 fn check_trait_item(&mut self, cx: &LateContext<'_>, item: &TraitItem<'_>) {
350 TraitItemKind::Const(ty, _) | TraitItemKind::Type(_, Some(ty)) => {
351 self.check_ty(cx, ty, CheckTyContext::default());
353 TraitItemKind::Fn(ref sig, _) => self.check_fn_decl(cx, sig.decl, CheckTyContext::default()),
354 TraitItemKind::Type(..) => (),
358 fn check_local(&mut self, cx: &LateContext<'_>, local: &Local<'_>) {
359 if let Some(ty) = local.ty {
365 ..CheckTyContext::default()
373 pub fn new(vec_box_size_threshold: u64, type_complexity_threshold: u64) -> Self {
375 vec_box_size_threshold,
376 type_complexity_threshold,
380 fn check_fn_decl(&mut self, cx: &LateContext<'_>, decl: &FnDecl<'_>, context: CheckTyContext) {
381 for input in decl.inputs {
382 self.check_ty(cx, input, context);
385 if let FnRetTy::Return(ty) = decl.output {
386 self.check_ty(cx, ty, context);
390 /// Recursively check for `TypePass` lints in the given type. Stop at the first
393 /// The parameter `is_local` distinguishes the context of the type.
394 fn check_ty(&mut self, cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, mut context: CheckTyContext) {
395 if hir_ty.span.from_expansion() {
399 if !context.is_nested_call && type_complexity::check(cx, hir_ty, self.type_complexity_threshold) {
403 // Skip trait implementations; see issue #605.
404 if context.is_in_trait_impl {
409 TyKind::Path(ref qpath) if !context.is_local => {
410 let hir_id = hir_ty.hir_id;
411 let res = cx.qpath_res(qpath, hir_id);
412 if let Some(def_id) = res.opt_def_id() {
413 let mut triggered = false;
414 triggered |= box_vec::check(cx, hir_ty, qpath, def_id);
415 triggered |= redundant_allocation::check(cx, hir_ty, qpath, def_id);
416 triggered |= rc_buffer::check(cx, hir_ty, qpath, def_id);
417 triggered |= vec_box::check(cx, hir_ty, qpath, def_id, self.vec_box_size_threshold);
418 triggered |= option_option::check(cx, hir_ty, qpath, def_id);
419 triggered |= linked_list::check(cx, hir_ty, def_id);
420 triggered |= rc_mutex::check(cx, hir_ty, qpath, def_id);
427 QPath::Resolved(Some(ty), p) => {
428 context.is_nested_call = true;
429 self.check_ty(cx, ty, context);
430 for ty in p.segments.iter().flat_map(|seg| {
433 .map_or_else(|| [].iter(), |params| params.args.iter())
434 .filter_map(|arg| match arg {
435 GenericArg::Type(ty) => Some(ty),
439 self.check_ty(cx, ty, context);
442 QPath::Resolved(None, p) => {
443 context.is_nested_call = true;
444 for ty in p.segments.iter().flat_map(|seg| {
447 .map_or_else(|| [].iter(), |params| params.args.iter())
448 .filter_map(|arg| match arg {
449 GenericArg::Type(ty) => Some(ty),
453 self.check_ty(cx, ty, context);
456 QPath::TypeRelative(ty, seg) => {
457 context.is_nested_call = true;
458 self.check_ty(cx, ty, context);
459 if let Some(params) = seg.args {
460 for ty in params.args.iter().filter_map(|arg| match arg {
461 GenericArg::Type(ty) => Some(ty),
464 self.check_ty(cx, ty, context);
468 QPath::LangItem(..) => {},
471 TyKind::Rptr(ref lt, ref mut_ty) => {
472 context.is_nested_call = true;
473 if !borrowed_box::check(cx, hir_ty, lt, mut_ty) {
474 self.check_ty(cx, mut_ty.ty, context);
477 TyKind::Slice(ty) | TyKind::Array(ty, _) | TyKind::Ptr(MutTy { ty, .. }) => {
478 context.is_nested_call = true;
479 self.check_ty(cx, ty, context);
481 TyKind::Tup(tys) => {
482 context.is_nested_call = true;
484 self.check_ty(cx, ty, context);
492 #[derive(Clone, Copy, Default)]
493 struct CheckTyContext {
494 is_in_trait_impl: bool,
496 is_nested_call: bool,