Skip to content

Commit a6e5159

Browse files
committed
fix: parenthesise the receiver if needed
1 parent 91dbaae commit a6e5159

File tree

4 files changed

+54
-3
lines changed

4 files changed

+54
-3
lines changed

clippy_lints/src/matches/match_as_ref.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::res::{MaybeDef, MaybeQPath};
3-
use clippy_utils::source::snippet_with_applicability;
3+
use clippy_utils::sugg::Sugg;
44
use clippy_utils::ty::option_arg_ty;
55
use clippy_utils::{is_none_arm, peel_blocks};
66
use rustc_errors::Applicability;
@@ -52,7 +52,7 @@ pub(crate) fn check(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>], expr:
5252
format!("use `Option::{method}()` directly"),
5353
format!(
5454
"{}.{method}(){cast}",
55-
snippet_with_applicability(cx, ex.span, "_", &mut applicability),
55+
Sugg::hir_with_applicability(cx, ex, "_", &mut applicability).maybe_paren(),
5656
),
5757
applicability,
5858
);

tests/ui/match_as_ref.fixed

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,16 @@ mod issue15691 {
7171
};
7272
}
7373
}
74+
75+
fn recv_requiring_parens() {
76+
struct S;
77+
78+
impl std::ops::Not for S {
79+
type Output = Option<u64>;
80+
fn not(self) -> Self::Output {
81+
None
82+
}
83+
}
84+
85+
let _ = (!S).as_ref();
86+
}

tests/ui/match_as_ref.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,20 @@ mod issue15691 {
8383
};
8484
}
8585
}
86+
87+
fn recv_requiring_parens() {
88+
struct S;
89+
90+
impl std::ops::Not for S {
91+
type Output = Option<u64>;
92+
fn not(self) -> Self::Output {
93+
None
94+
}
95+
}
96+
97+
let _ = match !S {
98+
//~^ match_as_ref
99+
None => None,
100+
Some(ref v) => Some(v),
101+
};
102+
}

tests/ui/match_as_ref.stderr

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,26 @@ LL - }
6262
LL + self.source.as_ref().map(|x| x as _)
6363
|
6464

65-
error: aborting due to 3 previous errors
65+
error: manual implementation of `Option::as_ref`
66+
--> tests/ui/match_as_ref.rs:97:13
67+
|
68+
LL | let _ = match !S {
69+
| _____________^
70+
LL | |
71+
LL | | None => None,
72+
LL | | Some(ref v) => Some(v),
73+
LL | | };
74+
| |_____^
75+
|
76+
help: use `Option::as_ref()` directly
77+
|
78+
LL - let _ = match !S {
79+
LL -
80+
LL - None => None,
81+
LL - Some(ref v) => Some(v),
82+
LL - };
83+
LL + let _ = (!S).as_ref();
84+
|
85+
86+
error: aborting due to 4 previous errors
6687

0 commit comments

Comments
 (0)