about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCentri3 <114838443+Centri3@users.noreply.github.com>2023-04-16 23:56:21 -0500
committerCentri3 <114838443+Centri3@users.noreply.github.com>2023-04-16 23:56:21 -0500
commita7c3301b5884b33fc87f2e498f28426dfbcb1a84 (patch)
treedfb4d7afcfc9f691e32b0e18a67364f5e84c060b
parentdfccebe3e01e8c63ea0fc2833550123e45091e5b (diff)
downloadrust-a7c3301b5884b33fc87f2e498f28426dfbcb1a84.tar.gz
rust-a7c3301b5884b33fc87f2e498f28426dfbcb1a84.zip
refactor
-rw-r--r--clippy_lints/src/semicolon_block.rs90
1 files changed, 61 insertions, 29 deletions
diff --git a/clippy_lints/src/semicolon_block.rs b/clippy_lints/src/semicolon_block.rs
index eabe0e4619f..d85d3b58305 100644
--- a/clippy_lints/src/semicolon_block.rs
+++ b/clippy_lints/src/semicolon_block.rs
@@ -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,39 +181,54 @@ 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 remove_line = cx
-        .sess()
-        .source_map()
-        .lookup_line(remove_span.lo())
-        .expect("failed to get `remove_span`'s line")
-        .line;
-    let insert_line = cx
-        .sess()
-        .source_map()
-        .lookup_line(insert_span.lo())
-        .expect("failed to get `insert_span`'s line")
-        .line;
+    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())],
+                );
+            },
+        );
+    }
+}
 
-    let eq = if inequality {
-        remove_line != insert_line
-    } else {
-        remove_line == insert_line
-    };
+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 eq {
+    if remove_line == insert_line {
         span_lint_and_then(
             cx,
             SEMICOLON_OUTSIDE_BLOCK_IF_SINGLELINE,
             block.span,
-            &format!("consider moving the `;` {ty} the block for consistent formatting"),
+            &format!("consider moving the `;` outside the block for consistent formatting"),
             |diag| {
                 multispan_sugg_with_applicability(
                     diag,
@@ -225,3 +240,20 @@ fn check_semicolon_outside_block_if_singleline(
         );
     }
 }
+
+fn get_line(cx: &LateContext<'_>, remove_span: Span, insert_span: Span) -> (usize, usize) {
+    let remove_line = cx
+        .sess()
+        .source_map()
+        .lookup_line(remove_span.lo())
+        .expect("failed to get `remove_span`'s line")
+        .line;
+    let insert_line = cx
+        .sess()
+        .source_map()
+        .lookup_line(insert_span.lo())
+        .expect("failed to get `insert_span`'s line")
+        .line;
+
+    (remove_line, insert_line)
+}