Skip to content

Commit

Permalink
Ignore functions annotated with #[test]
Browse files Browse the repository at this point in the history
  • Loading branch information
Centri3 committed Jun 18, 2023
1 parent a860526 commit e5f3be8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
5 changes: 3 additions & 2 deletions clippy_lints/src/single_call_fn.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::is_from_proc_macro;
use clippy_utils::{is_from_proc_macro, is_in_test_function};
use rustc_data_structures::fx::FxHashMap;
use rustc_hir::def_id::LocalDefId;
use rustc_hir::intravisit::{walk_expr, Visitor};
Expand All @@ -12,7 +12,7 @@ use rustc_span::Span;

declare_clippy_lint! {
/// ### What it does
/// Checks for functions that are only used once.
/// Checks for functions that are only used once. Does not lint tests.
///
/// ### Why is this bad?
/// It's usually not, splitting a function into multiple parts often improves readability and in
Expand Down Expand Up @@ -73,6 +73,7 @@ impl<'tcx> LateLintPass<'tcx> for SingleCallFn {
if self.avoid_breaking_exported_api && cx.effective_visibilities.is_exported(def_id)
|| in_external_macro(cx.sess(), span)
|| is_from_proc_macro(cx, &(&kind, body, cx.tcx.local_def_id_to_hir_id(def_id), span))
|| is_in_test_function(cx.tcx, body.value.hir_id)
{
return;
}
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/single_call_fn.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//@aux-build:proc_macros.rs
//@compile-flags: --test
#![allow(clippy::redundant_closure_call, unused)]
#![warn(clippy::single_call_fn)]
#![no_main]
Expand Down Expand Up @@ -64,3 +65,11 @@ fn e() {
b();
b();
}

#[test]
fn k() {}

#[test]
fn l() {
k();
}
16 changes: 8 additions & 8 deletions tests/ui/single_call_fn.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: this function is only used once
--> $DIR/single_call_fn.rs:33:1
--> $DIR/single_call_fn.rs:34:1
|
LL | / fn c() {
LL | | println!("really");
Expand All @@ -9,44 +9,44 @@ LL | | }
| |_^
|
help: used here
--> $DIR/single_call_fn.rs:40:5
--> $DIR/single_call_fn.rs:41:5
|
LL | c();
| ^
= note: `-D clippy::single-call-fn` implied by `-D warnings`

error: this function is only used once
--> $DIR/single_call_fn.rs:12:1
--> $DIR/single_call_fn.rs:13:1
|
LL | fn i() {}
| ^^^^^^^^^
|
help: used here
--> $DIR/single_call_fn.rs:17:13
--> $DIR/single_call_fn.rs:18:13
|
LL | let a = i;
| ^

error: this function is only used once
--> $DIR/single_call_fn.rs:43:1
--> $DIR/single_call_fn.rs:44:1
|
LL | fn a() {}
| ^^^^^^^^^
|
help: used here
--> $DIR/single_call_fn.rs:46:5
--> $DIR/single_call_fn.rs:47:5
|
LL | a();
| ^

error: this function is only used once
--> $DIR/single_call_fn.rs:13:1
--> $DIR/single_call_fn.rs:14:1
|
LL | fn j() {}
| ^^^^^^^^^
|
help: used here
--> $DIR/single_call_fn.rs:24:9
--> $DIR/single_call_fn.rs:25:9
|
LL | j();
| ^
Expand Down

0 comments on commit e5f3be8

Please sign in to comment.