Skip to content

too_many_lines: only highlight the function signature #15461

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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 @@ -503,7 +503,7 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
) {
let hir_id = cx.tcx.local_def_id_to_hir_id(def_id);
too_many_arguments::check_fn(cx, kind, decl, span, hir_id, self.too_many_arguments_threshold);
too_many_lines::check_fn(cx, kind, span, body, self.too_many_lines_threshold);
too_many_lines::check_fn(cx, kind, body, span, def_id, self.too_many_lines_threshold);
not_unsafe_ptr_arg_deref::check_fn(cx, kind, decl, body, def_id);
misnamed_getters::check_fn(cx, kind, decl, body, span);
impl_trait_in_params::check_fn(cx, &kind, body, hir_id);
Expand Down
6 changes: 4 additions & 2 deletions clippy_lints/src/functions/too_many_lines.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use clippy_utils::diagnostics::span_lint;
use clippy_utils::source::SpanRangeExt;
use rustc_hir as hir;
use rustc_hir::def_id::LocalDefId;
use rustc_hir::intravisit::FnKind;
use rustc_lint::{LateContext, LintContext};
use rustc_span::Span;
Expand All @@ -10,8 +11,9 @@ use super::TOO_MANY_LINES;
pub(super) fn check_fn(
cx: &LateContext<'_>,
kind: FnKind<'_>,
span: Span,
body: &hir::Body<'_>,
span: Span,
def_id: LocalDefId,
too_many_lines_threshold: u64,
) {
// Closures must be contained in a parent body, which will be checked for `too_many_lines`.
Expand Down Expand Up @@ -74,7 +76,7 @@ pub(super) fn check_fn(
span_lint(
cx,
TOO_MANY_LINES,
span,
cx.tcx.def_span(def_id),
format!("this function has too many lines ({line_count}/{too_many_lines_threshold})"),
);
}
Expand Down
35 changes: 8 additions & 27 deletions tests/ui-toml/functions_maxlines/test.stderr
Original file line number Diff line number Diff line change
@@ -1,48 +1,29 @@
error: this function has too many lines (2/1)
--> tests/ui-toml/functions_maxlines/test.rs:19:1
|
LL | / fn too_many_lines() {
LL | |
LL | | println!("This is bad.");
LL | | println!("This is bad.");
LL | | }
| |_^
LL | fn too_many_lines() {
| ^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::too-many-lines` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::too_many_lines)]`

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

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

error: this function has too many lines (2/1)
--> tests/ui-toml/functions_maxlines/test.rs:56:1
|
LL | / fn comment_before_code() {
LL | |
LL | | let _ = "test";
LL | | /* This comment extends to the front of
LL | | the code but this line should still count. */ let _ = 5;
LL | | }
| |_^
LL | fn comment_before_code() {
| ^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 4 previous errors

115 changes: 114 additions & 1 deletion tests/ui/functions_maxlines.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::unused_unit, clippy::missing_safety_doc)]
#![warn(clippy::too_many_lines)]

fn good_lines() {
Expand Down Expand Up @@ -55,7 +56,8 @@ fn good_lines() {
println!("This is good.");
}

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

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

struct Foo;
impl Foo {
#[allow(unused)] // the attr shouldn't get included in the highlight
pub async unsafe extern "Rust" fn bad_lines() -> () {
//~^ too_many_lines

println!("Dont get confused by braces: {{}}");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
println!("This is bad.");
}
}

fn main() {}
22 changes: 11 additions & 11 deletions tests/ui/functions_maxlines.stderr
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
error: this function has too many lines (102/100)
--> tests/ui/functions_maxlines.rs:58:1
error: this function has too many lines (104/100)
--> tests/ui/functions_maxlines.rs:60:1
|
LL | / fn bad_lines() {
LL | |
LL | |
LL | | println!("Dont get confused by braces: {{}}");
... |
LL | | println!("This is bad.");
LL | | }
| |_^
LL | pub async unsafe extern "Rust" fn bad_lines() -> () {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::too-many-lines` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::too_many_lines)]`

error: aborting due to 1 previous error
error: this function has too many lines (104/100)
--> tests/ui/functions_maxlines.rs:170:5
|
LL | pub async unsafe extern "Rust" fn bad_lines() -> () {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors