|
| 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}; |
| 4 | +use rustc_errors::Applicability; |
| 5 | +use rustc_hir::{Block, Expr, ExprKind, Node, Stmt, StmtKind}; |
| 6 | +use rustc_lint::{LateContext, LateLintPass}; |
| 7 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 8 | + |
| 9 | +declare_clippy_lint! { |
| 10 | + /// ### What it does |
| 11 | + /// |
| 12 | + /// For () returning expressions, check that the semicolon is inside the block. |
| 13 | + /// |
| 14 | + /// ### Why is this bad? |
| 15 | + /// |
| 16 | + /// For consistency it's best to have the semicolon inside/outside the block. Either way is fine and this lint suggests inside the block. |
| 17 | + /// Take a look at `semicolon_outside_block` for the other alternative. |
| 18 | + /// |
| 19 | + /// ### Example |
| 20 | + /// |
| 21 | + /// ```rust |
| 22 | + /// # fn f(_: u32) {} |
| 23 | + /// # let x = 0; |
| 24 | + /// unsafe { f(x) }; |
| 25 | + /// ``` |
| 26 | + /// Use instead: |
| 27 | + /// ```rust |
| 28 | + /// # fn f(_: u32) {} |
| 29 | + /// # let x = 0; |
| 30 | + /// unsafe { f(x); } |
| 31 | + /// ``` |
| 32 | + #[clippy::version = "1.66.0"] |
| 33 | + pub SEMICOLON_INSIDE_BLOCK, |
| 34 | + restriction, |
| 35 | + "add a semicolon inside the block" |
| 36 | +} |
| 37 | +declare_clippy_lint! { |
| 38 | + /// ### What it does |
| 39 | + /// |
| 40 | + /// For () returning expressions, check that the semicolon is outside the block. |
| 41 | + /// |
| 42 | + /// ### Why is this bad? |
| 43 | + /// |
| 44 | + /// For consistency it's best to have the semicolon inside/outside the block. Either way is fine and this lint suggests outside the block. |
| 45 | + /// Take a look at `semicolon_inside_block` for the other alternative. |
| 46 | + /// |
| 47 | + /// ### Example |
| 48 | + /// |
| 49 | + /// ```rust |
| 50 | + /// # fn f(_: u32) {} |
| 51 | + /// # let x = 0; |
| 52 | + /// unsafe { f(x); } |
| 53 | + /// ``` |
| 54 | + /// Use instead: |
| 55 | + /// ```rust |
| 56 | + /// # fn f(_: u32) {} |
| 57 | + /// # let x = 0; |
| 58 | + /// unsafe { f(x) }; |
| 59 | + /// ``` |
| 60 | + #[clippy::version = "1.66.0"] |
| 61 | + pub SEMICOLON_OUTSIDE_BLOCK, |
| 62 | + restriction, |
| 63 | + "add a semicolon outside the block" |
| 64 | +} |
| 65 | +declare_lint_pass!(SemicolonBlock => [SEMICOLON_INSIDE_BLOCK, SEMICOLON_OUTSIDE_BLOCK]); |
| 66 | + |
| 67 | +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; |
| 88 | + } |
| 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 | + } |
| 100 | +} |
| 101 | + |
| 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 | + let expr_snip = snippet_with_macro_callsite(cx, expr.span, ".."); |
| 109 | + |
| 110 | + let mut suggestion: String = snippet_with_macro_callsite(cx, block.span, "..").to_string(); |
| 111 | + |
| 112 | + if let Some((expr_offset, _)) = suggestion.rmatch_indices(&*expr_snip).next() |
| 113 | + && let Some(semi_offset) = suggestion[expr_offset + expr_snip.len()..].find(';') { |
| 114 | + suggestion.remove(expr_offset + expr_snip.len() + semi_offset); |
| 115 | + } else { |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + suggestion.push(';'); |
| 120 | + |
| 121 | + span_lint_and_sugg( |
| 122 | + cx, |
| 123 | + SEMICOLON_OUTSIDE_BLOCK, |
| 124 | + block.span, |
| 125 | + "consider moving the `;` outside the block for consistent formatting", |
| 126 | + "put the `;` outside the block", |
| 127 | + suggestion, |
| 128 | + Applicability::MaybeIncorrect, |
| 129 | + ); |
| 130 | + } |
| 131 | +} |
0 commit comments