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<T>` where T is a collection such as 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 /// Collections already keeps their contents in a separate area on
29 /// the heap. So if you `Box` them, you just add another level of indirection
30 /// without any benefit whatsoever.
35 /// values: Box<Vec<Foo>>,
46 #[clippy::version = "1.57.0"]
49 "usage of `Box<Vec<T>>`, vector elements are already on the heap"
52 declare_clippy_lint! {
54 /// Checks for use of `Vec<Box<T>>` where T: Sized anywhere in the code.
55 /// Check the [Box documentation](https://doc.rust-lang.org/std/boxed/index.html) for more information.
57 /// ### Why is this bad?
58 /// `Vec` already keeps its contents in a separate area on
59 /// the heap. So if you `Box` its contents, you just add another level of indirection.
61 /// ### Known problems
62 /// Vec<Box<T: Sized>> makes sense if T is a large type (see [#3530](https://github.com/rust-lang/rust-clippy/issues/3530),
68 /// values: Vec<Box<i32>>,
79 #[clippy::version = "1.33.0"]
82 "usage of `Vec<Box<T>>` where T: Sized, vector elements are already on the heap"
85 declare_clippy_lint! {
87 /// Checks for use of `Option<Option<_>>` in function signatures and type
90 /// ### Why is this bad?
91 /// `Option<_>` represents an optional value. `Option<Option<_>>`
92 /// represents an optional optional value which is logically the same thing as an optional
93 /// value but has an unneeded extra level of wrapping.
95 /// If you have a case where `Some(Some(_))`, `Some(None)` and `None` are distinct cases,
96 /// consider a custom `enum` instead, with clear names for each case.
100 /// fn get_data() -> Option<Option<u32>> {
108 /// pub enum Contents {
109 /// Data(Vec<u8>), // Was Some(Some(Vec<u8>))
110 /// NotYetFetched, // Was Some(None)
111 /// None, // Was None
114 /// fn get_data() -> Contents {
118 #[clippy::version = "pre 1.29.0"]
121 "usage of `Option<Option<T>>`"
124 declare_clippy_lint! {
126 /// Checks for usage of any `LinkedList`, suggesting to use a
127 /// `Vec` or a `VecDeque` (formerly called `RingBuf`).
129 /// ### Why is this bad?
132 /// > The TL;DR of `LinkedList` is that it's built on a massive amount of
133 /// pointers and indirection.
134 /// > It wastes memory, it has terrible cache locality, and is all-around slow.
136 /// > "only" amortized for push/pop, should be faster in the general case for
137 /// almost every possible
138 /// > workload, and isn't even amortized at all if you can predict the capacity
141 /// > `LinkedList`s are only really good if you're doing a lot of merging or
142 /// splitting of lists.
143 /// > This is because they can just mangle some pointers instead of actually
144 /// copying the data. Even
145 /// > if you're doing a lot of insertion in the middle of the list, `RingBuf`
146 /// can still be better
147 /// > because of how expensive it is to seek to the middle of a `LinkedList`.
149 /// ### Known problems
150 /// False positives – the instances where using a
151 /// `LinkedList` makes sense are few and far between, but they can still happen.
155 /// # use std::collections::LinkedList;
156 /// let x: LinkedList<usize> = LinkedList::new();
158 #[clippy::version = "pre 1.29.0"]
161 "usage of LinkedList, usually a vector is faster, or a more specialized data structure like a `VecDeque`"
164 declare_clippy_lint! {
166 /// Checks for use of `&Box<T>` anywhere in the code.
167 /// Check the [Box documentation](https://doc.rust-lang.org/std/boxed/index.html) for more information.
169 /// ### Why is this bad?
170 /// A `&Box<T>` parameter requires the function caller to box `T` first before passing it to a function.
171 /// Using `&T` defines a concrete type for the parameter and generalizes the function, this would also
172 /// auto-deref to `&T` at the function call site if passed a `&Box<T>`.
176 /// fn foo(bar: &Box<T>) { ... }
182 /// fn foo(bar: &T) { ... }
184 #[clippy::version = "pre 1.29.0"]
187 "a borrow of a boxed type"
190 declare_clippy_lint! {
192 /// Checks for use of redundant allocations anywhere in the code.
194 /// ### Why is this bad?
195 /// Expressions such as `Rc<&T>`, `Rc<Rc<T>>`, `Rc<Arc<T>>`, `Rc<Box<T>>`, `Arc<&T>`, `Arc<Rc<T>>`,
196 /// `Arc<Arc<T>>`, `Arc<Box<T>>`, `Box<&T>`, `Box<Rc<T>>`, `Box<Arc<T>>`, `Box<Box<T>>`, add an unnecessary level of indirection.
200 /// # use std::rc::Rc;
201 /// fn foo(bar: Rc<&usize>) {}
207 /// fn foo(bar: &usize) {}
209 #[clippy::version = "1.44.0"]
210 pub REDUNDANT_ALLOCATION,
212 "redundant allocation"
215 declare_clippy_lint! {
217 /// Checks for `Rc<T>` and `Arc<T>` when `T` is a mutable buffer type such as `String` or `Vec`.
219 /// ### Why is this bad?
220 /// Expressions such as `Rc<String>` usually have no advantage over `Rc<str>`, since
221 /// it is larger and involves an extra level of indirection, and doesn't implement `Borrow<str>`.
223 /// While mutating a buffer type would still be possible with `Rc::get_mut()`, it only
224 /// works if there are no additional references yet, which usually defeats the purpose of
225 /// enclosing it in a shared ownership type. Instead, additionally wrapping the inner
226 /// type with an interior mutable container (such as `RefCell` or `Mutex`) would normally
229 /// ### Known problems
230 /// This pattern can be desirable to avoid the overhead of a `RefCell` or `Mutex` for
231 /// cases where mutation only happens before there are any additional references.
235 /// # use std::rc::Rc;
236 /// fn foo(interned: Rc<String>) { ... }
242 /// fn foo(interned: Rc<str>) { ... }
244 #[clippy::version = "1.48.0"]
247 "shared ownership of a buffer type"
250 declare_clippy_lint! {
252 /// Checks for types used in structs, parameters and `let`
253 /// declarations above a certain complexity threshold.
255 /// ### Why is this bad?
256 /// Too complex types make the code less readable. Consider
257 /// using a `type` definition to simplify them.
261 /// # use std::rc::Rc;
263 /// inner: Rc<Vec<Vec<Box<(u32, u32, u32, u32)>>>>,
266 #[clippy::version = "pre 1.29.0"]
269 "usage of very complex types that might be better factored into `type` definitions"
272 declare_clippy_lint! {
274 /// Checks for `Rc<Mutex<T>>`.
276 /// ### Why is this bad?
277 /// `Rc` is used in single thread and `Mutex` is used in multi thread.
278 /// Consider using `Rc<RefCell<T>>` in single thread or `Arc<Mutex<T>>` in multi thread.
280 /// ### Known problems
281 /// Sometimes combining generic types can lead to the requirement that a
282 /// type use Rc in conjunction with Mutex. We must consider those cases false positives, but
283 /// alas they are quite hard to rule out. Luckily they are also rare.
288 /// use std::sync::Mutex;
289 /// fn foo(interned: Rc<Mutex<i32>>) { ... }
296 /// use std::cell::RefCell
297 /// fn foo(interned: Rc<RefCell<i32>>) { ... }
299 #[clippy::version = "1.55.0"]
302 "usage of `Rc<Mutex<T>>`"
306 vec_box_size_threshold: u64,
307 type_complexity_threshold: u64,
308 avoid_breaking_exported_api: bool,
311 impl_lint_pass!(Types => [BOX_COLLECTION, VEC_BOX, OPTION_OPTION, LINKEDLIST, BORROWED_BOX, REDUNDANT_ALLOCATION, RC_BUFFER, RC_MUTEX, TYPE_COMPLEXITY]);
313 impl<'tcx> LateLintPass<'tcx> for Types {
314 fn check_fn(&mut self, cx: &LateContext<'_>, _: FnKind<'_>, decl: &FnDecl<'_>, _: &Body<'_>, _: Span, id: HirId) {
315 let is_in_trait_impl =
316 if let Some(hir::Node::Item(item)) = cx.tcx.hir().find_by_def_id(cx.tcx.hir().get_parent_item(id)) {
317 matches!(item.kind, ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }))
322 let is_exported = cx.access_levels.is_exported(cx.tcx.hir().local_def_id(id));
330 ..CheckTyContext::default()
335 fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
336 let is_exported = cx.access_levels.is_exported(item.def_id);
339 ItemKind::Static(ty, _, _) | ItemKind::Const(ty, _) => self.check_ty(
344 ..CheckTyContext::default()
347 // functions, enums, structs, impls and traits are covered
352 fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx ImplItem<'_>) {
354 ImplItemKind::Const(ty, _) => {
355 let is_in_trait_impl = if let Some(hir::Node::Item(item)) =
356 cx.tcx.hir().find_by_def_id(cx.tcx.hir().get_parent_item(item.hir_id()))
358 matches!(item.kind, ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }))
368 ..CheckTyContext::default()
372 // Methods are covered by check_fn.
373 // Type aliases are ignored because oftentimes it's impossible to
374 // make type alias declaration in trait simpler, see #1013
375 ImplItemKind::Fn(..) | ImplItemKind::TyAlias(..) => (),
379 fn check_field_def(&mut self, cx: &LateContext<'_>, field: &hir::FieldDef<'_>) {
380 let is_exported = cx.access_levels.is_exported(cx.tcx.hir().local_def_id(field.hir_id));
387 ..CheckTyContext::default()
392 fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &TraitItem<'_>) {
393 let is_exported = cx.access_levels.is_exported(item.def_id);
395 let context = CheckTyContext {
397 ..CheckTyContext::default()
401 TraitItemKind::Const(ty, _) | TraitItemKind::Type(_, Some(ty)) => {
402 self.check_ty(cx, ty, context);
404 TraitItemKind::Fn(ref sig, _) => self.check_fn_decl(cx, sig.decl, context),
405 TraitItemKind::Type(..) => (),
409 fn check_local(&mut self, cx: &LateContext<'_>, local: &Local<'_>) {
410 if let Some(ty) = local.ty {
416 ..CheckTyContext::default()
424 pub fn new(vec_box_size_threshold: u64, type_complexity_threshold: u64, avoid_breaking_exported_api: bool) -> Self {
426 vec_box_size_threshold,
427 type_complexity_threshold,
428 avoid_breaking_exported_api,
432 fn check_fn_decl(&mut self, cx: &LateContext<'_>, decl: &FnDecl<'_>, context: CheckTyContext) {
433 // Ignore functions in trait implementations as they are usually forced by the trait definition.
435 // FIXME: idially we would like to warn *if the compicated type can be simplified*, but it's hard to
437 if context.is_in_trait_impl {
441 for input in decl.inputs {
442 self.check_ty(cx, input, context);
445 if let FnRetTy::Return(ty) = decl.output {
446 self.check_ty(cx, ty, context);
450 /// Recursively check for `TypePass` lints in the given type. Stop at the first
453 /// The parameter `is_local` distinguishes the context of the type.
454 fn check_ty(&mut self, cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, mut context: CheckTyContext) {
455 if hir_ty.span.from_expansion() {
459 // Skip trait implementations; see issue #605.
460 if context.is_in_trait_impl {
464 if !context.is_nested_call && type_complexity::check(cx, hir_ty, self.type_complexity_threshold) {
469 TyKind::Path(ref qpath) if !context.is_local => {
470 let hir_id = hir_ty.hir_id;
471 let res = cx.qpath_res(qpath, hir_id);
472 if let Some(def_id) = res.opt_def_id() {
473 if self.is_type_change_allowed(context) {
474 // All lints that are being checked in this block are guarded by
475 // the `avoid_breaking_exported_api` configuration. When adding a
476 // new lint, please also add the name to the configuration documentation
477 // in `clippy_lints::utils::conf.rs`
479 let mut triggered = false;
480 triggered |= box_collection::check(cx, hir_ty, qpath, def_id);
481 triggered |= redundant_allocation::check(cx, hir_ty, qpath, def_id);
482 triggered |= rc_buffer::check(cx, hir_ty, qpath, def_id);
483 triggered |= vec_box::check(cx, hir_ty, qpath, def_id, self.vec_box_size_threshold);
484 triggered |= option_option::check(cx, hir_ty, qpath, def_id);
485 triggered |= linked_list::check(cx, hir_ty, def_id);
486 triggered |= rc_mutex::check(cx, hir_ty, qpath, def_id);
494 QPath::Resolved(Some(ty), p) => {
495 context.is_nested_call = true;
496 self.check_ty(cx, ty, context);
497 for ty in p.segments.iter().flat_map(|seg| {
500 .map_or_else(|| [].iter(), |params| params.args.iter())
501 .filter_map(|arg| match arg {
502 GenericArg::Type(ty) => Some(ty),
506 self.check_ty(cx, ty, context);
509 QPath::Resolved(None, p) => {
510 context.is_nested_call = true;
511 for ty in p.segments.iter().flat_map(|seg| {
514 .map_or_else(|| [].iter(), |params| params.args.iter())
515 .filter_map(|arg| match arg {
516 GenericArg::Type(ty) => Some(ty),
520 self.check_ty(cx, ty, context);
523 QPath::TypeRelative(ty, seg) => {
524 context.is_nested_call = true;
525 self.check_ty(cx, ty, context);
526 if let Some(params) = seg.args {
527 for ty in params.args.iter().filter_map(|arg| match arg {
528 GenericArg::Type(ty) => Some(ty),
531 self.check_ty(cx, ty, context);
535 QPath::LangItem(..) => {},
538 TyKind::Rptr(ref lt, ref mut_ty) => {
539 context.is_nested_call = true;
540 if !borrowed_box::check(cx, hir_ty, lt, mut_ty) {
541 self.check_ty(cx, mut_ty.ty, context);
544 TyKind::Slice(ty) | TyKind::Array(ty, _) | TyKind::Ptr(MutTy { ty, .. }) => {
545 context.is_nested_call = true;
546 self.check_ty(cx, ty, context);
548 TyKind::Tup(tys) => {
549 context.is_nested_call = true;
551 self.check_ty(cx, ty, context);
558 /// This function checks if the type is allowed to change in the current context
559 /// based on the `avoid_breaking_exported_api` configuration
560 fn is_type_change_allowed(&self, context: CheckTyContext) -> bool {
561 !(context.is_exported && self.avoid_breaking_exported_api)
565 #[allow(clippy::struct_excessive_bools)]
566 #[derive(Clone, Copy, Default)]
567 struct CheckTyContext {
568 is_in_trait_impl: bool,
569 /// `true` for types on local variables.
571 /// `true` for types that are part of the public API.
573 is_nested_call: bool,