|
1 |
| -use clippy_utils::diagnostics::span_lint_and_sugg; |
2 |
| -use clippy_utils::source::snippet_with_macro_callsite; |
3 |
| -use clippy_utils::{get_parent_expr_for_hir, get_parent_node}; |
| 1 | +use clippy_utils::diagnostics::{multispan_sugg_with_applicability, span_lint_and_then}; |
4 | 2 | use rustc_errors::Applicability;
|
5 |
| -use rustc_hir::{Block, Expr, ExprKind, Node, Stmt, StmtKind}; |
| 3 | +use rustc_hir::{Block, Expr, ExprKind, Stmt, StmtKind}; |
6 | 4 | use rustc_lint::{LateContext, LateLintPass};
|
7 | 5 | use rustc_session::{declare_lint_pass, declare_tool_lint};
|
| 6 | +use rustc_span::Span; |
8 | 7 |
|
9 | 8 | declare_clippy_lint! {
|
10 | 9 | /// ### What it does
|
@@ -65,69 +64,73 @@ declare_clippy_lint! {
|
65 | 64 | declare_lint_pass!(SemicolonBlock => [SEMICOLON_INSIDE_BLOCK, SEMICOLON_OUTSIDE_BLOCK]);
|
66 | 65 |
|
67 | 66 | impl LateLintPass<'_> for SemicolonBlock {
|
68 |
| - fn check_block(&mut self, cx: &LateContext<'_>, block: &Block<'_>) { |
69 |
| - semicolon_inside_block(cx, block); |
70 |
| - semicolon_outside_block(cx, block); |
71 |
| - } |
72 |
| -} |
73 |
| - |
74 |
| -fn semicolon_inside_block(cx: &LateContext<'_>, block: &Block<'_>) { |
75 |
| - if !block.span.from_expansion() |
76 |
| - && let Some(tail) = block.expr |
77 |
| - && let Some(block_expr @ Expr { kind: ExprKind::Block(_, _), ..}) = get_parent_expr_for_hir(cx, block.hir_id) |
78 |
| - && let Some(Node::Stmt(Stmt { kind: StmtKind::Semi(_), span, .. })) = get_parent_node(cx.tcx, block_expr.hir_id) |
79 |
| - { |
80 |
| - let expr_snip = snippet_with_macro_callsite(cx, tail.span, ".."); |
81 |
| - |
82 |
| - let mut suggestion: String = snippet_with_macro_callsite(cx, block.span, "..").to_string(); |
83 |
| - |
84 |
| - if let Some((expr_offset, _)) = suggestion.rmatch_indices(&*expr_snip).next() { |
85 |
| - suggestion.insert(expr_offset + expr_snip.len(), ';'); |
86 |
| - } else { |
87 |
| - return; |
| 67 | + fn check_stmt(&mut self, cx: &LateContext<'_>, stmt: &Stmt<'_>) { |
| 68 | + match stmt.kind { |
| 69 | + StmtKind::Expr(Expr { |
| 70 | + kind: |
| 71 | + ExprKind::Block( |
| 72 | + block @ Block { |
| 73 | + expr: None, |
| 74 | + stmts: |
| 75 | + &[ |
| 76 | + .., |
| 77 | + Stmt { |
| 78 | + kind: StmtKind::Semi(expr), |
| 79 | + span, |
| 80 | + .. |
| 81 | + }, |
| 82 | + ], |
| 83 | + .. |
| 84 | + }, |
| 85 | + _, |
| 86 | + ), |
| 87 | + .. |
| 88 | + }) if !block.span.from_expansion() => semicolon_outside_block(cx, block, expr, span), |
| 89 | + StmtKind::Semi(Expr { |
| 90 | + kind: ExprKind::Block(block @ Block { expr: Some(tail), .. }, _), |
| 91 | + .. |
| 92 | + }) if !block.span.from_expansion() => semicolon_inside_block(cx, block, tail, stmt.span), |
| 93 | + _ => (), |
88 | 94 | }
|
89 |
| - |
90 |
| - span_lint_and_sugg( |
91 |
| - cx, |
92 |
| - SEMICOLON_INSIDE_BLOCK, |
93 |
| - *span, |
94 |
| - "consider moving the `;` inside the block for consistent formatting", |
95 |
| - "put the `;` here", |
96 |
| - suggestion, |
97 |
| - Applicability::MaybeIncorrect, |
98 |
| - ); |
99 | 95 | }
|
100 | 96 | }
|
101 | 97 |
|
102 |
| -fn semicolon_outside_block(cx: &LateContext<'_>, block: &Block<'_>) { |
103 |
| - if !block.span.from_expansion() |
104 |
| - && block.expr.is_none() |
105 |
| - && let [.., Stmt { kind: StmtKind::Semi(expr), .. }] = block.stmts |
106 |
| - && let Some(block_expr @ Expr { kind: ExprKind::Block(_, _), ..}) = get_parent_expr_for_hir(cx,block.hir_id) |
107 |
| - && let Some(Node::Stmt(Stmt { kind: StmtKind::Expr(_), .. })) = get_parent_node(cx.tcx, block_expr.hir_id) |
108 |
| - { |
109 |
| - let expr_snip = snippet_with_macro_callsite(cx, expr.span, ".."); |
110 |
| - |
111 |
| - let mut suggestion: String = snippet_with_macro_callsite(cx, block.span, "..").to_string(); |
| 98 | +fn semicolon_inside_block(cx: &LateContext<'_>, block: &Block<'_>, tail: &Expr<'_>, semi_span: Span) { |
| 99 | + let insert_span = tail.span.with_lo(tail.span.hi()); |
| 100 | + let remove_span = semi_span.with_lo(block.span.hi()); |
112 | 101 |
|
113 |
| - if let Some((expr_offset, _)) = suggestion.rmatch_indices(&*expr_snip).next() |
114 |
| - && let Some(semi_offset) = suggestion[expr_offset + expr_snip.len()..].find(';') |
115 |
| - { |
116 |
| - suggestion.remove(expr_offset + expr_snip.len() + semi_offset); |
117 |
| - } else { |
118 |
| - return; |
119 |
| - } |
| 102 | + span_lint_and_then( |
| 103 | + cx, |
| 104 | + SEMICOLON_INSIDE_BLOCK, |
| 105 | + semi_span, |
| 106 | + "consider moving the `;` inside the block for consistent formatting", |
| 107 | + |diag| { |
| 108 | + multispan_sugg_with_applicability( |
| 109 | + diag, |
| 110 | + "put the `;` here", |
| 111 | + Applicability::MachineApplicable, |
| 112 | + [(remove_span, String::new()), (insert_span, ";".to_owned())], |
| 113 | + ); |
| 114 | + }, |
| 115 | + ); |
| 116 | +} |
120 | 117 |
|
121 |
| - suggestion.push(';'); |
| 118 | +fn semicolon_outside_block(cx: &LateContext<'_>, block: &Block<'_>, tail_stmt_expr: &Expr<'_>, semi_span: Span) { |
| 119 | + let insert_span = block.span.with_lo(block.span.hi()); |
| 120 | + let remove_span = semi_span.with_lo(tail_stmt_expr.span.source_callsite().hi()); |
122 | 121 |
|
123 |
| - span_lint_and_sugg( |
124 |
| - cx, |
125 |
| - SEMICOLON_OUTSIDE_BLOCK, |
126 |
| - block.span, |
127 |
| - "consider moving the `;` outside the block for consistent formatting", |
128 |
| - "put the `;` outside the block", |
129 |
| - suggestion, |
130 |
| - Applicability::MaybeIncorrect, |
131 |
| - ); |
132 |
| - } |
| 122 | + span_lint_and_then( |
| 123 | + cx, |
| 124 | + SEMICOLON_OUTSIDE_BLOCK, |
| 125 | + block.span, |
| 126 | + "consider moving the `;` outside the block for consistent formatting", |
| 127 | + |diag| { |
| 128 | + multispan_sugg_with_applicability( |
| 129 | + diag, |
| 130 | + "put the `;` here", |
| 131 | + Applicability::MachineApplicable, |
| 132 | + [(remove_span, String::new()), (insert_span, ";".to_owned())], |
| 133 | + ); |
| 134 | + }, |
| 135 | + ); |
133 | 136 | }
|
0 commit comments