Skip to content

Commit 0def42f

Browse files
committed
Auto merge of rust-lang#7974 - matthiaskrgr:7973_opt_map_or_else_dont_expand_sugg_macro, r=flip1995
option_if_let_else: don't expand macros in suggestion Fixes rust-lang#7973 changelog: don't expand macros in suggestion of clippy::option_if_let_else
2 parents f82bf47 + 3fe11e7 commit 0def42f

File tree

4 files changed

+48
-8
lines changed

4 files changed

+48
-8
lines changed

clippy_lints/src/option_if_let_else.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ fn extract_body_from_expr<'a>(expr: &'a Expr<'a>) -> Option<&'a Expr<'a>> {
111111
fn format_option_in_sugg(cx: &LateContext<'_>, cond_expr: &Expr<'_>, as_ref: bool, as_mut: bool) -> String {
112112
format!(
113113
"{}{}",
114-
Sugg::hir(cx, cond_expr, "..").maybe_par(),
114+
Sugg::hir_with_macro_callsite(cx, cond_expr, "..").maybe_par(),
115115
if as_mut {
116116
".as_mut()"
117117
} else if as_ref {
@@ -184,8 +184,8 @@ fn detect_option_if_let_else<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) ->
184184
Some(OptionIfLetElseOccurence {
185185
option: format_option_in_sugg(cx, cond_expr, as_ref, as_mut),
186186
method_sugg: method_sugg.to_string(),
187-
some_expr: format!("|{}{}| {}", capture_mut, capture_name, Sugg::hir(cx, some_body, "..")),
188-
none_expr: format!("{}{}", if method_sugg == "map_or" { "" } else { "|| " }, Sugg::hir(cx, none_body, "..")),
187+
some_expr: format!("|{}{}| {}", capture_mut, capture_name, Sugg::hir_with_macro_callsite(cx, some_body, "..")),
188+
none_expr: format!("{}{}", if method_sugg == "map_or" { "" } else { "|| " }, Sugg::hir_with_macro_callsite(cx, none_body, "..")),
189189
})
190190
} else {
191191
None

tests/ui/option_if_let_else.fixed

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@ fn negative_tests(arg: Option<u32>) -> u32 {
7575
7
7676
}
7777

78+
// #7973
79+
fn pattern_to_vec(pattern: &str) -> Vec<String> {
80+
pattern
81+
.trim_matches('/')
82+
.split('/')
83+
.flat_map(|s| {
84+
s.find('.').map_or_else(|| vec![s.to_string()], |idx| vec![s[..idx].to_string(), s[idx..].to_string()])
85+
})
86+
.collect::<Vec<_>>()
87+
}
88+
7889
fn main() {
7990
let optional = Some(5);
8091
let _ = optional.map_or(5, |x| x + 2);
@@ -146,4 +157,6 @@ fn main() {
146157
// Don't lint. `await` can't be moved into a closure.
147158
let _ = if let Some(x) = Some(0) { _f1(x).await } else { 0 };
148159
}
160+
161+
let _ = pattern_to_vec("hello world");
149162
}

tests/ui/option_if_let_else.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,21 @@ fn negative_tests(arg: Option<u32>) -> u32 {
9494
7
9595
}
9696

97+
// #7973
98+
fn pattern_to_vec(pattern: &str) -> Vec<String> {
99+
pattern
100+
.trim_matches('/')
101+
.split('/')
102+
.flat_map(|s| {
103+
if let Some(idx) = s.find('.') {
104+
vec![s[..idx].to_string(), s[idx..].to_string()]
105+
} else {
106+
vec![s.to_string()]
107+
}
108+
})
109+
.collect::<Vec<_>>()
110+
}
111+
97112
fn main() {
98113
let optional = Some(5);
99114
let _ = if let Some(x) = optional { x + 2 } else { 5 };
@@ -171,4 +186,6 @@ fn main() {
171186
// Don't lint. `await` can't be moved into a closure.
172187
let _ = if let Some(x) = Some(0) { _f1(x).await } else { 0 };
173188
}
189+
190+
let _ = pattern_to_vec("hello world");
174191
}

tests/ui/option_if_let_else.stderr

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,24 @@ LL + y
142142
LL ~ }, |x| x * x * x * x);
143143
|
144144

145+
error: use Option::map_or_else instead of an if let/else
146+
--> $DIR/option_if_let_else.rs:103:13
147+
|
148+
LL | / if let Some(idx) = s.find('.') {
149+
LL | | vec![s[..idx].to_string(), s[idx..].to_string()]
150+
LL | | } else {
151+
LL | | vec![s.to_string()]
152+
LL | | }
153+
| |_____________^ help: try: `s.find('.').map_or_else(|| vec![s.to_string()], |idx| vec![s[..idx].to_string(), s[idx..].to_string()])`
154+
145155
error: use Option::map_or instead of an if let/else
146-
--> $DIR/option_if_let_else.rs:99:13
156+
--> $DIR/option_if_let_else.rs:114:13
147157
|
148158
LL | let _ = if let Some(x) = optional { x + 2 } else { 5 };
149159
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `optional.map_or(5, |x| x + 2)`
150160

151161
error: use Option::map_or instead of an if let/else
152-
--> $DIR/option_if_let_else.rs:108:13
162+
--> $DIR/option_if_let_else.rs:123:13
153163
|
154164
LL | let _ = if let Some(x) = Some(0) {
155165
| _____________^
@@ -171,13 +181,13 @@ LL ~ });
171181
|
172182

173183
error: use Option::map_or_else instead of an if let/else
174-
--> $DIR/option_if_let_else.rs:136:13
184+
--> $DIR/option_if_let_else.rs:151:13
175185
|
176186
LL | let _ = if let Some(x) = Some(0) { s.len() + x } else { s.len() };
177187
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Some(0).map_or_else(|| s.len(), |x| s.len() + x)`
178188

179189
error: use Option::map_or instead of an if let/else
180-
--> $DIR/option_if_let_else.rs:140:13
190+
--> $DIR/option_if_let_else.rs:155:13
181191
|
182192
LL | let _ = if let Some(x) = Some(0) {
183193
| _____________^
@@ -196,5 +206,5 @@ LL + s.len() + x
196206
LL ~ });
197207
|
198208

199-
error: aborting due to 14 previous errors
209+
error: aborting due to 15 previous errors
200210

0 commit comments

Comments
 (0)