Don't emit too_many_lines for closures

This commit is contained in:
LeSeulArtichaut 2021-08-05 15:38:57 +02:00
parent 2dbf0c138d
commit 4743ec1948
4 changed files with 51 additions and 6 deletions

View file

@ -251,7 +251,7 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
hir_id: hir::HirId, hir_id: hir::HirId,
) { ) {
too_many_arguments::check_fn(cx, kind, decl, span, hir_id, self.too_many_arguments_threshold); 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); not_unsafe_ptr_arg_deref::check_fn(cx, kind, decl, body, hir_id);
} }

View file

@ -1,4 +1,5 @@
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::intravisit::FnKind;
use rustc_lint::{LateContext, LintContext}; use rustc_lint::{LateContext, LintContext};
use rustc_middle::lint::in_external_macro; use rustc_middle::lint::in_external_macro;
use rustc_span::Span; use rustc_span::Span;
@ -8,8 +9,16 @@ use clippy_utils::source::snippet_opt;
use super::TOO_MANY_LINES; use super::TOO_MANY_LINES;
pub(super) fn check_fn(cx: &LateContext<'_>, span: Span, body: &'tcx hir::Body<'_>, too_many_lines_threshold: u64) { pub(super) fn check_fn(
if in_external_macro(cx.sess(), span) { 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; return;
} }

View file

@ -1,3 +1,5 @@
// edition:2018
#![warn(clippy::too_many_lines)] #![warn(clippy::too_many_lines)]
// This function should be considered one line. // This function should be considered one line.
@ -20,6 +22,20 @@ fn too_many_lines() {
println!("This is bad."); 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. // This should be considered one line.
#[rustfmt::skip] #[rustfmt::skip]
fn comment_starts_after_code() { fn comment_starts_after_code() {

View file

@ -1,5 +1,5 @@
error: this function has too many lines (2/1) 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 | / fn too_many_lines() {
LL | | println!("This is bad."); LL | | println!("This is bad.");
@ -9,8 +9,28 @@ LL | | }
| |
= note: `-D clippy::too-many-lines` implied by `-D warnings` = 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) 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 | / fn comment_before_code() {
LL | | let _ = "test"; LL | | let _ = "test";
@ -19,5 +39,5 @@ LL | | the code but this line should still count. */ let _ = 5;
LL | | } LL | | }
| |_^ | |_^
error: aborting due to 2 previous errors error: aborting due to 4 previous errors