|
| 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::{Block, ExprKind}; |
| 8 | +use rustc_lint::{LateContext, LateLintPass}; |
| 9 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 10 | + |
| 11 | +declare_clippy_lint! { |
| 12 | + /// **What it does:** For () returning expressions, check that the semicolon is inside the block. |
| 13 | + /// |
| 14 | + /// **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 inside the block. |
| 15 | + /// Take a look at `semicolon_outside_block` for the other alternative. |
| 16 | + /// |
| 17 | + /// **Known problems:** None. |
| 18 | + /// |
| 19 | + /// **Example:** |
| 20 | + /// |
| 21 | + /// ```rust |
| 22 | + /// unsafe { f(x) }; |
| 23 | + /// ``` |
| 24 | + /// Use instead: |
| 25 | + /// ```rust |
| 26 | + /// unsafe { f(x); } |
| 27 | + /// ``` |
| 28 | + pub SEMICOLON_INSIDE_BLOCK, |
| 29 | + pedantic, |
| 30 | + "add a semicolon inside the block" |
| 31 | +} |
| 32 | + |
| 33 | +declare_lint_pass!(SemicolonInsideBlock => [SEMICOLON_INSIDE_BLOCK]); |
| 34 | + |
| 35 | +impl LateLintPass<'_> for SemicolonInsideBlock { |
| 36 | + fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>) { |
| 37 | + if_chain! { |
| 38 | + if !in_macro(block.span); |
| 39 | + if let Some(expr) = block.expr; |
| 40 | + let t_expr = cx.typeck_results().expr_ty(expr); |
| 41 | + if t_expr.is_unit(); |
| 42 | + if let snippet = snippet_with_macro_callsite(cx, expr.span, "}"); |
| 43 | + if !snippet.ends_with("};") && !snippet.ends_with('}'); |
| 44 | + then { |
| 45 | + // filter out the desugared `for` loop |
| 46 | + if let ExprKind::DropTemps(..) = &expr.kind { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + let expr_snip = snippet_with_macro_callsite(cx, expr.span, ".."); |
| 51 | + |
| 52 | + // check for the right suggestion and span, differs if the block spans |
| 53 | + // multiple lines |
| 54 | + let (suggestion, span) = if cx.sess().source_map().is_multiline(block.span) { |
| 55 | + (format!("{};", expr_snip), expr.span.source_callsite()) |
| 56 | + } else { |
| 57 | + let block_with_pot_sem = cx.sess().source_map().span_extend_to_next_char(block.span, '\n', false); |
| 58 | + let block_snip = snippet_with_macro_callsite(cx, block.span, ".."); |
| 59 | + |
| 60 | + (block_snip.replace(expr_snip.as_ref(), &format!("{};", expr_snip)), block_with_pot_sem) |
| 61 | + }; |
| 62 | + |
| 63 | + span_lint_and_sugg( |
| 64 | + cx, |
| 65 | + SEMICOLON_INSIDE_BLOCK, |
| 66 | + span, |
| 67 | + "consider moving the `;` inside the block for consistent formatting", |
| 68 | + "put the `;` here", |
| 69 | + suggestion, |
| 70 | + Applicability::MaybeIncorrect, |
| 71 | + ); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments