Skip to content

Commit c7bb200

Browse files
committed
Auto merge of #12532 - samueltardieu:issue-12531, r=llogiq
Add necessary parentheses to `manual_unwrap_or_default` lint output Fix #12531 ---- changelog: [`manual_unwrap_or_default`]: add parentheses to suggestion when appropriate
2 parents 44a5eda + 2ffd133 commit c7bb200

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

clippy_lints/src/manual_unwrap_or_default.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use clippy_utils::sugg::Sugg;
12
use rustc_errors::Applicability;
23
use rustc_hir::def::Res;
34
use rustc_hir::{Arm, Expr, ExprKind, HirId, LangItem, MatchSource, Pat, PatKind, QPath};
@@ -124,15 +125,15 @@ fn handle_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool {
124125
// We now check the `None` arm is calling a method equivalent to `Default::default`.
125126
&& let body_none = body_none.peel_blocks()
126127
&& is_default_equivalent(cx, body_none)
127-
&& let Some(match_expr_snippet) = snippet_opt(cx, match_expr.span)
128+
&& let Some(receiver) = Sugg::hir_opt(cx, match_expr).map(Sugg::maybe_par)
128129
{
129130
span_lint_and_sugg(
130131
cx,
131132
MANUAL_UNWRAP_OR_DEFAULT,
132133
expr.span,
133134
"match can be simplified with `.unwrap_or_default()`",
134135
"replace it with",
135-
format!("{match_expr_snippet}.unwrap_or_default()"),
136+
format!("{receiver}.unwrap_or_default()"),
136137
Applicability::MachineApplicable,
137138
);
138139
}

tests/ui/manual_unwrap_or_default.fixed

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,12 @@ fn main() {
1717
let x: Option<Vec<String>> = None;
1818
x.unwrap_or_default();
1919
}
20+
21+
// Issue #12531
22+
unsafe fn no_deref_ptr(a: Option<i32>, b: *const Option<i32>) -> i32 {
23+
match a {
24+
// `*b` being correct depends on `a == Some(_)`
25+
Some(_) => (*b).unwrap_or_default(),
26+
_ => 0,
27+
}
28+
}

tests/ui/manual_unwrap_or_default.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,15 @@ fn main() {
3838
Vec::default()
3939
};
4040
}
41+
42+
// Issue #12531
43+
unsafe fn no_deref_ptr(a: Option<i32>, b: *const Option<i32>) -> i32 {
44+
match a {
45+
// `*b` being correct depends on `a == Some(_)`
46+
Some(_) => match *b {
47+
Some(v) => v,
48+
_ => 0,
49+
},
50+
_ => 0,
51+
}
52+
}

tests/ui/manual_unwrap_or_default.stderr

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,15 @@ LL | | Vec::default()
5252
LL | | };
5353
| |_____^ help: replace it with: `x.unwrap_or_default()`
5454

55-
error: aborting due to 5 previous errors
55+
error: match can be simplified with `.unwrap_or_default()`
56+
--> tests/ui/manual_unwrap_or_default.rs:46:20
57+
|
58+
LL | Some(_) => match *b {
59+
| ____________________^
60+
LL | | Some(v) => v,
61+
LL | | _ => 0,
62+
LL | | },
63+
| |_________^ help: replace it with: `(*b).unwrap_or_default()`
64+
65+
error: aborting due to 6 previous errors
5666

0 commit comments

Comments
 (0)