Skip to content

Commit 9ebdc2a

Browse files
committed
Ignore functions annotated with #[test]
1 parent 334c483 commit 9ebdc2a

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

clippy_lints/src/single_call_fn.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
2-
use clippy_utils::is_from_proc_macro;
2+
use clippy_utils::{is_from_proc_macro, is_in_test_function};
33
use rustc_data_structures::fx::FxHashMap;
44
use rustc_hir::def_id::LocalDefId;
55
use rustc_hir::intravisit::{walk_expr, Visitor};
@@ -12,7 +12,7 @@ use rustc_span::Span;
1212

1313
declare_clippy_lint! {
1414
/// ### What it does
15-
/// Checks for functions that are only used once.
15+
/// Checks for functions that are only used once. Does not lint tests.
1616
///
1717
/// ### Why is this bad?
1818
/// It's usually not, splitting a function into multiple parts often improves readability and in
@@ -73,6 +73,7 @@ impl<'tcx> LateLintPass<'tcx> for SingleCallFn {
7373
if self.avoid_breaking_exported_api && cx.effective_visibilities.is_exported(def_id)
7474
|| in_external_macro(cx.sess(), span)
7575
|| is_from_proc_macro(cx, &(&kind, body, cx.tcx.local_def_id_to_hir_id(def_id), span))
76+
|| is_in_test_function(cx.tcx, body.value.hir_id)
7677
{
7778
return;
7879
}

tests/ui/single_call_fn.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//@aux-build:proc_macros.rs
2+
//@compile-flags: --test
23
#![allow(clippy::redundant_closure_call, unused)]
34
#![warn(clippy::single_call_fn)]
45
#![no_main]
@@ -64,3 +65,11 @@ fn e() {
6465
b();
6566
b();
6667
}
68+
69+
#[test]
70+
fn k() {}
71+
72+
#[test]
73+
fn l() {
74+
k();
75+
}

tests/ui/single_call_fn.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this function is only used once
2-
--> $DIR/single_call_fn.rs:33:1
2+
--> $DIR/single_call_fn.rs:34:1
33
|
44
LL | / fn c() {
55
LL | | println!("really");
@@ -9,44 +9,44 @@ LL | | }
99
| |_^
1010
|
1111
help: used here
12-
--> $DIR/single_call_fn.rs:40:5
12+
--> $DIR/single_call_fn.rs:41:5
1313
|
1414
LL | c();
1515
| ^
1616
= note: `-D clippy::single-call-fn` implied by `-D warnings`
1717

1818
error: this function is only used once
19-
--> $DIR/single_call_fn.rs:12:1
19+
--> $DIR/single_call_fn.rs:13:1
2020
|
2121
LL | fn i() {}
2222
| ^^^^^^^^^
2323
|
2424
help: used here
25-
--> $DIR/single_call_fn.rs:17:13
25+
--> $DIR/single_call_fn.rs:18:13
2626
|
2727
LL | let a = i;
2828
| ^
2929

3030
error: this function is only used once
31-
--> $DIR/single_call_fn.rs:43:1
31+
--> $DIR/single_call_fn.rs:44:1
3232
|
3333
LL | fn a() {}
3434
| ^^^^^^^^^
3535
|
3636
help: used here
37-
--> $DIR/single_call_fn.rs:46:5
37+
--> $DIR/single_call_fn.rs:47:5
3838
|
3939
LL | a();
4040
| ^
4141

4242
error: this function is only used once
43-
--> $DIR/single_call_fn.rs:13:1
43+
--> $DIR/single_call_fn.rs:14:1
4444
|
4545
LL | fn j() {}
4646
| ^^^^^^^^^
4747
|
4848
help: used here
49-
--> $DIR/single_call_fn.rs:24:9
49+
--> $DIR/single_call_fn.rs:25:9
5050
|
5151
LL | j();
5252
| ^

0 commit comments

Comments
 (0)