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 19, 2023
1 parent a860526 commit 2cd4a91
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
1 change: 1 addition & 0 deletions book/src/lint_configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ Suppress lints whenever the suggested change would cause breakage for other crat
* [`linkedlist`](https://rust-lang.github.io/rust-clippy/master/index.html#linkedlist)
* [`rc_mutex`](https://rust-lang.github.io/rust-clippy/master/index.html#rc_mutex)
* [`unnecessary_box_returns`](https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_box_returns)
* [`single_call_fn`](https://rust-lang.github.io/rust-clippy/master/index.html#single_call_fn)


## `msrv`
Expand Down
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 2cd4a91

Please sign in to comment.