Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit fa1efa8

Browse files
committed
refactor
1 parent 80707aa commit fa1efa8

File tree

1 file changed

+22
-55
lines changed

1 file changed

+22
-55
lines changed

clippy_lints/src/semicolon_block.rs

Lines changed: 22 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,12 @@ impl LateLintPass<'_> for SemicolonBlock {
126126
..
127127
} = stmt else { return };
128128
semicolon_outside_block(cx, block, expr, span);
129-
semicolon_outside_block_if_singleline_check_outside(cx, block, expr, stmt.span);
130129
},
131130
StmtKind::Semi(Expr {
132131
kind: ExprKind::Block(block @ Block { expr: Some(tail), .. }, _),
133132
..
134133
}) if !block.span.from_expansion() => {
135134
semicolon_inside_block(cx, block, tail, stmt.span);
136-
semicolon_outside_block_if_singleline_check_inside(cx, block, tail, stmt.span);
137135
},
138136
_ => (),
139137
}
@@ -144,6 +142,8 @@ fn semicolon_inside_block(cx: &LateContext<'_>, block: &Block<'_>, tail: &Expr<'
144142
let insert_span = tail.span.source_callsite().shrink_to_hi();
145143
let remove_span = semi_span.with_lo(block.span.hi());
146144

145+
semicolon_outside_block_if_singleline(cx, block, remove_span, insert_span, true, "inside");
146+
147147
span_lint_and_then(
148148
cx,
149149
SEMICOLON_INSIDE_BLOCK,
@@ -166,6 +166,8 @@ fn semicolon_outside_block(cx: &LateContext<'_>, block: &Block<'_>, tail_stmt_ex
166166
let semi_span = cx.sess().source_map().stmt_span(semi_span, block.span);
167167
let remove_span = semi_span.with_lo(tail_stmt_expr.span.source_callsite().hi());
168168

169+
semicolon_outside_block_if_singleline(cx, block, remove_span, insert_span, false, "outside");
170+
169171
span_lint_and_then(
170172
cx,
171173
SEMICOLON_OUTSIDE_BLOCK,
@@ -182,23 +184,28 @@ fn semicolon_outside_block(cx: &LateContext<'_>, block: &Block<'_>, tail_stmt_ex
182184
);
183185
}
184186

185-
fn semicolon_outside_block_if_singleline_check_inside(
187+
fn semicolon_outside_block_if_singleline(
186188
cx: &LateContext<'_>,
187189
block: &Block<'_>,
188-
tail: &Expr<'_>,
189-
semi_span: Span,
190+
remove_span: Span,
191+
insert_span: Span,
192+
inequality: bool,
193+
ty: &str,
190194
) {
191-
let insert_span = tail.span.source_callsite().shrink_to_hi();
192-
let remove_span = semi_span.with_lo(block.span.hi());
195+
let (remove_line, insert_line) = (get_line(cx, remove_span), get_line(cx, insert_span));
193196

194-
let (remove_line, insert_line) = get_line(cx, remove_span, insert_span);
197+
let eq = if inequality {
198+
remove_line != insert_line
199+
} else {
200+
remove_line == insert_line
201+
};
195202

196-
if insert_line != remove_line {
203+
if eq {
197204
span_lint_and_then(
198205
cx,
199206
SEMICOLON_OUTSIDE_BLOCK_IF_SINGLELINE,
200207
block.span,
201-
"consider moving the `;` inside the block for consistent formatting",
208+
&format!("consider moving the `;` {ty} the block for consistent formatting"),
202209
|diag| {
203210
multispan_sugg_with_applicability(
204211
diag,
@@ -211,50 +218,10 @@ fn semicolon_outside_block_if_singleline_check_inside(
211218
}
212219
}
213220

214-
fn semicolon_outside_block_if_singleline_check_outside(
215-
cx: &LateContext<'_>,
216-
block: &Block<'_>,
217-
tail_stmt_expr: &Expr<'_>,
218-
semi_span: Span,
219-
) {
220-
let insert_span = block.span.with_lo(block.span.hi());
221-
// account for macro calls
222-
let semi_span = cx.sess().source_map().stmt_span(semi_span, block.span);
223-
let remove_span = semi_span.with_lo(tail_stmt_expr.span.source_callsite().hi());
224-
225-
let (remove_line, insert_line) = get_line(cx, remove_span, insert_span);
226-
227-
if remove_line == insert_line {
228-
span_lint_and_then(
229-
cx,
230-
SEMICOLON_OUTSIDE_BLOCK_IF_SINGLELINE,
231-
block.span,
232-
"consider moving the `;` outside the block for consistent formatting",
233-
|diag| {
234-
multispan_sugg_with_applicability(
235-
diag,
236-
"put the `;` here",
237-
Applicability::MachineApplicable,
238-
[(remove_span, String::new()), (insert_span, ";".to_owned())],
239-
);
240-
},
241-
);
242-
}
243-
}
244-
245-
fn get_line(cx: &LateContext<'_>, remove_span: Span, insert_span: Span) -> (usize, usize) {
246-
let remove_line = cx
247-
.sess()
248-
.source_map()
249-
.lookup_line(remove_span.lo())
250-
.expect("failed to get `remove_span`'s line")
251-
.line;
252-
let insert_line = cx
253-
.sess()
221+
fn get_line(cx: &LateContext<'_>, span: Span) -> usize {
222+
cx.sess()
254223
.source_map()
255-
.lookup_line(insert_span.lo())
256-
.expect("failed to get `insert_span`'s line")
257-
.line;
258-
259-
(remove_line, insert_line)
224+
.lookup_line(span.lo())
225+
.expect("failed to get span's line")
226+
.line
260227
}

0 commit comments

Comments
 (0)