1 use crate::rustc_lint::LintContext;
2 use clippy_utils::diagnostics::span_lint_and_sugg;
3 use clippy_utils::source::snippet_with_macro_callsite;
4 use clippy_utils::{in_macro, sugg};
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};
11 declare_clippy_lint! {
12 /// **What it does:** Looks for blocks of expressions and fires if the last expression returns
13 /// `()` but is not followed by a semicolon.
15 /// **Why is this bad?** The semicolon might be optional but when extending the block with new
16 /// code, it doesn't require a change in previous last line.
18 /// **Known problems:** None.
24 /// println!("Hello world")
30 /// println!("Hello world");
33 pub SEMICOLON_IF_NOTHING_RETURNED,
35 "add a semicolon if nothing is returned"
38 declare_lint_pass!(SemicolonIfNothingReturned => [SEMICOLON_IF_NOTHING_RETURNED]);
40 impl LateLintPass<'_> for SemicolonIfNothingReturned {
41 fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>) {
43 if !in_macro(block.span);
44 if let Some(expr) = block.expr;
45 let t_expr = cx.typeck_results().expr_ty(expr);
47 if let snippet = snippet_with_macro_callsite(cx, expr.span, "}");
48 if !snippet.ends_with('}');
49 if cx.sess().source_map().is_multiline(block.span);
51 // filter out the desugared `for` loop
52 if let ExprKind::DropTemps(..) = &expr.kind {
56 let sugg = sugg::Sugg::hir_with_macro_callsite(cx, expr, "..");
57 let suggestion = format!("{0};", sugg);
60 SEMICOLON_IF_NOTHING_RETURNED,
61 expr.span.source_callsite(),
62 "consider adding a `;` to the last statement for consistent formatting",
65 Applicability::MaybeIncorrect,