Skip to content

Commit aacf1cb

Browse files
committed
Add a help message
1 parent 2c642b5 commit aacf1cb

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

clippy_lints/src/unnecessary_box_returns.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::span_lint_and_sugg;
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;
@@ -66,15 +66,22 @@ impl UnnecessaryBoxReturns {
6666

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

tests/ui/unnecessary_box_returns.stderr

+7
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:24: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:29: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)