about summary refs log tree commit diff
diff options
context:
space:
mode:
authorYoshitomo Nakanishi <yurayura.rounin.3@gmail.com>2021-03-24 23:20:31 +0900
committerYoshitomo Nakanishi <yurayura.rounin.3@gmail.com>2021-03-27 22:49:55 +0900
commit7c028de05f7b6803c062e12ee09a41ea9007d1e1 (patch)
tree9abba2005cf9ab0b8f8871ca901e9a9c960b55c9
parentfebf34e2b4ed9b66fc3095d55ae13cef9b6b154b (diff)
downloadrust-7c028de05f7b6803c062e12ee09a41ea9007d1e1.tar.gz
rust-7c028de05f7b6803c062e12ee09a41ea9007d1e1.zip
Move too_many_lines to its own module
-rw-r--r--clippy_lints/src/functions/mod.rs64
-rw-r--r--clippy_lints/src/functions/too_many_lines.rs68
2 files changed, 71 insertions, 61 deletions
diff --git a/clippy_lints/src/functions/mod.rs b/clippy_lints/src/functions/mod.rs
index 68badb87b22..97d3623d919 100644
--- a/clippy_lints/src/functions/mod.rs
+++ b/clippy_lints/src/functions/mod.rs
@@ -1,7 +1,8 @@
 mod too_many_arguments;
+mod too_many_lines;
 
 use clippy_utils::diagnostics::{span_lint, span_lint_and_help, span_lint_and_then};
-use clippy_utils::source::{snippet, snippet_opt};
+use clippy_utils::source::snippet_opt;
 use clippy_utils::ty::{is_must_use_ty, is_type_diagnostic_item, type_is_unsafe_function};
 use clippy_utils::{
     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 {
         hir_id: hir::HirId,
     ) {
         too_many_arguments::check_fn(cx, kind, decl, span, hir_id, self.too_many_arguments_threshold);
+        too_many_lines::check(cx, span, body, self.too_many_lines_threshold);
 
         let unsafety = match kind {
             intravisit::FnKind::ItemFn(_, _, hir::FnHeader { unsafety, .. }, _) => unsafety,
@@ -264,7 +266,6 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
         };
 
         Self::check_raw_ptr(cx, unsafety, decl, body, hir_id);
-        self.check_line_number(cx, span, body);
     }
 
     fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
@@ -356,65 +357,6 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
 }
 
 impl<'tcx> Functions {
-    fn check_line_number(self, cx: &LateContext<'_>, span: Span, body: &'tcx hir::Body<'_>) {
-        if in_external_macro(cx.sess(), span) {
-            return;
-        }
-
-        let code_snippet = snippet(cx, body.value.span, "..");
-        let mut line_count: u64 = 0;
-        let mut in_comment = false;
-        let mut code_in_line;
-
-        // Skip the surrounding function decl.
-        let start_brace_idx = code_snippet.find('{').map_or(0, |i| i + 1);
-        let end_brace_idx = code_snippet.rfind('}').unwrap_or_else(|| code_snippet.len());
-        let function_lines = code_snippet[start_brace_idx..end_brace_idx].lines();
-
-        for mut line in function_lines {
-            code_in_line = false;
-            loop {
-                line = line.trim_start();
-                if line.is_empty() {
-                    break;
-                }
-                if in_comment {
-                    if let Some(i) = line.find("*/") {
-                        line = &line[i + 2..];
-                        in_comment = false;
-                        continue;
-                    }
-                } else {
-                    let multi_idx = line.find("/*").unwrap_or_else(|| line.len());
-                    let single_idx = line.find("//").unwrap_or_else(|| line.len());
-                    code_in_line |= multi_idx > 0 && single_idx > 0;
-                    // Implies multi_idx is below line.len()
-                    if multi_idx < single_idx {
-                        line = &line[multi_idx + 2..];
-                        in_comment = true;
-                        continue;
-                    }
-                }
-                break;
-            }
-            if code_in_line {
-                line_count += 1;
-            }
-        }
-
-        if line_count > self.too_many_lines_threshold {
-            span_lint(
-                cx,
-                TOO_MANY_LINES,
-                span,
-                &format!(
-                    "this function has too many lines ({}/{})",
-                    line_count, self.too_many_lines_threshold
-                ),
-            )
-        }
-    }
-
     fn check_raw_ptr(
         cx: &LateContext<'tcx>,
         unsafety: hir::Unsafety,
diff --git a/clippy_lints/src/functions/too_many_lines.rs b/clippy_lints/src/functions/too_many_lines.rs
new file mode 100644
index 00000000000..99d5028befc
--- /dev/null
+++ b/clippy_lints/src/functions/too_many_lines.rs
@@ -0,0 +1,68 @@
+use rustc_hir as hir;
+use rustc_lint::{LateContext, LintContext};
+use rustc_middle::lint::in_external_macro;
+use rustc_span::Span;
+
+use clippy_utils::diagnostics::span_lint;
+use clippy_utils::source::snippet;
+
+use super::TOO_MANY_LINES;
+
+pub(super) fn check(cx: &LateContext<'_>, span: Span, body: &'tcx hir::Body<'_>, too_many_lines_threshold: u64) {
+    if in_external_macro(cx.sess(), span) {
+        return;
+    }
+
+    let code_snippet = snippet(cx, body.value.span, "..");
+    let mut line_count: u64 = 0;
+    let mut in_comment = false;
+    let mut code_in_line;
+
+    // Skip the surrounding function decl.
+    let start_brace_idx = code_snippet.find('{').map_or(0, |i| i + 1);
+    let end_brace_idx = code_snippet.rfind('}').unwrap_or_else(|| code_snippet.len());
+    let function_lines = code_snippet[start_brace_idx..end_brace_idx].lines();
+
+    for mut line in function_lines {
+        code_in_line = false;
+        loop {
+            line = line.trim_start();
+            if line.is_empty() {
+                break;
+            }
+            if in_comment {
+                if let Some(i) = line.find("*/") {
+                    line = &line[i + 2..];
+                    in_comment = false;
+                    continue;
+                }
+            } else {
+                let multi_idx = line.find("/*").unwrap_or_else(|| line.len());
+                let single_idx = line.find("//").unwrap_or_else(|| line.len());
+                code_in_line |= multi_idx > 0 && single_idx > 0;
+                // Implies multi_idx is below line.len()
+                if multi_idx < single_idx {
+                    line = &line[multi_idx + 2..];
+                    in_comment = true;
+                    continue;
+                }
+            }
+            break;
+        }
+        if code_in_line {
+            line_count += 1;
+        }
+    }
+
+    if line_count > too_many_lines_threshold {
+        span_lint(
+            cx,
+            TOO_MANY_LINES,
+            span,
+            &format!(
+                "this function has too many lines ({}/{})",
+                line_count, too_many_lines_threshold
+            ),
+        )
+    }
+}