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