@@ -68,17 +68,73 @@ impl_lint_pass!(SemicolonBlock => [SEMICOLON_INSIDE_BLOCK, SEMICOLON_OUTSIDE_BLO
6868
6969#[ derive( Copy , Clone ) ]
7070pub struct SemicolonBlock {
71- semicolon_inside_block_if_multiline : bool ,
72- semicolon_outside_block_if_singleline : bool ,
71+ semicolon_inside_block_ignore_singleline : bool ,
72+ semicolon_outside_block_ignore_multiline : bool ,
7373}
7474
7575impl SemicolonBlock {
76- pub fn new ( semicolon_inside_block_if_multiline : bool , semicolon_outside_block_if_singleline : bool ) -> Self {
76+ pub fn new ( semicolon_inside_block_ignore_singleline : bool , semicolon_outside_block_ignore_multiline : bool ) -> Self {
7777 Self {
78- semicolon_inside_block_if_multiline ,
79- semicolon_outside_block_if_singleline ,
78+ semicolon_inside_block_ignore_singleline ,
79+ semicolon_outside_block_ignore_multiline ,
8080 }
8181 }
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+ }
82138}
83139
84140impl LateLintPass < ' _ > for SemicolonBlock {
@@ -98,81 +154,19 @@ impl LateLintPass<'_> for SemicolonBlock {
98154 span,
99155 ..
100156 } = stmt else { return } ;
101- semicolon_outside_block ( self , cx, block, expr, span) ;
157+ self . semicolon_outside_block ( cx, block, expr, span) ;
102158 } ,
103159 StmtKind :: Semi ( Expr {
104160 kind : ExprKind :: Block ( block @ Block { expr : Some ( tail) , .. } , _) ,
105161 ..
106162 } ) if !block. span . from_expansion ( ) => {
107- semicolon_inside_block ( self , cx, block, tail, stmt. span ) ;
163+ self . semicolon_inside_block ( cx, block, tail, stmt. span ) ;
108164 } ,
109165 _ => ( ) ,
110166 }
111167 }
112168}
113169
114- fn semicolon_inside_block (
115- conf : & mut SemicolonBlock ,
116- cx : & LateContext < ' _ > ,
117- block : & Block < ' _ > ,
118- tail : & Expr < ' _ > ,
119- semi_span : Span ,
120- ) {
121- let insert_span = tail. span . source_callsite ( ) . shrink_to_hi ( ) ;
122- let remove_span = semi_span. with_lo ( block. span . hi ( ) ) ;
123-
124- if conf. semicolon_inside_block_if_multiline && get_line ( cx, remove_span) == get_line ( cx, insert_span) {
125- return ;
126- }
127-
128- span_lint_and_then (
129- cx,
130- SEMICOLON_INSIDE_BLOCK ,
131- semi_span,
132- "consider moving the `;` inside the block for consistent formatting" ,
133- |diag| {
134- multispan_sugg_with_applicability (
135- diag,
136- "put the `;` here" ,
137- Applicability :: MachineApplicable ,
138- [ ( remove_span, String :: new ( ) ) , ( insert_span, ";" . to_owned ( ) ) ] ,
139- ) ;
140- } ,
141- ) ;
142- }
143-
144- fn semicolon_outside_block (
145- conf : & mut SemicolonBlock ,
146- cx : & LateContext < ' _ > ,
147- block : & Block < ' _ > ,
148- tail_stmt_expr : & Expr < ' _ > ,
149- semi_span : Span ,
150- ) {
151- let insert_span = block. span . with_lo ( block. span . hi ( ) ) ;
152- // account for macro calls
153- let semi_span = cx. sess ( ) . source_map ( ) . stmt_span ( semi_span, block. span ) ;
154- let remove_span = semi_span. with_lo ( tail_stmt_expr. span . source_callsite ( ) . hi ( ) ) ;
155-
156- if conf. semicolon_outside_block_if_singleline && get_line ( cx, remove_span) != get_line ( cx, insert_span) {
157- return ;
158- }
159-
160- span_lint_and_then (
161- cx,
162- SEMICOLON_OUTSIDE_BLOCK ,
163- block. span ,
164- "consider moving the `;` outside the block for consistent formatting" ,
165- |diag| {
166- multispan_sugg_with_applicability (
167- diag,
168- "put the `;` here" ,
169- Applicability :: MachineApplicable ,
170- [ ( remove_span, String :: new ( ) ) , ( insert_span, ";" . to_owned ( ) ) ] ,
171- ) ;
172- } ,
173- ) ;
174- }
175-
176170fn get_line ( cx : & LateContext < ' _ > , span : Span ) -> Option < usize > {
177171 if let Ok ( line) = cx. sess ( ) . source_map ( ) . lookup_line ( span. lo ( ) ) {
178172 return Some ( line. line ) ;
0 commit comments