]>
Commit | Line | Data |
---|---|---|
9c376795 FG |
1 | use clippy_utils::diagnostics::{multispan_sugg_with_applicability, span_lint_and_then}; |
2 | use rustc_errors::Applicability; | |
3 | use rustc_hir::{Block, Expr, ExprKind, Stmt, StmtKind}; | |
4 | use rustc_lint::{LateContext, LateLintPass, LintContext}; | |
4b012472 | 5 | use rustc_session::impl_lint_pass; |
9c376795 FG |
6 | use rustc_span::Span; |
7 | ||
8 | declare_clippy_lint! { | |
9 | /// ### What it does | |
10 | /// | |
11 | /// Suggests moving the semicolon after a block to the inside of the block, after its last | |
12 | /// expression. | |
13 | /// | |
31ef2f64 | 14 | /// ### Why restrict this? |
9c376795 FG |
15 | /// For consistency it's best to have the semicolon inside/outside the block. Either way is fine |
16 | /// and this lint suggests inside the block. | |
17 | /// Take a look at `semicolon_outside_block` for the other alternative. | |
18 | /// | |
19 | /// ### Example | |
20 | /// | |
ed00b5ec | 21 | /// ```no_run |
9c376795 FG |
22 | /// # fn f(_: u32) {} |
23 | /// # let x = 0; | |
24 | /// unsafe { f(x) }; | |
25 | /// ``` | |
26 | /// Use instead: | |
ed00b5ec | 27 | /// ```no_run |
9c376795 FG |
28 | /// # fn f(_: u32) {} |
29 | /// # let x = 0; | |
30 | /// unsafe { f(x); } | |
31 | /// ``` | |
9ffffee4 | 32 | #[clippy::version = "1.68.0"] |
9c376795 FG |
33 | pub SEMICOLON_INSIDE_BLOCK, |
34 | restriction, | |
35 | "add a semicolon inside the block" | |
36 | } | |
37 | declare_clippy_lint! { | |
38 | /// ### What it does | |
39 | /// | |
40 | /// Suggests moving the semicolon from a block's final expression outside of the block. | |
41 | /// | |
31ef2f64 | 42 | /// ### Why restrict this? |
9c376795 FG |
43 | /// For consistency it's best to have the semicolon inside/outside the block. Either way is fine |
44 | /// and this lint suggests outside the block. | |
45 | /// Take a look at `semicolon_inside_block` for the other alternative. | |
46 | /// | |
47 | /// ### Example | |
48 | /// | |
ed00b5ec | 49 | /// ```no_run |
9c376795 FG |
50 | /// # fn f(_: u32) {} |
51 | /// # let x = 0; | |
52 | /// unsafe { f(x); } | |
53 | /// ``` | |
54 | /// Use instead: | |
ed00b5ec | 55 | /// ```no_run |
9c376795 FG |
56 | /// # fn f(_: u32) {} |
57 | /// # let x = 0; | |
58 | /// unsafe { f(x) }; | |
59 | /// ``` | |
9ffffee4 | 60 | #[clippy::version = "1.68.0"] |
9c376795 FG |
61 | pub SEMICOLON_OUTSIDE_BLOCK, |
62 | restriction, | |
63 | "add a semicolon outside the block" | |
64 | } | |
49aad941 FG |
65 | impl_lint_pass!(SemicolonBlock => [SEMICOLON_INSIDE_BLOCK, SEMICOLON_OUTSIDE_BLOCK]); |
66 | ||
67 | #[derive(Copy, Clone)] | |
68 | pub struct SemicolonBlock { | |
69 | semicolon_inside_block_ignore_singleline: bool, | |
70 | semicolon_outside_block_ignore_multiline: bool, | |
71 | } | |
72 | ||
73 | impl SemicolonBlock { | |
74 | pub fn new(semicolon_inside_block_ignore_singleline: bool, semicolon_outside_block_ignore_multiline: bool) -> Self { | |
75 | Self { | |
76 | semicolon_inside_block_ignore_singleline, | |
77 | semicolon_outside_block_ignore_multiline, | |
78 | } | |
79 | } | |
80 | ||
81 | fn semicolon_inside_block(self, cx: &LateContext<'_>, block: &Block<'_>, tail: &Expr<'_>, semi_span: Span) { | |
82 | let insert_span = tail.span.source_callsite().shrink_to_hi(); | |
83 | let remove_span = semi_span.with_lo(block.span.hi()); | |
84 | ||
85 | if self.semicolon_inside_block_ignore_singleline && get_line(cx, remove_span) == get_line(cx, insert_span) { | |
86 | return; | |
87 | } | |
88 | ||
89 | span_lint_and_then( | |
90 | cx, | |
91 | SEMICOLON_INSIDE_BLOCK, | |
92 | semi_span, | |
93 | "consider moving the `;` inside the block for consistent formatting", | |
94 | |diag| { | |
95 | multispan_sugg_with_applicability( | |
96 | diag, | |
97 | "put the `;` here", | |
98 | Applicability::MachineApplicable, | |
99 | [(remove_span, String::new()), (insert_span, ";".to_owned())], | |
100 | ); | |
101 | }, | |
102 | ); | |
103 | } | |
104 | ||
105 | fn semicolon_outside_block( | |
106 | self, | |
107 | cx: &LateContext<'_>, | |
108 | block: &Block<'_>, | |
109 | tail_stmt_expr: &Expr<'_>, | |
110 | semi_span: Span, | |
111 | ) { | |
112 | let insert_span = block.span.with_lo(block.span.hi()); | |
113 | // account for macro calls | |
114 | let semi_span = cx.sess().source_map().stmt_span(semi_span, block.span); | |
115 | let remove_span = semi_span.with_lo(tail_stmt_expr.span.source_callsite().hi()); | |
116 | ||
117 | if self.semicolon_outside_block_ignore_multiline && get_line(cx, remove_span) != get_line(cx, insert_span) { | |
118 | return; | |
119 | } | |
120 | ||
121 | span_lint_and_then( | |
122 | cx, | |
123 | SEMICOLON_OUTSIDE_BLOCK, | |
124 | block.span, | |
125 | "consider moving the `;` outside the block for consistent formatting", | |
126 | |diag| { | |
127 | multispan_sugg_with_applicability( | |
128 | diag, | |
129 | "put the `;` here", | |
130 | Applicability::MachineApplicable, | |
131 | [(remove_span, String::new()), (insert_span, ";".to_owned())], | |
132 | ); | |
133 | }, | |
134 | ); | |
135 | } | |
136 | } | |
9c376795 FG |
137 | |
138 | impl LateLintPass<'_> for SemicolonBlock { | |
139 | fn check_stmt(&mut self, cx: &LateContext<'_>, stmt: &Stmt<'_>) { | |
140 | match stmt.kind { | |
141 | StmtKind::Expr(Expr { | |
142 | kind: ExprKind::Block(block, _), | |
143 | .. | |
144 | }) if !block.span.from_expansion() => { | |
145 | let Block { | |
146 | expr: None, | |
147 | stmts: [.., stmt], | |
148 | .. | |
add651ee FG |
149 | } = block |
150 | else { | |
151 | return; | |
152 | }; | |
9c376795 FG |
153 | let &Stmt { |
154 | kind: StmtKind::Semi(expr), | |
155 | span, | |
156 | .. | |
add651ee FG |
157 | } = stmt |
158 | else { | |
159 | return; | |
160 | }; | |
49aad941 | 161 | self.semicolon_outside_block(cx, block, expr, span); |
9c376795 FG |
162 | }, |
163 | StmtKind::Semi(Expr { | |
164 | kind: ExprKind::Block(block @ Block { expr: Some(tail), .. }, _), | |
165 | .. | |
49aad941 FG |
166 | }) if !block.span.from_expansion() => { |
167 | self.semicolon_inside_block(cx, block, tail, stmt.span); | |
168 | }, | |
9c376795 FG |
169 | _ => (), |
170 | } | |
171 | } | |
172 | } | |
173 | ||
49aad941 FG |
174 | fn get_line(cx: &LateContext<'_>, span: Span) -> Option<usize> { |
175 | if let Ok(line) = cx.sess().source_map().lookup_line(span.lo()) { | |
176 | return Some(line.line); | |
177 | } | |
9c376795 | 178 | |
49aad941 | 179 | None |
9c376795 | 180 | } |