Skip to content

Commit 7c028de

Browse files
committed
Move too_many_lines to its own module
1 parent febf34e commit 7c028de

File tree

2 files changed

+71
-61
lines changed

2 files changed

+71
-61
lines changed

clippy_lints/src/functions/mod.rs

Lines changed: 3 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
mod too_many_arguments;
2+
mod too_many_lines;
23

34
use clippy_utils::diagnostics::{span_lint, span_lint_and_help, span_lint_and_then};
4-
use clippy_utils::source::{snippet, snippet_opt};
5+
use clippy_utils::source::snippet_opt;
56
use clippy_utils::ty::{is_must_use_ty, is_type_diagnostic_item, type_is_unsafe_function};
67
use clippy_utils::{
78
attr_by_name, attrs::is_proc_macro, iter_input_pats, match_def_path, must_use_attr, path_to_local, return_ty,
@@ -256,6 +257,7 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
256257
hir_id: hir::HirId,
257258
) {
258259
too_many_arguments::check_fn(cx, kind, decl, span, hir_id, self.too_many_arguments_threshold);
260+
too_many_lines::check(cx, span, body, self.too_many_lines_threshold);
259261

260262
let unsafety = match kind {
261263
intravisit::FnKind::ItemFn(_, _, hir::FnHeader { unsafety, .. }, _) => unsafety,
@@ -264,7 +266,6 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
264266
};
265267

266268
Self::check_raw_ptr(cx, unsafety, decl, body, hir_id);
267-
self.check_line_number(cx, span, body);
268269
}
269270

270271
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
@@ -356,65 +357,6 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
356357
}
357358

358359
impl<'tcx> Functions {
359-
fn check_line_number(self, cx: &LateContext<'_>, span: Span, body: &'tcx hir::Body<'_>) {
360-
if in_external_macro(cx.sess(), span) {
361-
return;
362-
}
363-
364-
let code_snippet = snippet(cx, body.value.span, "..");
365-
let mut line_count: u64 = 0;
366-
let mut in_comment = false;
367-
let mut code_in_line;
368-
369-
// Skip the surrounding function decl.
370-
let start_brace_idx = code_snippet.find('{').map_or(0, |i| i + 1);
371-
let end_brace_idx = code_snippet.rfind('}').unwrap_or_else(|| code_snippet.len());
372-
let function_lines = code_snippet[start_brace_idx..end_brace_idx].lines();
373-
374-
for mut line in function_lines {
375-
code_in_line = false;
376-
loop {
377-
line = line.trim_start();
378-
if line.is_empty() {
379-
break;
380-
}
381-
if in_comment {
382-
if let Some(i) = line.find("*/") {
383-
line = &line[i + 2..];
384-
in_comment = false;
385-
continue;
386-
}
387-
} else {
388-
let multi_idx = line.find("/*").unwrap_or_else(|| line.len());
389-
let single_idx = line.find("//").unwrap_or_else(|| line.len());
390-
code_in_line |= multi_idx > 0 && single_idx > 0;
391-
// Implies multi_idx is below line.len()
392-
if multi_idx < single_idx {
393-
line = &line[multi_idx + 2..];
394-
in_comment = true;
395-
continue;
396-
}
397-
}
398-
break;
399-
}
400-
if code_in_line {
401-
line_count += 1;
402-
}
403-
}
404-
405-
if line_count > self.too_many_lines_threshold {
406-
span_lint(
407-
cx,
408-
TOO_MANY_LINES,
409-
span,
410-
&format!(
411-
"this function has too many lines ({}/{})",
412-
line_count, self.too_many_lines_threshold
413-
),
414-
)
415-
}
416-
}
417-
418360
fn check_raw_ptr(
419361
cx: &LateContext<'tcx>,
420362
unsafety: hir::Unsafety,
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use rustc_hir as hir;
2+
use rustc_lint::{LateContext, LintContext};
3+
use rustc_middle::lint::in_external_macro;
4+
use rustc_span::Span;
5+
6+
use clippy_utils::diagnostics::span_lint;
7+
use clippy_utils::source::snippet;
8+
9+
use super::TOO_MANY_LINES;
10+
11+
pub(super) fn check(cx: &LateContext<'_>, span: Span, body: &'tcx hir::Body<'_>, too_many_lines_threshold: u64) {
12+
if in_external_macro(cx.sess(), span) {
13+
return;
14+
}
15+
16+
let code_snippet = snippet(cx, body.value.span, "..");
17+
let mut line_count: u64 = 0;
18+
let mut in_comment = false;
19+
let mut code_in_line;
20+
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();
25+
26+
for mut line in function_lines {
27+
code_in_line = false;
28+
loop {
29+
line = line.trim_start();
30+
if line.is_empty() {
31+
break;
32+
}
33+
if in_comment {
34+
if let Some(i) = line.find("*/") {
35+
line = &line[i + 2..];
36+
in_comment = false;
37+
continue;
38+
}
39+
} else {
40+
let multi_idx = line.find("/*").unwrap_or_else(|| line.len());
41+
let single_idx = line.find("//").unwrap_or_else(|| line.len());
42+
code_in_line |= multi_idx > 0 && single_idx > 0;
43+
// Implies multi_idx is below line.len()
44+
if multi_idx < single_idx {
45+
line = &line[multi_idx + 2..];
46+
in_comment = true;
47+
continue;
48+
}
49+
}
50+
break;
51+
}
52+
if code_in_line {
53+
line_count += 1;
54+
}
55+
}
56+
57+
if line_count > too_many_lines_threshold {
58+
span_lint(
59+
cx,
60+
TOO_MANY_LINES,
61+
span,
62+
&format!(
63+
"this function has too many lines ({}/{})",
64+
line_count, too_many_lines_threshold
65+
),
66+
)
67+
}
68+
}

0 commit comments

Comments
 (0)