@@ -4,7 +4,7 @@ use rustc_middle::lint::in_external_macro;
44use rustc_span:: Span ;
55
66use clippy_utils:: diagnostics:: span_lint;
7- use clippy_utils:: source:: snippet ;
7+ use clippy_utils:: source:: snippet_opt ;
88
99use super :: TOO_MANY_LINES ;
1010
@@ -13,15 +13,25 @@ pub(super) fn check_fn(cx: &LateContext<'_>, span: Span, body: &'tcx hir::Body<'
1313 return ;
1414 }
1515
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+ } ;
1720 let mut line_count: u64 = 0 ;
1821 let mut in_comment = false ;
1922 let mut code_in_line;
2023
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 ( ) ;
2535
2636 for mut line in function_lines {
2737 code_in_line = false ;
0 commit comments