Skip to content

Commit 7fbd55c

Browse files
Unknownunknown
authored andcommitted
Reworking function logic, and adding doc example.
This should fix line count logic issues that the previous code had, with assumptions it would make.
1 parent 93bf74a commit 7fbd55c

File tree

1 file changed

+48
-30
lines changed

1 file changed

+48
-30
lines changed

clippy_lints/src/functions.rs

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ declare_clippy_lint! {
4040
///
4141
/// **Known problems:** None.
4242
///
43+
/// **Example:**
44+
/// ``` rust
45+
/// fn im_too_long() {
46+
/// println!("");
47+
/// // ... 100 more LoC
48+
/// println!("");
49+
/// }
4350
/// ```
4451
declare_clippy_lint! {
4552
pub TOO_MANY_LINES,
@@ -174,46 +181,57 @@ impl<'a, 'tcx> Functions {
174181
}
175182
}
176183

177-
fn check_line_number(self, cx: &LateContext, span: Span) {
184+
fn check_line_number(self, cx: &LateContext<'_, '_>, span: Span) {
178185
let code_snippet = snippet(cx, span, "..");
179-
let mut line_count = 0;
186+
let mut line_count: u64 = 0;
180187
let mut in_comment = false;
181-
for mut line in code_snippet.lines() {
182-
if in_comment {
183-
let end_comment_loc = match line.find("*/") {
184-
Some(i) => i,
185-
None => continue
186-
};
187-
in_comment = false;
188-
line = &line[end_comment_loc..];
189-
}
190-
line = line.trim_left();
191-
if line.is_empty() || line.starts_with("//") { continue; }
192-
if line.contains("/*") {
193-
let mut count_line: bool = !line.starts_with("/*");
194-
let close_counts = line.match_indices("*/").count();
195-
let open_counts = line.match_indices("/*").count();
188+
let mut code_in_line;
196189

197-
if close_counts > 1 || open_counts > 1 {
198-
line_count += 1;
199-
} else if close_counts == 1 {
190+
// Skip the surrounding function decl.
191+
let start_brace_idx = match code_snippet.find("{") {
192+
Some(i) => i + 1,
193+
None => 0
194+
};
195+
let end_brace_idx = match code_snippet.find("}") {
196+
Some(i) => i,
197+
None => code_snippet.len()
198+
};
199+
let function_lines = code_snippet[start_brace_idx..end_brace_idx].lines();
200+
201+
for mut line in function_lines {
202+
code_in_line = false;
203+
loop {
204+
line = line.trim_start();
205+
if line.is_empty() { break; }
206+
if in_comment {
200207
match line.find("*/") {
201208
Some(i) => {
202-
line = line[i..].trim_left();
203-
if !line.is_empty() && !line.starts_with("//") {
204-
count_line = true;
205-
}
209+
line = &line[i + 2..];
210+
in_comment = false;
211+
continue;
206212
},
207-
None => continue
213+
None => break
208214
}
209215
} else {
210-
in_comment = true;
216+
let multi_idx = match line.find("/*") {
217+
Some(i) => i,
218+
None => line.len()
219+
};
220+
let single_idx = match line.find("//") {
221+
Some(i) => i,
222+
None => line.len()
223+
};
224+
code_in_line |= multi_idx > 0 && single_idx > 0;
225+
// Implies multi_idx is below line.len()
226+
if multi_idx < single_idx {
227+
line = &line[multi_idx + 2..];
228+
in_comment = true;
229+
continue;
230+
}
231+
break;
211232
}
212-
if count_line { line_count += 1; }
213-
} else {
214-
// No multipart comment, no single comment, non-empty string.
215-
line_count += 1;
216233
}
234+
if code_in_line { line_count += 1; }
217235
}
218236

219237
if line_count > self.max_lines {

0 commit comments

Comments
 (0)