Skip to content

Commit 7b611d4

Browse files
authored
fix or_then_unwrap: suggestion preserves macro calls (#15483)
Before this change, the suggestion for `Option.or(Some(vec![])).unwrap()` expanded the `vec!` macro which broke the code, both in terms of readability, and because the expansion references `$crate` which cannot be inlined. changelog: [`or_then_unwrap`]: Preserve macro calls rather than expanding them Relates to #6851
2 parents f6c7786 + 0170d6c commit 7b611d4

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

clippy_lints/src/methods/or_then_unwrap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ fn get_content_if_ctor_matches(cx: &LateContext<'_>, expr: &Expr<'_>, item: Lang
6262
if let ExprKind::Call(some_expr, [arg]) = expr.kind
6363
&& is_res_lang_ctor(cx, path_res(cx, some_expr), item)
6464
{
65-
Some(arg.span)
65+
Some(arg.span.source_callsite())
6666
} else {
6767
None
6868
}

tests/ui/or_then_unwrap.fixed

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ fn main() {
2828
//
2929
//~^^ or_then_unwrap
3030

31+
// Call with macro should preserve the macro call rather than expand it
32+
let option: Option<Vec<&str>> = None;
33+
let _ = option.unwrap_or(vec!["fallback"]); // should trigger lint
34+
//
35+
//~^^ or_then_unwrap
36+
3137
// as part of a method chain
3238
let option: Option<&str> = None;
3339
let _ = option.map(|v| v).unwrap_or("fallback").to_string().chars(); // should trigger lint

tests/ui/or_then_unwrap.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ fn main() {
2828
//
2929
//~^^ or_then_unwrap
3030

31+
// Call with macro should preserve the macro call rather than expand it
32+
let option: Option<Vec<&str>> = None;
33+
let _ = option.or(Some(vec!["fallback"])).unwrap(); // should trigger lint
34+
//
35+
//~^^ or_then_unwrap
36+
3137
// as part of a method chain
3238
let option: Option<&str> = None;
3339
let _ = option.map(|v| v).or(Some("fallback")).unwrap().to_string().chars(); // should trigger lint

tests/ui/or_then_unwrap.stderr

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,16 @@ LL | let _ = result.or::<&str>(Ok("fallback")).unwrap(); // should trigger l
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or("fallback")`
1515

1616
error: found `.or(Some(…)).unwrap()`
17-
--> tests/ui/or_then_unwrap.rs:33:31
17+
--> tests/ui/or_then_unwrap.rs:33:20
18+
|
19+
LL | let _ = option.or(Some(vec!["fallback"])).unwrap(); // should trigger lint
20+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or(vec!["fallback"])`
21+
22+
error: found `.or(Some(…)).unwrap()`
23+
--> tests/ui/or_then_unwrap.rs:39:31
1824
|
1925
LL | let _ = option.map(|v| v).or(Some("fallback")).unwrap().to_string().chars(); // should trigger lint
2026
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or("fallback")`
2127

22-
error: aborting due to 3 previous errors
28+
error: aborting due to 4 previous errors
2329

0 commit comments

Comments
 (0)