Skip to content

Commit 6cb30ad

Browse files
committed
Auto merge of rust-lang#7534 - LeSeulArtichaut:7517-closure-too-many-lines, r=flip1995
Don't emit `too_many_lines` for closures changelog: don't emit the [`too_many_lines`] lint inside closures to avoir duplicated diagnostics. Fixes rust-lang#7517.
2 parents a0e71e5 + 4743ec1 commit 6cb30ad

File tree

4 files changed

+51
-6
lines changed

4 files changed

+51
-6
lines changed

clippy_lints/src/functions/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
251251
hir_id: hir::HirId,
252252
) {
253253
too_many_arguments::check_fn(cx, kind, decl, span, hir_id, self.too_many_arguments_threshold);
254-
too_many_lines::check_fn(cx, span, body, self.too_many_lines_threshold);
254+
too_many_lines::check_fn(cx, kind, span, body, self.too_many_lines_threshold);
255255
not_unsafe_ptr_arg_deref::check_fn(cx, kind, decl, body, hir_id);
256256
}
257257

clippy_lints/src/functions/too_many_lines.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use rustc_hir as hir;
2+
use rustc_hir::intravisit::FnKind;
23
use rustc_lint::{LateContext, LintContext};
34
use rustc_middle::lint::in_external_macro;
45
use rustc_span::Span;
@@ -8,8 +9,16 @@ use clippy_utils::source::snippet_opt;
89

910
use super::TOO_MANY_LINES;
1011

11-
pub(super) fn check_fn(cx: &LateContext<'_>, span: Span, body: &'tcx hir::Body<'_>, too_many_lines_threshold: u64) {
12-
if in_external_macro(cx.sess(), span) {
12+
pub(super) fn check_fn(
13+
cx: &LateContext<'_>,
14+
kind: FnKind<'tcx>,
15+
span: Span,
16+
body: &'tcx hir::Body<'_>,
17+
too_many_lines_threshold: u64,
18+
) {
19+
// Closures must be contained in a parent body, which will be checked for `too_many_lines`.
20+
// Don't check closures for `too_many_lines` to avoid duplicated lints.
21+
if matches!(kind, FnKind::Closure) || in_external_macro(cx.sess(), span) {
1322
return;
1423
}
1524

tests/ui-toml/functions_maxlines/test.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// edition:2018
2+
13
#![warn(clippy::too_many_lines)]
24

35
// This function should be considered one line.
@@ -20,6 +22,20 @@ fn too_many_lines() {
2022
println!("This is bad.");
2123
}
2224

25+
// This should only fail once (#7517).
26+
async fn async_too_many_lines() {
27+
println!("This is bad.");
28+
println!("This is bad.");
29+
}
30+
31+
// This should fail only once, without failing on the closure.
32+
fn closure_too_many_lines() {
33+
let _ = {
34+
println!("This is bad.");
35+
println!("This is bad.");
36+
};
37+
}
38+
2339
// This should be considered one line.
2440
#[rustfmt::skip]
2541
fn comment_starts_after_code() {
Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this function has too many lines (2/1)
2-
--> $DIR/test.rs:18:1
2+
--> $DIR/test.rs:20:1
33
|
44
LL | / fn too_many_lines() {
55
LL | | println!("This is bad.");
@@ -9,8 +9,28 @@ LL | | }
99
|
1010
= note: `-D clippy::too-many-lines` implied by `-D warnings`
1111

12+
error: this function has too many lines (4/1)
13+
--> $DIR/test.rs:26:1
14+
|
15+
LL | / async fn async_too_many_lines() {
16+
LL | | println!("This is bad.");
17+
LL | | println!("This is bad.");
18+
LL | | }
19+
| |_^
20+
21+
error: this function has too many lines (4/1)
22+
--> $DIR/test.rs:32:1
23+
|
24+
LL | / fn closure_too_many_lines() {
25+
LL | | let _ = {
26+
LL | | println!("This is bad.");
27+
LL | | println!("This is bad.");
28+
LL | | };
29+
LL | | }
30+
| |_^
31+
1232
error: this function has too many lines (2/1)
13-
--> $DIR/test.rs:38:1
33+
--> $DIR/test.rs:54:1
1434
|
1535
LL | / fn comment_before_code() {
1636
LL | | let _ = "test";
@@ -19,5 +39,5 @@ LL | | the code but this line should still count. */ let _ = 5;
1939
LL | | }
2040
| |_^
2141

22-
error: aborting due to 2 previous errors
42+
error: aborting due to 4 previous errors
2343

0 commit comments

Comments
 (0)