Skip to content

Commit

Permalink
Don't emit too_many_lines for closures
Browse files Browse the repository at this point in the history
  • Loading branch information
LeSeulArtichaut committed Aug 5, 2021
1 parent 2dbf0c1 commit 4743ec1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
2 changes: 1 addition & 1 deletion clippy_lints/src/functions/mod.rs
Expand Up @@ -251,7 +251,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_fn(cx, span, body, self.too_many_lines_threshold);
too_many_lines::check_fn(cx, kind, span, body, self.too_many_lines_threshold);
not_unsafe_ptr_arg_deref::check_fn(cx, kind, decl, body, hir_id);
}

Expand Down
13 changes: 11 additions & 2 deletions clippy_lints/src/functions/too_many_lines.rs
@@ -1,4 +1,5 @@
use rustc_hir as hir;
use rustc_hir::intravisit::FnKind;
use rustc_lint::{LateContext, LintContext};
use rustc_middle::lint::in_external_macro;
use rustc_span::Span;
Expand All @@ -8,8 +9,16 @@ use clippy_utils::source::snippet_opt;

use super::TOO_MANY_LINES;

pub(super) fn check_fn(cx: &LateContext<'_>, span: Span, body: &'tcx hir::Body<'_>, too_many_lines_threshold: u64) {
if in_external_macro(cx.sess(), span) {
pub(super) fn check_fn(
cx: &LateContext<'_>,
kind: FnKind<'tcx>,
span: Span,
body: &'tcx hir::Body<'_>,
too_many_lines_threshold: u64,
) {
// Closures must be contained in a parent body, which will be checked for `too_many_lines`.
// Don't check closures for `too_many_lines` to avoid duplicated lints.
if matches!(kind, FnKind::Closure) || in_external_macro(cx.sess(), span) {
return;
}

Expand Down
16 changes: 16 additions & 0 deletions tests/ui-toml/functions_maxlines/test.rs
@@ -1,3 +1,5 @@
// edition:2018

#![warn(clippy::too_many_lines)]

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

// This should only fail once (#7517).
async fn async_too_many_lines() {
println!("This is bad.");
println!("This is bad.");
}

// This should fail only once, without failing on the closure.
fn closure_too_many_lines() {
let _ = {
println!("This is bad.");
println!("This is bad.");
};
}

// This should be considered one line.
#[rustfmt::skip]
fn comment_starts_after_code() {
Expand Down
26 changes: 23 additions & 3 deletions tests/ui-toml/functions_maxlines/test.stderr
@@ -1,5 +1,5 @@
error: this function has too many lines (2/1)
--> $DIR/test.rs:18:1
--> $DIR/test.rs:20:1
|
LL | / fn too_many_lines() {
LL | | println!("This is bad.");
Expand All @@ -9,8 +9,28 @@ LL | | }
|
= note: `-D clippy::too-many-lines` implied by `-D warnings`

error: this function has too many lines (4/1)
--> $DIR/test.rs:26:1
|
LL | / async fn async_too_many_lines() {
LL | | println!("This is bad.");
LL | | println!("This is bad.");
LL | | }
| |_^

error: this function has too many lines (4/1)
--> $DIR/test.rs:32:1
|
LL | / fn closure_too_many_lines() {
LL | | let _ = {
LL | | println!("This is bad.");
LL | | println!("This is bad.");
LL | | };
LL | | }
| |_^

error: this function has too many lines (2/1)
--> $DIR/test.rs:38:1
--> $DIR/test.rs:54:1
|
LL | / fn comment_before_code() {
LL | | let _ = "test";
Expand All @@ -19,5 +39,5 @@ LL | | the code but this line should still count. */ let _ = 5;
LL | | }
| |_^

error: aborting due to 2 previous errors
error: aborting due to 4 previous errors

0 comments on commit 4743ec1

Please sign in to comment.