Skip to content

Commit c18363c

Browse files
authored
too_many_lines: only highlight the function signature (#15461)
resolves rust-lang/rust-clippy#15430 changelog: [`too_many_lines`]: only highlight the function signature
2 parents c32c864 + 49666f6 commit c18363c

File tree

5 files changed

+138
-42
lines changed

5 files changed

+138
-42
lines changed

clippy_lints/src/functions/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
536536
) {
537537
let hir_id = cx.tcx.local_def_id_to_hir_id(def_id);
538538
too_many_arguments::check_fn(cx, kind, decl, span, hir_id, self.too_many_arguments_threshold);
539-
too_many_lines::check_fn(cx, kind, span, body, self.too_many_lines_threshold);
539+
too_many_lines::check_fn(cx, kind, body, span, def_id, self.too_many_lines_threshold);
540540
not_unsafe_ptr_arg_deref::check_fn(cx, kind, decl, body, def_id);
541541
misnamed_getters::check_fn(cx, kind, decl, body, span);
542542
impl_trait_in_params::check_fn(cx, &kind, body, hir_id);

clippy_lints/src/functions/too_many_lines.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use clippy_utils::diagnostics::span_lint;
22
use clippy_utils::source::SpanRangeExt;
33
use rustc_hir as hir;
4+
use rustc_hir::def_id::LocalDefId;
45
use rustc_hir::intravisit::FnKind;
56
use rustc_lint::{LateContext, LintContext};
67
use rustc_span::Span;
@@ -10,8 +11,9 @@ use super::TOO_MANY_LINES;
1011
pub(super) fn check_fn(
1112
cx: &LateContext<'_>,
1213
kind: FnKind<'_>,
13-
span: Span,
1414
body: &hir::Body<'_>,
15+
span: Span,
16+
def_id: LocalDefId,
1517
too_many_lines_threshold: u64,
1618
) {
1719
// Closures must be contained in a parent body, which will be checked for `too_many_lines`.
@@ -74,7 +76,7 @@ pub(super) fn check_fn(
7476
span_lint(
7577
cx,
7678
TOO_MANY_LINES,
77-
span,
79+
cx.tcx.def_span(def_id),
7880
format!("this function has too many lines ({line_count}/{too_many_lines_threshold})"),
7981
);
8082
}
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.rs

Lines changed: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![allow(clippy::unused_unit, clippy::missing_safety_doc)]
12
#![warn(clippy::too_many_lines)]
23

34
fn good_lines() {
@@ -55,7 +56,8 @@ fn good_lines() {
5556
println!("This is good.");
5657
}
5758

58-
fn bad_lines() {
59+
#[allow(unused)] // the attr shouldn't get included in the highlight
60+
pub async unsafe extern "Rust" fn bad_lines() -> () {
5961
//~^ too_many_lines
6062

6163
println!("Dont get confused by braces: {{}}");
@@ -162,4 +164,115 @@ fn bad_lines() {
162164
println!("This is bad.");
163165
}
164166

167+
struct Foo;
168+
impl Foo {
169+
#[allow(unused)] // the attr shouldn't get included in the highlight
170+
pub async unsafe extern "Rust" fn bad_lines() -> () {
171+
//~^ too_many_lines
172+
173+
println!("Dont get confused by braces: {{}}");
174+
println!("This is bad.");
175+
println!("This is bad.");
176+
println!("This is bad.");
177+
println!("This is bad.");
178+
println!("This is bad.");
179+
println!("This is bad.");
180+
println!("This is bad.");
181+
println!("This is bad.");
182+
println!("This is bad.");
183+
println!("This is bad.");
184+
println!("This is bad.");
185+
println!("This is bad.");
186+
println!("This is bad.");
187+
println!("This is bad.");
188+
println!("This is bad.");
189+
println!("This is bad.");
190+
println!("This is bad.");
191+
println!("This is bad.");
192+
println!("This is bad.");
193+
println!("This is bad.");
194+
println!("This is bad.");
195+
println!("This is bad.");
196+
println!("This is bad.");
197+
println!("This is bad.");
198+
println!("This is bad.");
199+
println!("This is bad.");
200+
println!("This is bad.");
201+
println!("This is bad.");
202+
println!("This is bad.");
203+
println!("This is bad.");
204+
println!("This is bad.");
205+
println!("This is bad.");
206+
println!("This is bad.");
207+
println!("This is bad.");
208+
println!("This is bad.");
209+
println!("This is bad.");
210+
println!("This is bad.");
211+
println!("This is bad.");
212+
println!("This is bad.");
213+
println!("This is bad.");
214+
println!("This is bad.");
215+
println!("This is bad.");
216+
println!("This is bad.");
217+
println!("This is bad.");
218+
println!("This is bad.");
219+
println!("This is bad.");
220+
println!("This is bad.");
221+
println!("This is bad.");
222+
println!("This is bad.");
223+
println!("This is bad.");
224+
println!("This is bad.");
225+
println!("This is bad.");
226+
println!("This is bad.");
227+
println!("This is bad.");
228+
println!("This is bad.");
229+
println!("This is bad.");
230+
println!("This is bad.");
231+
println!("This is bad.");
232+
println!("This is bad.");
233+
println!("This is bad.");
234+
println!("This is bad.");
235+
println!("This is bad.");
236+
println!("This is bad.");
237+
println!("This is bad.");
238+
println!("This is bad.");
239+
println!("This is bad.");
240+
println!("This is bad.");
241+
println!("This is bad.");
242+
println!("This is bad.");
243+
println!("This is bad.");
244+
println!("This is bad.");
245+
println!("This is bad.");
246+
println!("This is bad.");
247+
println!("This is bad.");
248+
println!("This is bad.");
249+
println!("This is bad.");
250+
println!("This is bad.");
251+
println!("This is bad.");
252+
println!("This is bad.");
253+
println!("This is bad.");
254+
println!("This is bad.");
255+
println!("This is bad.");
256+
println!("This is bad.");
257+
println!("This is bad.");
258+
println!("This is bad.");
259+
println!("This is bad.");
260+
println!("This is bad.");
261+
println!("This is bad.");
262+
println!("This is bad.");
263+
println!("This is bad.");
264+
println!("This is bad.");
265+
println!("This is bad.");
266+
println!("This is bad.");
267+
println!("This is bad.");
268+
println!("This is bad.");
269+
println!("This is bad.");
270+
println!("This is bad.");
271+
println!("This is bad.");
272+
println!("This is bad.");
273+
println!("This is bad.");
274+
println!("This is bad.");
275+
}
276+
}
277+
165278
fn main() {}

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)