Skip to content

Commit 2fe9d4b

Browse files
committed
option_option: split part of diagnostic message into help message
1 parent 3e1f862 commit 2fe9d4b

File tree

2 files changed

+49
-16
lines changed

2 files changed

+49
-16
lines changed

clippy_lints/src/types/option_option.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use clippy_utils::diagnostics::span_lint;
1+
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::qpath_generic_tys;
33
use clippy_utils::res::MaybeResPath;
4+
use clippy_utils::source::snippet;
45
use rustc_hir::def_id::DefId;
56
use rustc_hir::{self as hir, QPath};
67
use rustc_lint::LateContext;
@@ -13,12 +14,21 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
1314
&& let Some(arg) = qpath_generic_tys(qpath).next()
1415
&& arg.basic_res().opt_def_id() == Some(def_id)
1516
{
16-
span_lint(
17+
span_lint_and_then(
1718
cx,
1819
OPTION_OPTION,
1920
hir_ty.span,
20-
"consider using `Option<T>` instead of `Option<Option<T>>` or a custom \
21-
enum if you need to distinguish all 3 cases",
21+
// use just `T` here, as the inner type is not what's problematic
22+
"use of `Option<Option<T>>`",
23+
|diag| {
24+
// but use the specific type here, as:
25+
// - this is kind of a suggestion
26+
// - it's printed right after the linted type
27+
let inner_opt = snippet(cx, arg.span, "_");
28+
diag.help(format!(
29+
"consider using `{inner_opt}`, or a custom enum if you need to distinguish all 3 cases"
30+
));
31+
},
2232
);
2333
true
2434
} else {

tests/ui/option_option.stderr

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,100 @@
1-
error: consider using `Option<T>` instead of `Option<Option<T>>` or a custom enum if you need to distinguish all 3 cases
1+
error: use of `Option<Option<T>>`
22
--> tests/ui/option_option.rs:4:10
33
|
44
LL | const C: Option<Option<i32>> = None;
55
| ^^^^^^^^^^^^^^^^^^^
66
|
7+
= help: consider using `Option<i32>`, or a custom enum if you need to distinguish all 3 cases
78
= note: `-D clippy::option-option` implied by `-D warnings`
89
= help: to override `-D warnings` add `#[allow(clippy::option_option)]`
910

10-
error: consider using `Option<T>` instead of `Option<Option<T>>` or a custom enum if you need to distinguish all 3 cases
11+
error: use of `Option<Option<T>>`
1112
--> tests/ui/option_option.rs:6:11
1213
|
1314
LL | static S: Option<Option<i32>> = None;
1415
| ^^^^^^^^^^^^^^^^^^^
16+
|
17+
= help: consider using `Option<i32>`, or a custom enum if you need to distinguish all 3 cases
1518

16-
error: consider using `Option<T>` instead of `Option<Option<T>>` or a custom enum if you need to distinguish all 3 cases
19+
error: use of `Option<Option<T>>`
1720
--> tests/ui/option_option.rs:9:13
1821
|
1922
LL | fn input(_: Option<Option<u8>>) {}
2023
| ^^^^^^^^^^^^^^^^^^
24+
|
25+
= help: consider using `Option<u8>`, or a custom enum if you need to distinguish all 3 cases
2126

22-
error: consider using `Option<T>` instead of `Option<Option<T>>` or a custom enum if you need to distinguish all 3 cases
27+
error: use of `Option<Option<T>>`
2328
--> tests/ui/option_option.rs:12:16
2429
|
2530
LL | fn output() -> Option<Option<u8>> {
2631
| ^^^^^^^^^^^^^^^^^^
32+
|
33+
= help: consider using `Option<u8>`, or a custom enum if you need to distinguish all 3 cases
2734

28-
error: consider using `Option<T>` instead of `Option<Option<T>>` or a custom enum if you need to distinguish all 3 cases
35+
error: use of `Option<Option<T>>`
2936
--> tests/ui/option_option.rs:17:27
3037
|
3138
LL | fn output_nested() -> Vec<Option<Option<u8>>> {
3239
| ^^^^^^^^^^^^^^^^^^
40+
|
41+
= help: consider using `Option<u8>`, or a custom enum if you need to distinguish all 3 cases
3342

34-
error: consider using `Option<T>` instead of `Option<Option<T>>` or a custom enum if you need to distinguish all 3 cases
43+
error: use of `Option<Option<T>>`
3544
--> tests/ui/option_option.rs:23:30
3645
|
3746
LL | fn output_nested_nested() -> Option<Option<Option<u8>>> {
3847
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
48+
|
49+
= help: consider using `Option<Option<u8>>`, or a custom enum if you need to distinguish all 3 cases
3950

40-
error: consider using `Option<T>` instead of `Option<Option<T>>` or a custom enum if you need to distinguish all 3 cases
51+
error: use of `Option<Option<T>>`
4152
--> tests/ui/option_option.rs:29:8
4253
|
4354
LL | x: Option<Option<u8>>,
4455
| ^^^^^^^^^^^^^^^^^^
56+
|
57+
= help: consider using `Option<u8>`, or a custom enum if you need to distinguish all 3 cases
4558

46-
error: consider using `Option<T>` instead of `Option<Option<T>>` or a custom enum if you need to distinguish all 3 cases
59+
error: use of `Option<Option<T>>`
4760
--> tests/ui/option_option.rs:34:23
4861
|
4962
LL | fn struct_fn() -> Option<Option<u8>> {
5063
| ^^^^^^^^^^^^^^^^^^
64+
|
65+
= help: consider using `Option<u8>`, or a custom enum if you need to distinguish all 3 cases
5166

52-
error: consider using `Option<T>` instead of `Option<Option<T>>` or a custom enum if you need to distinguish all 3 cases
67+
error: use of `Option<Option<T>>`
5368
--> tests/ui/option_option.rs:41:22
5469
|
5570
LL | fn trait_fn() -> Option<Option<u8>>;
5671
| ^^^^^^^^^^^^^^^^^^
72+
|
73+
= help: consider using `Option<u8>`, or a custom enum if you need to distinguish all 3 cases
5774

58-
error: consider using `Option<T>` instead of `Option<Option<T>>` or a custom enum if you need to distinguish all 3 cases
75+
error: use of `Option<Option<T>>`
5976
--> tests/ui/option_option.rs:46:11
6077
|
6178
LL | Tuple(Option<Option<u8>>),
6279
| ^^^^^^^^^^^^^^^^^^
80+
|
81+
= help: consider using `Option<u8>`, or a custom enum if you need to distinguish all 3 cases
6382

64-
error: consider using `Option<T>` instead of `Option<Option<T>>` or a custom enum if you need to distinguish all 3 cases
83+
error: use of `Option<Option<T>>`
6584
--> tests/ui/option_option.rs:48:17
6685
|
6786
LL | Struct { x: Option<Option<u8>> },
6887
| ^^^^^^^^^^^^^^^^^^
88+
|
89+
= help: consider using `Option<u8>`, or a custom enum if you need to distinguish all 3 cases
6990

70-
error: consider using `Option<T>` instead of `Option<Option<T>>` or a custom enum if you need to distinguish all 3 cases
91+
error: use of `Option<Option<T>>`
7192
--> tests/ui/option_option.rs:90:14
7293
|
7394
LL | foo: Option<Option<Cow<'a, str>>>,
7495
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
96+
|
97+
= help: consider using `Option<Cow<'a, str>>`, or a custom enum if you need to distinguish all 3 cases
7598

7699
error: aborting due to 12 previous errors
77100

0 commit comments

Comments
 (0)