Skip to content

unnecessary_box_returns don't lint on large objects #10641

Closed
@tgross35

Description

@tgross35

Description

A lot of the reason you may Box something is if it is too large to move cheaply. For large types it wouldn't be very unusual to return a Box<T>, especially if the box existed before returning it (as in the example here) to prevent some unnecessary moves or reallocations.

It could be a good idea to not raise this lint if T is e.g. >64 bytes (or reuse the threashold for suggesting boxed enum variants).

Some relevant zulip discussion: https://rust-lang.zulipchat.com/#narrow/stream/122651-general/topic/returning.20boxed.20values

#![allow(unused)]
#![warn(clippy::pedantic)]

struct Huge([u8; 500]);
struct HasHuge(Box<Huge> /* ... */);

impl HasHuge {
  fn into_huge(self) -> Box<Huge> {
    self.0
  }
}

Version

rustc 1.68.2 stable

Additional Labels

@rustbot label +C-enhancement

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: Enhancement of lints, like adding more cases or adding help messages

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions