Skip to content

Commit 6236b1e

Browse files
committed
fix: remove semicolon
1 parent a224e00 commit 6236b1e

File tree

1 file changed

+56
-29
lines changed

1 file changed

+56
-29
lines changed

crates/ide_assists/src/handlers/convert_iter_for_each_to_for.rs

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,50 +28,54 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
2828
/// }
2929
/// ```
3030
pub(crate) fn convert_iter_for_each_to_for(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
31-
let closure;
32-
33-
let total_expr = match ctx.find_node_at_offset::<ast::Expr>()? {
34-
ast::Expr::MethodCallExpr(expr) => {
35-
closure = match expr.arg_list()?.args().next()? {
36-
ast::Expr::ClosureExpr(expr) => expr,
37-
_ => {
38-
return None;
39-
}
40-
};
41-
42-
expr
43-
}
44-
ast::Expr::ClosureExpr(expr) => {
45-
closure = expr;
46-
ast::MethodCallExpr::cast(closure.syntax().ancestors().nth(2)?)?
47-
}
31+
let method;
32+
33+
let stmt = if let Some(stmt) = ctx.find_node_at_offset::<ast::ExprStmt>() {
34+
method = ast::MethodCallExpr::cast(stmt.syntax().first_child()?)?;
35+
Some(stmt)
36+
} else {
37+
method = match ctx.find_node_at_offset::<ast::Expr>()? {
38+
ast::Expr::MethodCallExpr(expr) => expr,
39+
ast::Expr::ClosureExpr(expr) => {
40+
ast::MethodCallExpr::cast(expr.syntax().ancestors().nth(2)?)?
41+
}
42+
_ => {
43+
return None;
44+
}
45+
};
46+
None
47+
};
48+
49+
let closure = match method.arg_list()?.args().next()? {
50+
ast::Expr::ClosureExpr(expr) => expr,
4851
_ => {
4952
return None;
5053
}
5154
};
5255

53-
let (total_expr, parent) = validate_method_call_expr(&ctx.sema, total_expr)?;
56+
let (method, parent) = validate_method_call_expr(&ctx.sema, method)?;
5457

5558
let param_list = closure.param_list()?;
5659
let param = param_list.params().next()?.pat()?;
5760
let body = closure.body()?;
5861

62+
let indent = stmt.as_ref().map_or(method.indent_level(), |stmt| stmt.indent_level());
63+
let syntax = stmt.as_ref().map_or(method.syntax(), |stmt| stmt.syntax());
64+
5965
acc.add(
6066
AssistId("convert_iter_for_each_to_for", AssistKind::RefactorRewrite),
6167
"Replace this `Iterator::for_each` with a for loop",
62-
total_expr.syntax().text_range(),
68+
syntax.text_range(),
6369
|builder| {
64-
let original_indentation = total_expr.indent_level();
65-
6670
let block = match body {
6771
ast::Expr::BlockExpr(block) => block,
6872
_ => make::block_expr(Vec::new(), Some(body)),
6973
}
7074
.reset_indent()
71-
.indent(original_indentation);
75+
.indent(indent);
7276

7377
let expr_for_loop = make::expr_for_loop(param, parent, block);
74-
builder.replace_ast(total_expr, expr_for_loop)
78+
builder.replace(syntax.text_range(), expr_for_loop.syntax().text())
7579
},
7680
)
7781
}
@@ -125,7 +129,7 @@ impl Empty {
125129
}
126130

127131
#[test]
128-
fn test_for_each_in_method() {
132+
fn test_for_each_in_method_stmt() {
129133
check_assist_with_fixtures(
130134
r#"
131135
use empty_iter::*;
@@ -141,14 +145,37 @@ fn main() {
141145
let x = Empty;
142146
for (x, y) in x.iter() {
143147
println!("x: {}, y: {}", x, y);
144-
};
148+
}
145149
}
146150
"#,
147151
)
148152
}
149153

150154
#[test]
151-
fn test_for_each_without_braces() {
155+
fn test_for_each_in_method() {
156+
check_assist_with_fixtures(
157+
r#"
158+
use empty_iter::*;
159+
fn main() {
160+
let x = Empty;
161+
x.iter().$0for_each(|(x, y)| {
162+
println!("x: {}, y: {}", x, y);
163+
})
164+
}"#,
165+
r#"
166+
use empty_iter::*;
167+
fn main() {
168+
let x = Empty;
169+
for (x, y) in x.iter() {
170+
println!("x: {}, y: {}", x, y);
171+
}
172+
}
173+
"#,
174+
)
175+
}
176+
177+
#[test]
178+
fn test_for_each_without_braces_stmt() {
152179
check_assist_with_fixtures(
153180
r#"
154181
use empty_iter::*;
@@ -162,14 +189,14 @@ fn main() {
162189
let x = Empty;
163190
for (x, y) in x.iter() {
164191
println!("x: {}, y: {}", x, y)
165-
};
192+
}
166193
}
167194
"#,
168195
)
169196
}
170197

171198
#[test]
172-
fn test_for_each_in_closure() {
199+
fn test_for_each_in_closure_stmt() {
173200
check_assist_with_fixtures(
174201
r#"
175202
use empty_iter::*;
@@ -183,7 +210,7 @@ fn main() {
183210
let x = Empty;
184211
for (x, y) in x.iter() {
185212
println!("x: {}, y: {}", x, y)
186-
};
213+
}
187214
}
188215
"#,
189216
)

0 commit comments

Comments
 (0)