@@ -4,7 +4,7 @@ use rustc_middle::lint::in_external_macro;
4
4
use rustc_span:: Span ;
5
5
6
6
use clippy_utils:: diagnostics:: span_lint;
7
- use clippy_utils:: source:: snippet ;
7
+ use clippy_utils:: source:: snippet_opt ;
8
8
9
9
use super :: TOO_MANY_LINES ;
10
10
@@ -13,15 +13,25 @@ pub(super) fn check_fn(cx: &LateContext<'_>, span: Span, body: &'tcx hir::Body<'
13
13
return ;
14
14
}
15
15
16
- let code_snippet = snippet ( cx, body. value . span , ".." ) ;
16
+ let code_snippet = match snippet_opt ( cx, body. value . span ) {
17
+ Some ( s) => s,
18
+ _ => return ,
19
+ } ;
17
20
let mut line_count: u64 = 0 ;
18
21
let mut in_comment = false ;
19
22
let mut code_in_line;
20
23
21
- // Skip the surrounding function decl.
22
- let start_brace_idx = code_snippet. find ( '{' ) . map_or ( 0 , |i| i + 1 ) ;
23
- let end_brace_idx = code_snippet. rfind ( '}' ) . unwrap_or_else ( || code_snippet. len ( ) ) ;
24
- let function_lines = code_snippet[ start_brace_idx..end_brace_idx] . lines ( ) ;
24
+ let function_lines = if matches ! ( body. value. kind, hir:: ExprKind :: Block ( ..) )
25
+ && code_snippet. as_bytes ( ) . first ( ) . copied ( ) == Some ( b'{' )
26
+ && code_snippet. as_bytes ( ) . last ( ) . copied ( ) == Some ( b'}' )
27
+ {
28
+ // Removing the braces from the enclosing block
29
+ & code_snippet[ 1 ..code_snippet. len ( ) - 1 ]
30
+ } else {
31
+ & code_snippet
32
+ }
33
+ . trim ( ) // Remove leading and trailing blank lines
34
+ . lines ( ) ;
25
35
26
36
for mut line in function_lines {
27
37
code_in_line = false ;
0 commit comments