Skip to content

Commit 0a330e6

Browse files
committed
Auto merge of rust-lang#7136 - mgacek8:issue6965_manual_unwrap_or_invalid_sugg_macro_expansion, r=llogiq
manual_unwrap_or: fix invalid code suggestion, due to macro expansion fixes rust-lang#6965 changelog: fix invalid code suggestion in `manual_unwrap_or` lint, due to macro expansion
2 parents f33d86d + 84003aa commit 0a330e6

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

clippy_lints/src/manual_unwrap_or.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,23 @@ fn lint_manual_unwrap_or<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
112112
then {
113113
let reindented_or_body =
114114
reindent_multiline(or_body_snippet.into(), true, Some(indent));
115+
116+
let suggestion = if scrutinee.span.from_expansion() {
117+
// we don't want parenthesis around macro, e.g. `(some_macro!()).unwrap_or(0)`
118+
sugg::Sugg::hir_with_macro_callsite(cx, scrutinee, "..")
119+
}
120+
else {
121+
sugg::Sugg::hir(cx, scrutinee, "..").maybe_par()
122+
};
123+
115124
span_lint_and_sugg(
116125
cx,
117126
MANUAL_UNWRAP_OR, expr.span,
118127
&format!("this pattern reimplements `{}`", case.unwrap_fn_path()),
119128
"replace with",
120129
format!(
121130
"{}.unwrap_or({})",
122-
sugg::Sugg::hir(cx, scrutinee, "..").maybe_par(),
131+
suggestion,
123132
reindented_or_body,
124133
),
125134
Applicability::MachineApplicable,

tests/ui/manual_unwrap_or.fixed

+12
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,16 @@ const fn const_fn_result_unwrap_or() {
151151
};
152152
}
153153

154+
mod issue6965 {
155+
macro_rules! some_macro {
156+
() => {
157+
if 1 > 2 { Some(1) } else { None }
158+
};
159+
}
160+
161+
fn test() {
162+
let _ = some_macro!().unwrap_or(0);
163+
}
164+
}
165+
154166
fn main() {}

tests/ui/manual_unwrap_or.rs

+15
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,19 @@ const fn const_fn_result_unwrap_or() {
190190
};
191191
}
192192

193+
mod issue6965 {
194+
macro_rules! some_macro {
195+
() => {
196+
if 1 > 2 { Some(1) } else { None }
197+
};
198+
}
199+
200+
fn test() {
201+
let _ = match some_macro!() {
202+
Some(val) => val,
203+
None => 0,
204+
};
205+
}
206+
}
207+
193208
fn main() {}

tests/ui/manual_unwrap_or.stderr

+11-1
Original file line numberDiff line numberDiff line change
@@ -141,5 +141,15 @@ LL | | Err(_) => "Alice",
141141
LL | | };
142142
| |_____^ help: replace with: `Ok::<&str, &str>("Bob").unwrap_or("Alice")`
143143

144-
error: aborting due to 13 previous errors
144+
error: this pattern reimplements `Option::unwrap_or`
145+
--> $DIR/manual_unwrap_or.rs:201:17
146+
|
147+
LL | let _ = match some_macro!() {
148+
| _________________^
149+
LL | | Some(val) => val,
150+
LL | | None => 0,
151+
LL | | };
152+
| |_________^ help: replace with: `some_macro!().unwrap_or(0)`
153+
154+
error: aborting due to 14 previous errors
145155

0 commit comments

Comments
 (0)