diff --git a/clippy_lints/src/types/borrowed_box.rs b/clippy_lints/src/types/borrowed_box.rs index acdf54710691..bade7d08f3bc 100644 --- a/clippy_lints/src/types/borrowed_box.rs +++ b/clippy_lints/src/types/borrowed_box.rs @@ -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; @@ -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(); diff --git a/tests/ui/borrow_box.rs b/tests/ui/borrow_box.rs index 3b5b6bf4c950..8f733ca695d9 100644 --- a/tests/ui/borrow_box.rs +++ b/tests/ui/borrow_box.rs @@ -104,6 +104,16 @@ pub fn test19<'a>(_display: &'a Box) {} // 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 { + unimplemented!(); +} + +// Don't lint (not a reference) +pub fn test22() -> Box { + unimplemented!(); +} + fn main() { test1(&mut Box::new(false)); test2();