|
| 1 | +use crate::rustc_lint::LintContext; |
| 2 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 3 | +use clippy_utils::in_macro; |
| 4 | +use clippy_utils::source::snippet_with_macro_callsite; |
| 5 | +use if_chain::if_chain; |
| 6 | +use rustc_errors::Applicability; |
| 7 | +use rustc_hir::ExprKind; |
| 8 | +use rustc_hir::{Block, StmtKind, ItemKind}; |
| 9 | +use rustc_lint::{LateContext, LateLintPass}; |
| 10 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 11 | +use rustc_span::BytePos; |
| 12 | +use rustc_span::Span; |
| 13 | +use std::ops::Add; |
| 14 | + |
| 15 | +declare_clippy_lint! { |
| 16 | + /// **What it does:** For () returning expressions, check that the semicolon is outside the block. |
| 17 | + /// |
| 18 | + /// **Why is this bad?** For consistency it's best to have the semicolon inside/outside the block. Either way is fine and this lint suggests outside the block. |
| 19 | + /// Take a look at `semicolon_inside_block` for the other alternative. |
| 20 | + /// |
| 21 | + /// **Known problems:** None. |
| 22 | + /// |
| 23 | + /// **Example:** |
| 24 | + /// |
| 25 | + /// ```rust |
| 26 | + /// unsafe { f(x); } |
| 27 | + /// ``` |
| 28 | + /// Use instead: |
| 29 | + /// ```rust |
| 30 | + /// unsafe { f(x) }; |
| 31 | + /// ``` |
| 32 | + pub SEMICOLON_OUTSIDE_BLOCK, |
| 33 | + pedantic, |
| 34 | + "add a semicolon outside the block" |
| 35 | +} |
| 36 | + |
| 37 | +declare_lint_pass!(SemicolonOutsideBlock => [SEMICOLON_OUTSIDE_BLOCK]); |
| 38 | + |
| 39 | +impl LateLintPass<'_> for SemicolonOutsideBlock { |
| 40 | + fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>) { |
| 41 | + if_chain! { |
| 42 | + if !in_macro(block.span); |
| 43 | + if block.expr.is_none(); |
| 44 | + if let Some(last) = block.stmts.last(); |
| 45 | + if let StmtKind::Semi(expr) = last.kind; |
| 46 | + let t_expr = cx.typeck_results().expr_ty(expr); |
| 47 | + if t_expr.is_unit(); |
| 48 | + |
| 49 | + // make sure that the block does not belong to a function |
| 50 | + let parent_item_id = cx.tcx.hir().get_parent_item(block.hir_id); |
| 51 | + let parent_item = cx.tcx.hir().expect_item(parent_item_id); |
| 52 | + if let ItemKind::Fn(_, _, body_id) = parent_item.kind; |
| 53 | + let item_body = cx.tcx.hir().body(body_id); |
| 54 | + if let ExprKind::Block(fn_block, _) = item_body.value.kind; |
| 55 | + if fn_block.hir_id != block.hir_id; |
| 56 | + then { |
| 57 | + // filter out other blocks and the desugared for loop |
| 58 | + if let ExprKind::Block(..) | ExprKind::DropTemps(..) = expr.kind { return } |
| 59 | + |
| 60 | + // make sure we're also having the semicolon at the end of the expression... |
| 61 | + let expr_w_sem = expand_span_to_semicolon(cx, expr.span); |
| 62 | + let expr_snip = snippet_with_macro_callsite(cx, expr_w_sem, ".."); |
| 63 | + let mut expr_sugg = expr_snip.to_string(); |
| 64 | + expr_sugg.pop(); |
| 65 | + |
| 66 | + // and the block |
| 67 | + let block_w_sem = expand_span_to_semicolon(cx, block.span); |
| 68 | + let mut block_snip: String = snippet_with_macro_callsite(cx, block_w_sem, "..").to_string(); |
| 69 | + if block_snip.ends_with('\n') { |
| 70 | + block_snip.pop(); |
| 71 | + } |
| 72 | + |
| 73 | + // retrieve the suggestion |
| 74 | + let suggestion = if block_snip.ends_with(';') { |
| 75 | + block_snip.replace(expr_snip.as_ref(), &format!("{}", expr_sugg.as_str())) |
| 76 | + } else { |
| 77 | + format!("{};", block_snip.replace(expr_snip.as_ref(), &format!("{}", expr_sugg.as_str()))) |
| 78 | + }; |
| 79 | + |
| 80 | + span_lint_and_sugg( |
| 81 | + cx, |
| 82 | + SEMICOLON_OUTSIDE_BLOCK, |
| 83 | + if block_snip.ends_with(';') { |
| 84 | + block_w_sem |
| 85 | + } else { |
| 86 | + block.span |
| 87 | + }, |
| 88 | + "consider moving the `;` outside the block for consistent formatting", |
| 89 | + "put the `;` outside the block", |
| 90 | + suggestion, |
| 91 | + Applicability::MaybeIncorrect, |
| 92 | + ); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +/// Takes a span and extzends it until after a semicolon in the last line of the span. |
| 99 | +fn expand_span_to_semicolon<'tcx>(cx: &LateContext<'tcx>, expr_span: Span) -> Span { |
| 100 | + let expr_span_with_sem = cx.sess().source_map().span_extend_to_next_char(expr_span, ';', false); |
| 101 | + expr_span_with_sem.with_hi(expr_span_with_sem.hi().add(BytePos(1))) |
| 102 | +} |
0 commit comments