Skip to content
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

borrowed_box: Ignore return types #10991

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion clippy_lints/src/types/borrowed_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clippy_utils::source::snippet;
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::{self as hir, GenericArg, GenericBounds, GenericParamKind};
use rustc_hir::{HirId, Lifetime, MutTy, Mutability, Node, QPath, TyKind};
use rustc_hir::{HirId, Item, ItemKind, Lifetime, MutTy, Mutability, Node, QPath, TyKind};
use rustc_lint::LateContext;
use rustc_span::sym;

Expand All @@ -14,6 +14,18 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, lt: &Lifetime, m
TyKind::Path(ref qpath) => {
let hir_id = mut_ty.ty.hir_id;
let def = cx.qpath_res(qpath, hir_id);
// Don't lint if it's a return type. See #10982
if let Node::Item(Item {
ident: _,
owner_id: _,
kind: ItemKind::Fn(sig, ..),
..
}) = cx.tcx.hir().get_parent(hir_ty.hir_id)
{
if hir_ty.span == sig.decl.output.span() {
return false;
}
}
if_chain! {
if let Some(def_id) = def.opt_def_id();
if Some(def_id) == cx.tcx.lang_items().owned_box();
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/borrow_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ pub fn test19<'a>(_display: &'a Box<impl Display + 'a>) {}
// it's fine that unnecessary parentheses appear in the future for some reason.
pub fn test20(_display: &Box<(dyn Display + Send)>) {}

// Don't lint
pub fn test21<'a>() -> &'a Box<u32> {
unimplemented!();
}

// Don't lint (not a reference)
pub fn test22() -> Box<u32> {
unimplemented!();
}

fn main() {
test1(&mut Box::new(false));
test2();
Expand Down