@@ -40,6 +40,13 @@ declare_clippy_lint! {
40
40
///
41
41
/// **Known problems:** None.
42
42
///
43
+ /// **Example:**
44
+ /// ``` rust
45
+ /// fn im_too_long() {
46
+ /// println!("");
47
+ /// // ... 100 more LoC
48
+ /// println!("");
49
+ /// }
43
50
/// ```
44
51
declare_clippy_lint ! {
45
52
pub TOO_MANY_LINES ,
@@ -174,46 +181,57 @@ impl<'a, 'tcx> Functions {
174
181
}
175
182
}
176
183
177
- fn check_line_number ( self , cx : & LateContext , span : Span ) {
184
+ fn check_line_number ( self , cx : & LateContext < ' _ , ' _ > , span : Span ) {
178
185
let code_snippet = snippet ( cx, span, ".." ) ;
179
- let mut line_count = 0 ;
186
+ let mut line_count: u64 = 0 ;
180
187
let mut in_comment = false ;
181
- for mut line in code_snippet. lines ( ) {
182
- if in_comment {
183
- let end_comment_loc = match line. find ( "*/" ) {
184
- Some ( i) => i,
185
- None => continue
186
- } ;
187
- in_comment = false ;
188
- line = & line[ end_comment_loc..] ;
189
- }
190
- line = line. trim_left ( ) ;
191
- if line. is_empty ( ) || line. starts_with ( "//" ) { continue ; }
192
- if line. contains ( "/*" ) {
193
- let mut count_line: bool = !line. starts_with ( "/*" ) ;
194
- let close_counts = line. match_indices ( "*/" ) . count ( ) ;
195
- let open_counts = line. match_indices ( "/*" ) . count ( ) ;
188
+ let mut code_in_line;
196
189
197
- if close_counts > 1 || open_counts > 1 {
198
- line_count += 1 ;
199
- } else if close_counts == 1 {
190
+ // Skip the surrounding function decl.
191
+ let start_brace_idx = match code_snippet. find ( "{" ) {
192
+ Some ( i) => i + 1 ,
193
+ None => 0
194
+ } ;
195
+ let end_brace_idx = match code_snippet. find ( "}" ) {
196
+ Some ( i) => i,
197
+ None => code_snippet. len ( )
198
+ } ;
199
+ let function_lines = code_snippet[ start_brace_idx..end_brace_idx] . lines ( ) ;
200
+
201
+ for mut line in function_lines {
202
+ code_in_line = false ;
203
+ loop {
204
+ line = line. trim_start ( ) ;
205
+ if line. is_empty ( ) { break ; }
206
+ if in_comment {
200
207
match line. find ( "*/" ) {
201
208
Some ( i) => {
202
- line = line[ i..] . trim_left ( ) ;
203
- if !line. is_empty ( ) && !line. starts_with ( "//" ) {
204
- count_line = true ;
205
- }
209
+ line = & line[ i + 2 ..] ;
210
+ in_comment = false ;
211
+ continue ;
206
212
} ,
207
- None => continue
213
+ None => break
208
214
}
209
215
} else {
210
- in_comment = true ;
216
+ let multi_idx = match line. find ( "/*" ) {
217
+ Some ( i) => i,
218
+ None => line. len ( )
219
+ } ;
220
+ let single_idx = match line. find ( "//" ) {
221
+ Some ( i) => i,
222
+ None => line. len ( )
223
+ } ;
224
+ code_in_line |= multi_idx > 0 && single_idx > 0 ;
225
+ // Implies multi_idx is below line.len()
226
+ if multi_idx < single_idx {
227
+ line = & line[ multi_idx + 2 ..] ;
228
+ in_comment = true ;
229
+ continue ;
230
+ }
231
+ break ;
211
232
}
212
- if count_line { line_count += 1 ; }
213
- } else {
214
- // No multipart comment, no single comment, non-empty string.
215
- line_count += 1 ;
216
233
}
234
+ if code_in_line { line_count += 1 ; }
217
235
}
218
236
219
237
if line_count > self . max_lines {
0 commit comments