This commit is contained in:
Centri3 2023-04-16 23:56:21 -05:00
parent dfccebe3e0
commit a7c3301b58

View File

@ -125,11 +125,15 @@ impl LateLintPass<'_> for SemicolonBlock {
..
} = stmt else { return };
semicolon_outside_block(cx, block, expr, span);
semicolon_outside_block_if_singleline_check_outside(cx, block, expr, stmt.span)
},
StmtKind::Semi(Expr {
kind: ExprKind::Block(block @ Block { expr: Some(tail), .. }, _),
..
}) if !block.span.from_expansion() => semicolon_inside_block(cx, block, tail, stmt.span),
}) if !block.span.from_expansion() => {
semicolon_inside_block(cx, block, tail, stmt.span);
semicolon_outside_block_if_singleline_check_inside(cx, block, tail, stmt.span)
},
_ => (),
}
}
@ -139,8 +143,6 @@ fn semicolon_inside_block(cx: &LateContext<'_>, block: &Block<'_>, tail: &Expr<'
let insert_span = tail.span.source_callsite().shrink_to_hi();
let remove_span = semi_span.with_lo(block.span.hi());
check_semicolon_outside_block_if_singleline(cx, block, remove_span, insert_span, true, "inside");
span_lint_and_then(
cx,
SEMICOLON_INSIDE_BLOCK,
@ -163,8 +165,6 @@ fn semicolon_outside_block(cx: &LateContext<'_>, block: &Block<'_>, tail_stmt_ex
let semi_span = cx.sess().source_map().stmt_span(semi_span, block.span);
let remove_span = semi_span.with_lo(tail_stmt_expr.span.source_callsite().hi());
check_semicolon_outside_block_if_singleline(cx, block, remove_span, insert_span, false, "outside");
span_lint_and_then(
cx,
SEMICOLON_OUTSIDE_BLOCK,
@ -181,14 +181,67 @@ fn semicolon_outside_block(cx: &LateContext<'_>, block: &Block<'_>, tail_stmt_ex
);
}
fn check_semicolon_outside_block_if_singleline(
fn semicolon_outside_block_if_singleline_check_inside(
cx: &LateContext<'_>,
block: &Block<'_>,
remove_span: Span,
insert_span: Span,
inequality: bool,
ty: &str,
tail: &Expr<'_>,
semi_span: Span,
) {
let insert_span = tail.span.source_callsite().shrink_to_hi();
let remove_span = semi_span.with_lo(block.span.hi());
let (remove_line, insert_line) = get_line(cx, remove_span, insert_span);
if insert_line != remove_line {
span_lint_and_then(
cx,
SEMICOLON_OUTSIDE_BLOCK_IF_SINGLELINE,
block.span,
&format!("consider moving the `;` inside the block for consistent formatting"),
|diag| {
multispan_sugg_with_applicability(
diag,
"put the `;` here",
Applicability::MachineApplicable,
[(remove_span, String::new()), (insert_span, ";".to_owned())],
);
},
);
}
}
fn semicolon_outside_block_if_singleline_check_outside(
cx: &LateContext<'_>,
block: &Block<'_>,
tail_stmt_expr: &Expr<'_>,
semi_span: Span,
) {
let insert_span = block.span.with_lo(block.span.hi());
// account for macro calls
let semi_span = cx.sess().source_map().stmt_span(semi_span, block.span);
let remove_span = semi_span.with_lo(tail_stmt_expr.span.source_callsite().hi());
let (remove_line, insert_line) = get_line(cx, remove_span, insert_span);
if remove_line == insert_line {
span_lint_and_then(
cx,
SEMICOLON_OUTSIDE_BLOCK_IF_SINGLELINE,
block.span,
&format!("consider moving the `;` outside the block for consistent formatting"),
|diag| {
multispan_sugg_with_applicability(
diag,
"put the `;` here",
Applicability::MachineApplicable,
[(remove_span, String::new()), (insert_span, ";".to_owned())],
);
},
);
}
}
fn get_line(cx: &LateContext<'_>, remove_span: Span, insert_span: Span) -> (usize, usize) {
let remove_line = cx
.sess()
.source_map()
@ -202,26 +255,5 @@ fn check_semicolon_outside_block_if_singleline(
.expect("failed to get `insert_span`'s line")
.line;
let eq = if inequality {
remove_line != insert_line
} else {
remove_line == insert_line
};
if eq {
span_lint_and_then(
cx,
SEMICOLON_OUTSIDE_BLOCK_IF_SINGLELINE,
block.span,
&format!("consider moving the `;` {ty} the block for consistent formatting"),
|diag| {
multispan_sugg_with_applicability(
diag,
"put the `;` here",
Applicability::MachineApplicable,
[(remove_span, String::new()), (insert_span, ";".to_owned())],
);
},
);
}
(remove_line, insert_line)
}