Skip to content

Commit 91dbaae

Browse files
committed
improve diagnostics
1 parent a8fa9e1 commit 91dbaae

File tree

2 files changed

+49
-15
lines changed

2 files changed

+49
-15
lines changed

clippy_lints/src/matches/match_as_ref.rs

Lines changed: 14 additions & 9 deletions
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 clippy_utils::res::{MaybeDef, MaybeQPath};
33
use clippy_utils::source::snippet_with_applicability;
44
use clippy_utils::ty::option_arg_ty;
@@ -41,17 +41,22 @@ pub(crate) fn check(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>], expr:
4141
};
4242

4343
let mut applicability = Applicability::MachineApplicable;
44-
span_lint_and_sugg(
44+
span_lint_and_then(
4545
cx,
4646
MATCH_AS_REF,
4747
expr.span,
48-
format!("use `{method}()` instead"),
49-
"try",
50-
format!(
51-
"{}.{method}(){cast}",
52-
snippet_with_applicability(cx, ex.span, "_", &mut applicability),
53-
),
54-
applicability,
48+
format!("manual implementation of `Option::{method}`"),
49+
|diag| {
50+
diag.span_suggestion_verbose(
51+
expr.span,
52+
format!("use `Option::{method}()` directly"),
53+
format!(
54+
"{}.{method}(){cast}",
55+
snippet_with_applicability(cx, ex.span, "_", &mut applicability),
56+
),
57+
applicability,
58+
);
59+
},
5560
);
5661
}
5762
}

tests/ui/match_as_ref.stderr

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: use `as_ref()` instead
1+
error: manual implementation of `Option::as_ref`
22
--> tests/ui/match_as_ref.rs:6:33
33
|
44
LL | let borrowed: Option<&()> = match owned {
@@ -7,12 +7,21 @@ LL | |
77
LL | | None => None,
88
LL | | Some(ref v) => Some(v),
99
LL | | };
10-
| |_____^ help: try: `owned.as_ref()`
10+
| |_____^
1111
|
1212
= note: `-D clippy::match-as-ref` implied by `-D warnings`
1313
= help: to override `-D warnings` add `#[allow(clippy::match_as_ref)]`
14+
help: use `Option::as_ref()` directly
15+
|
16+
LL - let borrowed: Option<&()> = match owned {
17+
LL -
18+
LL - None => None,
19+
LL - Some(ref v) => Some(v),
20+
LL - };
21+
LL + let borrowed: Option<&()> = owned.as_ref();
22+
|
1423

15-
error: use `as_mut()` instead
24+
error: manual implementation of `Option::as_mut`
1625
--> tests/ui/match_as_ref.rs:13:39
1726
|
1827
LL | let borrow_mut: Option<&mut ()> = match mut_owned {
@@ -21,17 +30,37 @@ LL | |
2130
LL | | None => None,
2231
LL | | Some(ref mut v) => Some(v),
2332
LL | | };
24-
| |_____^ help: try: `mut_owned.as_mut()`
33+
| |_____^
34+
|
35+
help: use `Option::as_mut()` directly
36+
|
37+
LL - let borrow_mut: Option<&mut ()> = match mut_owned {
38+
LL -
39+
LL - None => None,
40+
LL - Some(ref mut v) => Some(v),
41+
LL - };
42+
LL + let borrow_mut: Option<&mut ()> = mut_owned.as_mut();
43+
|
2544

26-
error: use `as_ref()` instead
45+
error: manual implementation of `Option::as_ref`
2746
--> tests/ui/match_as_ref.rs:32:13
2847
|
2948
LL | / match self.source {
3049
LL | |
3150
LL | | Some(ref s) => Some(s),
3251
LL | | None => None,
3352
LL | | }
34-
| |_____________^ help: try: `self.source.as_ref().map(|x| x as _)`
53+
| |_____________^
54+
|
55+
help: use `Option::as_ref()` directly
56+
|
57+
LL - match self.source {
58+
LL -
59+
LL - Some(ref s) => Some(s),
60+
LL - None => None,
61+
LL - }
62+
LL + self.source.as_ref().map(|x| x as _)
63+
|
3564

3665
error: aborting due to 3 previous errors
3766

0 commit comments

Comments
 (0)