2 use rustc_lint::{LateContext, LintContext};
3 use rustc_middle::lint::in_external_macro;
6 use clippy_utils::diagnostics::span_lint;
7 use clippy_utils::source::snippet_opt;
9 use super::TOO_MANY_LINES;
11 pub(super) fn check_fn(cx: &LateContext<'_>, span: Span, body: &'tcx hir::Body<'_>, too_many_lines_threshold: u64) {
12 if in_external_macro(cx.sess(), span) {
16 let code_snippet = match snippet_opt(cx, body.value.span) {
20 let mut line_count: u64 = 0;
21 let mut in_comment = false;
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'}')
28 // Removing the braces from the enclosing block
29 &code_snippet[1..code_snippet.len() - 1]
33 .trim() // Remove leading and trailing blank lines
36 for mut line in function_lines {
39 line = line.trim_start();
44 if let Some(i) = line.find("*/") {
45 line = &line[i + 2..];
50 let multi_idx = line.find("/*").unwrap_or_else(|| line.len());
51 let single_idx = line.find("//").unwrap_or_else(|| line.len());
52 code_in_line |= multi_idx > 0 && single_idx > 0;
53 // Implies multi_idx is below line.len()
54 if multi_idx < single_idx {
55 line = &line[multi_idx + 2..];
67 if line_count > too_many_lines_threshold {
73 "this function has too many lines ({}/{})",
74 line_count, too_many_lines_threshold