1 use crate::utils::{in_macro, span_lint_and_sugg, sugg};
2 use clippy_utils::source::snippet_with_macro_callsite;
3 use if_chain::if_chain;
4 use rustc_errors::Applicability;
5 use rustc_hir::{Block, ExprKind};
6 use rustc_lint::{LateContext, LateLintPass};
7 use rustc_session::{declare_lint_pass, declare_tool_lint};
10 /// **What it does:** Looks for blocks of expressions and fires if the last expression returns `()`
11 /// but is not followed by a semicolon.
13 /// **Why is this bad?** The semicolon might be optional but when
14 /// extending the block with new code, it doesn't require a change in previous last line.
16 /// **Known problems:** None.
22 /// println!("Hello world")
28 /// println!("Hello world");
31 pub SEMICOLON_IF_NOTHING_RETURNED,
33 "add a semicolon if nothing is returned"
36 declare_lint_pass!(SemicolonIfNothingReturned => [SEMICOLON_IF_NOTHING_RETURNED]);
38 impl LateLintPass<'_> for SemicolonIfNothingReturned {
39 fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>) {
41 if !in_macro(block.span);
42 if let Some(expr) = block.expr;
43 let t_expr = cx.typeck_results().expr_ty(expr);
45 if let snippet = snippet_with_macro_callsite(cx, expr.span, "}");
46 if !snippet.ends_with('}');
48 // filter out the desugared `for` loop
49 if let ExprKind::DropTemps(..) = &expr.kind {
53 let sugg = sugg::Sugg::hir_with_macro_callsite(cx, &expr, "..");
54 let suggestion = format!("{0};", sugg);
57 SEMICOLON_IF_NOTHING_RETURNED,
58 expr.span.source_callsite(),
59 "consider adding a `;` to the last statement for consistent formatting",
62 Applicability::MaybeIncorrect,