Skip to content

Commit 25bdf8e

Browse files
committed
too_many_lines: only highlight the function signature
1 parent 833937a commit 25bdf8e

File tree

4 files changed

+33
-41
lines changed

4 files changed

+33
-41
lines changed

clippy_lints/src/functions/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
503503
) {
504504
let hir_id = cx.tcx.local_def_id_to_hir_id(def_id);
505505
too_many_arguments::check_fn(cx, kind, decl, span, hir_id, self.too_many_arguments_threshold);
506-
too_many_lines::check_fn(cx, kind, body, span, self.too_many_lines_threshold);
506+
too_many_lines::check_fn(cx, kind, body, span, hir_id, self.too_many_lines_threshold);
507507
not_unsafe_ptr_arg_deref::check_fn(cx, kind, decl, body, def_id);
508508
misnamed_getters::check_fn(cx, kind, decl, body, span);
509509
impl_trait_in_params::check_fn(cx, &kind, body, hir_id);

clippy_lints/src/functions/too_many_lines.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint;
22
use clippy_utils::source::SpanRangeExt;
3-
use rustc_hir as hir;
43
use rustc_hir::intravisit::FnKind;
4+
use rustc_hir::{self as hir, HirId};
55
use rustc_lint::{LateContext, LintContext};
66
use rustc_span::Span;
77

@@ -12,6 +12,7 @@ pub(super) fn check_fn(
1212
kind: FnKind<'_>,
1313
body: &hir::Body<'_>,
1414
span: Span,
15+
hir_id: HirId,
1516
too_many_lines_threshold: u64,
1617
) {
1718
// Closures must be contained in a parent body, which will be checked for `too_many_lines`.
@@ -70,11 +71,21 @@ pub(super) fn check_fn(
7071
line_count > too_many_lines_threshold
7172
});
7273

74+
let fn_sig = match kind {
75+
FnKind::ItemFn(..) => cx
76+
.tcx
77+
.hir_node(hir_id)
78+
.fn_sig()
79+
.expect("`hir_id` is that of a function"),
80+
FnKind::Method(_, fn_sig) => fn_sig,
81+
FnKind::Closure => unreachable!("early-returned by the guard clause"),
82+
};
83+
7384
if too_many {
7485
span_lint(
7586
cx,
7687
TOO_MANY_LINES,
77-
span,
88+
fn_sig.span,
7889
format!("this function has too many lines ({line_count}/{too_many_lines_threshold})"),
7990
);
8091
}
Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,29 @@
11
error: this function has too many lines (2/1)
22
--> tests/ui-toml/functions_maxlines/test.rs:19:1
33
|
4-
LL | / fn too_many_lines() {
5-
LL | |
6-
LL | | println!("This is bad.");
7-
LL | | println!("This is bad.");
8-
LL | | }
9-
| |_^
4+
LL | fn too_many_lines() {
5+
| ^^^^^^^^^^^^^^^^^^^
106
|
117
= note: `-D clippy::too-many-lines` implied by `-D warnings`
128
= help: to override `-D warnings` add `#[allow(clippy::too_many_lines)]`
139

1410
error: this function has too many lines (4/1)
1511
--> tests/ui-toml/functions_maxlines/test.rs:26:1
1612
|
17-
LL | / async fn async_too_many_lines() {
18-
LL | |
19-
LL | | println!("This is bad.");
20-
LL | | println!("This is bad.");
21-
LL | | }
22-
| |_^
13+
LL | async fn async_too_many_lines() {
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2315

2416
error: this function has too many lines (4/1)
2517
--> tests/ui-toml/functions_maxlines/test.rs:33:1
2618
|
27-
LL | / fn closure_too_many_lines() {
28-
LL | |
29-
LL | | let _ = {
30-
LL | | println!("This is bad.");
31-
LL | | println!("This is bad.");
32-
LL | | };
33-
LL | | }
34-
| |_^
19+
LL | fn closure_too_many_lines() {
20+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
3521

3622
error: this function has too many lines (2/1)
3723
--> tests/ui-toml/functions_maxlines/test.rs:56:1
3824
|
39-
LL | / fn comment_before_code() {
40-
LL | |
41-
LL | | let _ = "test";
42-
LL | | /* This comment extends to the front of
43-
LL | | the code but this line should still count. */ let _ = 5;
44-
LL | | }
45-
| |_^
25+
LL | fn comment_before_code() {
26+
| ^^^^^^^^^^^^^^^^^^^^^^^^
4627

4728
error: aborting due to 4 previous errors
4829

tests/ui/functions_maxlines.stderr

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
error: this function has too many lines (102/100)
2-
--> tests/ui/functions_maxlines.rs:58:1
1+
error: this function has too many lines (104/100)
2+
--> tests/ui/functions_maxlines.rs:60:1
33
|
4-
LL | / fn bad_lines() {
5-
LL | |
6-
LL | |
7-
LL | | println!("Dont get confused by braces: {{}}");
8-
... |
9-
LL | | println!("This is bad.");
10-
LL | | }
11-
| |_^
4+
LL | pub async unsafe extern "Rust" fn bad_lines() -> () {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
126
|
137
= note: `-D clippy::too-many-lines` implied by `-D warnings`
148
= help: to override `-D warnings` add `#[allow(clippy::too_many_lines)]`
159

16-
error: aborting due to 1 previous error
10+
error: this function has too many lines (104/100)
11+
--> tests/ui/functions_maxlines.rs:170:5
12+
|
13+
LL | pub async unsafe extern "Rust" fn bad_lines() -> () {
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15+
16+
error: aborting due to 2 previous errors
1717

0 commit comments

Comments
 (0)