]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/semicolon_if_nothing_returned.rs
Fixed failing tests
[rust.git] / clippy_lints / src / semicolon_if_nothing_returned.rs
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, in_macro, spans_on_same_line, sugg};
4 use if_chain::if_chain;
5 use rustc_errors::Applicability;
6 use rustc_hir::Expr;
7 use rustc_hir::{Block, BlockCheckMode, 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:** Looks for blocks of expressions and fires if the last expression returns
13     /// `()` but is not followed by a semicolon.
14     ///
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.
17     ///
18     /// **Known problems:** None.
19     ///
20     /// **Example:**
21     ///
22     /// ```rust
23     /// fn main() {
24     ///     println!("Hello world")
25     /// }
26     /// ```
27     /// Use instead:
28     /// ```rust
29     /// fn main() {
30     ///     println!("Hello world");
31     /// }
32     /// ```
33     pub SEMICOLON_IF_NOTHING_RETURNED,
34     pedantic,
35     "add a semicolon if nothing is returned"
36 }
37
38 declare_lint_pass!(SemicolonIfNothingReturned => [SEMICOLON_IF_NOTHING_RETURNED]);
39
40 impl LateLintPass<'_> for SemicolonIfNothingReturned {
41     fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>) {
42         if_chain! {
43             if !in_macro(block.span);
44             if let Some(expr) = block.expr;
45             let t_expr = cx.typeck_results().expr_ty(expr);
46             if t_expr.is_unit();
47             if let snippet = snippet_with_macro_callsite(cx, expr.span, "}");
48             if !snippet.ends_with('}');
49             if !check_if_inside_block_on_same_line(cx, block, expr);
50             then {
51                 // filter out the desugared `for` loop
52                 if let ExprKind::DropTemps(..) = &expr.kind {
53                     return;
54                 }
55
56                 let sugg = sugg::Sugg::hir_with_macro_callsite(cx, expr, "..");
57                 let suggestion = format!("{0};", sugg);
58                 span_lint_and_sugg(
59                     cx,
60                     SEMICOLON_IF_NOTHING_RETURNED,
61                     expr.span.source_callsite(),
62                     "consider adding a `;` to the last statement for consistent formatting",
63                     "add a `;` here",
64                     suggestion,
65                     Applicability::MaybeIncorrect,
66                 );
67             }
68         }
69     }
70 }
71
72 /// Check if this block is inside a closure or an unsafe block or a normal on the same line.
73 fn check_if_inside_block_on_same_line<'tcx>(
74     cx: &LateContext<'tcx>,
75     block: &'tcx Block<'tcx>,
76     last_expr: &'tcx Expr<'_>,
77 ) -> bool {
78     if_chain! {
79         if let Some(parent) = get_parent_expr_for_hir(cx, block.hir_id);
80
81         if !matches!(block.rules, BlockCheckMode::DefaultBlock) ||
82         matches!(parent.kind, ExprKind::Closure(..) | ExprKind::Block(..));
83
84         if block.stmts.is_empty();
85         then {
86             return spans_on_same_line(cx, parent.span, last_expr.span);
87         }
88     }
89     false
90 }