Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't emit too_many_lines for closures #7534

Merged
merged 1 commit into from
Aug 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clippy_lints/src/functions/mod.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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