Skip to content

Commit 4748d67

Browse files
committed
Add a help message
1 parent 27da9aa commit 4748d67

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

clippy_lints/src/unnecessary_box_returns.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::{diagnostics::span_lint_and_sugg, ty::implements_trait};
1+
use clippy_utils::diagnostics::span_lint_and_then;
22
use rustc_errors::Applicability;
33
use rustc_hir::{def_id::LocalDefId, FnDecl, FnRetTy, ImplItemKind, Item, ItemKind, Node, TraitItem, TraitItemKind};
44
use rustc_hir_analysis::hir_ty_to_ty;
@@ -67,15 +67,22 @@ impl UnnecessaryBoxReturns {
6767

6868
// it's sometimes useful to return Box<T> if T is unsized, so don't lint those
6969
if implements_trait(cx, boxed_ty, sized_trait, &[]) {
70-
span_lint_and_sugg(
70+
span_lint_and_then(
7171
cx,
7272
UNNECESSARY_BOX_RETURNS,
7373
return_ty_hir.span,
7474
format!("boxed return of the sized type `{boxed_ty}`").as_str(),
75-
"try",
76-
boxed_ty.to_string(),
77-
// the return value and function callers also needs to be changed, so this can't be MachineApplicable
78-
Applicability::Unspecified,
75+
|diagnostic| {
76+
diagnostic.span_suggestion(
77+
return_ty_hir.span,
78+
"try",
79+
boxed_ty.to_string(),
80+
// the return value and function callers also needs to
81+
// be changed, so this can't be MachineApplicable
82+
Applicability::Unspecified,
83+
);
84+
diagnostic.help("changing this also requires a change to the return expressions in this function");
85+
},
7986
);
8087
}
8188
}

tests/ui/unnecessary_box_returns.stderr

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,32 @@ error: boxed return of the sized type `usize`
44
LL | fn baz(&self) -> Box<usize>;
55
| ^^^^^^^^^^ help: try: `usize`
66
|
7+
= help: changing this also requires a change to the return expressions in this function
78
= note: `-D clippy::unnecessary-box-returns` implied by `-D warnings`
89

910
error: boxed return of the sized type `usize`
1011
--> $DIR/unnecessary_box_returns.rs:18:22
1112
|
1213
LL | fn baz(&self) -> Box<usize> {
1314
| ^^^^^^^^^^ help: try: `usize`
15+
|
16+
= help: changing this also requires a change to the return expressions in this function
1417

1518
error: boxed return of the sized type `usize`
1619
--> $DIR/unnecessary_box_returns.rs:25:21
1720
|
1821
LL | fn boxed_usize() -> Box<usize> {
1922
| ^^^^^^^^^^ help: try: `usize`
23+
|
24+
= help: changing this also requires a change to the return expressions in this function
2025

2126
error: boxed return of the sized type `Foo`
2227
--> $DIR/unnecessary_box_returns.rs:30:20
2328
|
2429
LL | fn _boxed_foo() -> Box<Foo> {
2530
| ^^^^^^^^ help: try: `Foo`
31+
|
32+
= help: changing this also requires a change to the return expressions in this function
2633

2734
error: aborting due to 4 previous errors
2835

0 commit comments

Comments
 (0)