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

Commit a7c3301

Browse files
committed
refactor
1 parent dfccebe commit a7c3301

File tree

1 file changed

+61
-29
lines changed

1 file changed

+61
-29
lines changed

clippy_lints/src/semicolon_block.rs

Lines changed: 61 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,15 @@ impl LateLintPass<'_> for SemicolonBlock {
125125
..
126126
} = stmt else { return };
127127
semicolon_outside_block(cx, block, expr, span);
128+
semicolon_outside_block_if_singleline_check_outside(cx, block, expr, stmt.span)
128129
},
129130
StmtKind::Semi(Expr {
130131
kind: ExprKind::Block(block @ Block { expr: Some(tail), .. }, _),
131132
..
132-
}) if !block.span.from_expansion() => semicolon_inside_block(cx, block, tail, stmt.span),
133+
}) if !block.span.from_expansion() => {
134+
semicolon_inside_block(cx, block, tail, stmt.span);
135+
semicolon_outside_block_if_singleline_check_inside(cx, block, tail, stmt.span)
136+
},
133137
_ => (),
134138
}
135139
}
@@ -139,8 +143,6 @@ fn semicolon_inside_block(cx: &LateContext<'_>, block: &Block<'_>, tail: &Expr<'
139143
let insert_span = tail.span.source_callsite().shrink_to_hi();
140144
let remove_span = semi_span.with_lo(block.span.hi());
141145

142-
check_semicolon_outside_block_if_singleline(cx, block, remove_span, insert_span, true, "inside");
143-
144146
span_lint_and_then(
145147
cx,
146148
SEMICOLON_INSIDE_BLOCK,
@@ -163,8 +165,6 @@ fn semicolon_outside_block(cx: &LateContext<'_>, block: &Block<'_>, tail_stmt_ex
163165
let semi_span = cx.sess().source_map().stmt_span(semi_span, block.span);
164166
let remove_span = semi_span.with_lo(tail_stmt_expr.span.source_callsite().hi());
165167

166-
check_semicolon_outside_block_if_singleline(cx, block, remove_span, insert_span, false, "outside");
167-
168168
span_lint_and_then(
169169
cx,
170170
SEMICOLON_OUTSIDE_BLOCK,
@@ -181,39 +181,54 @@ fn semicolon_outside_block(cx: &LateContext<'_>, block: &Block<'_>, tail_stmt_ex
181181
);
182182
}
183183

184-
fn check_semicolon_outside_block_if_singleline(
184+
fn semicolon_outside_block_if_singleline_check_inside(
185185
cx: &LateContext<'_>,
186186
block: &Block<'_>,
187-
remove_span: Span,
188-
insert_span: Span,
189-
inequality: bool,
190-
ty: &str,
187+
tail: &Expr<'_>,
188+
semi_span: Span,
191189
) {
192-
let remove_line = cx
193-
.sess()
194-
.source_map()
195-
.lookup_line(remove_span.lo())
196-
.expect("failed to get `remove_span`'s line")
197-
.line;
198-
let insert_line = cx
199-
.sess()
200-
.source_map()
201-
.lookup_line(insert_span.lo())
202-
.expect("failed to get `insert_span`'s line")
203-
.line;
190+
let insert_span = tail.span.source_callsite().shrink_to_hi();
191+
let remove_span = semi_span.with_lo(block.span.hi());
192+
193+
let (remove_line, insert_line) = get_line(cx, remove_span, insert_span);
194+
195+
if insert_line != remove_line {
196+
span_lint_and_then(
197+
cx,
198+
SEMICOLON_OUTSIDE_BLOCK_IF_SINGLELINE,
199+
block.span,
200+
&format!("consider moving the `;` inside the block for consistent formatting"),
201+
|diag| {
202+
multispan_sugg_with_applicability(
203+
diag,
204+
"put the `;` here",
205+
Applicability::MachineApplicable,
206+
[(remove_span, String::new()), (insert_span, ";".to_owned())],
207+
);
208+
},
209+
);
210+
}
211+
}
204212

205-
let eq = if inequality {
206-
remove_line != insert_line
207-
} else {
208-
remove_line == insert_line
209-
};
213+
fn semicolon_outside_block_if_singleline_check_outside(
214+
cx: &LateContext<'_>,
215+
block: &Block<'_>,
216+
tail_stmt_expr: &Expr<'_>,
217+
semi_span: Span,
218+
) {
219+
let insert_span = block.span.with_lo(block.span.hi());
220+
// account for macro calls
221+
let semi_span = cx.sess().source_map().stmt_span(semi_span, block.span);
222+
let remove_span = semi_span.with_lo(tail_stmt_expr.span.source_callsite().hi());
223+
224+
let (remove_line, insert_line) = get_line(cx, remove_span, insert_span);
210225

211-
if eq {
226+
if remove_line == insert_line {
212227
span_lint_and_then(
213228
cx,
214229
SEMICOLON_OUTSIDE_BLOCK_IF_SINGLELINE,
215230
block.span,
216-
&format!("consider moving the `;` {ty} the block for consistent formatting"),
231+
&format!("consider moving the `;` outside the block for consistent formatting"),
217232
|diag| {
218233
multispan_sugg_with_applicability(
219234
diag,
@@ -225,3 +240,20 @@ fn check_semicolon_outside_block_if_singleline(
225240
);
226241
}
227242
}
243+
244+
fn get_line(cx: &LateContext<'_>, remove_span: Span, insert_span: Span) -> (usize, usize) {
245+
let remove_line = cx
246+
.sess()
247+
.source_map()
248+
.lookup_line(remove_span.lo())
249+
.expect("failed to get `remove_span`'s line")
250+
.line;
251+
let insert_line = cx
252+
.sess()
253+
.source_map()
254+
.lookup_line(insert_span.lo())
255+
.expect("failed to get `insert_span`'s line")
256+
.line;
257+
258+
(remove_line, insert_line)
259+
}

0 commit comments

Comments
 (0)