1
+ use clippy_utils:: diagnostics:: span_lint;
2
+ use clippy_utils:: source:: SpanRangeExt ;
1
3
use rustc_hir as hir;
2
4
use rustc_hir:: intravisit:: FnKind ;
3
5
use rustc_lint:: { LateContext , LintContext } ;
4
6
use rustc_middle:: lint:: in_external_macro;
5
7
use rustc_span:: Span ;
6
8
7
- use clippy_utils:: diagnostics:: span_lint;
8
- use clippy_utils:: source:: snippet_opt;
9
-
10
9
use super :: TOO_MANY_LINES ;
11
10
12
11
pub ( super ) fn check_fn (
@@ -22,57 +21,57 @@ pub(super) fn check_fn(
22
21
return ;
23
22
}
24
23
25
- let Some ( code_snippet) = snippet_opt ( cx, body. value . span ) else {
26
- return ;
27
- } ;
28
24
let mut line_count: u64 = 0 ;
29
- let mut in_comment = false ;
30
- let mut code_in_line;
25
+ let too_many = body. value . span . check_source_text ( cx, |src| {
26
+ let mut in_comment = false ;
27
+ let mut code_in_line;
31
28
32
- let function_lines = if matches ! ( body. value. kind, hir:: ExprKind :: Block ( ..) )
33
- && code_snippet . as_bytes ( ) . first ( ) . copied ( ) == Some ( b'{' )
34
- && code_snippet . as_bytes ( ) . last ( ) . copied ( ) == Some ( b'}' )
35
- {
36
- // Removing the braces from the enclosing block
37
- & code_snippet [ 1 ..code_snippet . len ( ) - 1 ]
38
- } else {
39
- & code_snippet
40
- }
41
- . trim ( ) // Remove leading and trailing blank lines
42
- . lines ( ) ;
29
+ let function_lines = if matches ! ( body. value. kind, hir:: ExprKind :: Block ( ..) )
30
+ && src . as_bytes ( ) . first ( ) . copied ( ) == Some ( b'{' )
31
+ && src . as_bytes ( ) . last ( ) . copied ( ) == Some ( b'}' )
32
+ {
33
+ // Removing the braces from the enclosing block
34
+ & src [ 1 ..src . len ( ) - 1 ]
35
+ } else {
36
+ src
37
+ }
38
+ . trim ( ) // Remove leading and trailing blank lines
39
+ . lines ( ) ;
43
40
44
- for mut line in function_lines {
45
- code_in_line = false ;
46
- loop {
47
- line = line. trim_start ( ) ;
48
- if line. is_empty ( ) {
49
- break ;
50
- }
51
- if in_comment {
52
- if let Some ( i) = line. find ( "*/" ) {
53
- line = & line[ i + 2 ..] ;
54
- in_comment = false ;
55
- continue ;
41
+ for mut line in function_lines {
42
+ code_in_line = false ;
43
+ loop {
44
+ line = line. trim_start ( ) ;
45
+ if line. is_empty ( ) {
46
+ break ;
56
47
}
57
- } else {
58
- let multi_idx = line. find ( "/*" ) . unwrap_or ( line. len ( ) ) ;
59
- let single_idx = line. find ( "//" ) . unwrap_or ( line. len ( ) ) ;
60
- code_in_line |= multi_idx > 0 && single_idx > 0 ;
61
- // Implies multi_idx is below line.len()
62
- if multi_idx < single_idx {
63
- line = & line[ multi_idx + 2 ..] ;
64
- in_comment = true ;
65
- continue ;
48
+ if in_comment {
49
+ if let Some ( i) = line. find ( "*/" ) {
50
+ line = & line[ i + 2 ..] ;
51
+ in_comment = false ;
52
+ continue ;
53
+ }
54
+ } else {
55
+ let multi_idx = line. find ( "/*" ) . unwrap_or ( line. len ( ) ) ;
56
+ let single_idx = line. find ( "//" ) . unwrap_or ( line. len ( ) ) ;
57
+ code_in_line |= multi_idx > 0 && single_idx > 0 ;
58
+ // Implies multi_idx is below line.len()
59
+ if multi_idx < single_idx {
60
+ line = & line[ multi_idx + 2 ..] ;
61
+ in_comment = true ;
62
+ continue ;
63
+ }
66
64
}
65
+ break ;
66
+ }
67
+ if code_in_line {
68
+ line_count += 1 ;
67
69
}
68
- break ;
69
- }
70
- if code_in_line {
71
- line_count += 1 ;
72
70
}
73
- }
71
+ line_count > too_many_lines_threshold
72
+ } ) ;
74
73
75
- if line_count > too_many_lines_threshold {
74
+ if too_many {
76
75
span_lint (
77
76
cx,
78
77
TOO_MANY_LINES ,
0 commit comments