Skip to content

Commit 77d1cc5

Browse files
committed
add test for closures
1 parent e7cfa0c commit 77d1cc5

File tree

4 files changed

+61
-20
lines changed

4 files changed

+61
-20
lines changed

clippy_lints/src/single_call_fn.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,14 @@ impl<'tcx> LateLintPass<'tcx> for SingleCallFn {
8888
cx.tcx.hir().visit_all_item_likes_in_crate(&mut v);
8989

9090
for usage in self.def_id_to_usage.values() {
91-
let fn_span = usage.0;
92-
if let [usage] = *usage.1 {
91+
let single_call_fn_span = usage.0;
92+
if let [caller_span] = *usage.1 {
9393
span_lint_and_help(
9494
cx,
9595
SINGLE_CALL_FN,
96-
fn_span,
96+
single_call_fn_span,
9797
"this function is only used once",
98-
Some(usage),
98+
Some(caller_span),
9999
"used here",
100100
);
101101
}

clippy_lints/src/utils/conf.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ define_Conf! {
289289
/// arithmetic-side-effects-allowed-unary = ["SomeType", "AnotherType"]
290290
/// ```
291291
(arithmetic_side_effects_allowed_unary: rustc_data_structures::fx::FxHashSet<String> = <_>::default()),
292-
/// Lint: ENUM_VARIANT_NAMES, LARGE_TYPES_PASSED_BY_VALUE, TRIVIALLY_COPY_PASS_BY_REF, UNNECESSARY_WRAPS, UNUSED_SELF, UPPER_CASE_ACRONYMS, WRONG_SELF_CONVENTION, BOX_COLLECTION, REDUNDANT_ALLOCATION, RC_BUFFER, VEC_BOX, OPTION_OPTION, LINKEDLIST, RC_MUTEX, UNNECESSARY_BOX_RETURNS.
292+
/// Lint: ENUM_VARIANT_NAMES, LARGE_TYPES_PASSED_BY_VALUE, TRIVIALLY_COPY_PASS_BY_REF, UNNECESSARY_WRAPS, UNUSED_SELF, UPPER_CASE_ACRONYMS, WRONG_SELF_CONVENTION, BOX_COLLECTION, REDUNDANT_ALLOCATION, RC_BUFFER, VEC_BOX, OPTION_OPTION, LINKEDLIST, RC_MUTEX, UNNECESSARY_BOX_RETURNS, SINGLE_CALL_FN.
293293
///
294294
/// Suppress lints whenever the suggested change would cause breakage for other crates.
295295
(avoid_breaking_exported_api: bool = true),

tests/ui/single_call_fn.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//@aux-build:proc_macros.rs
2-
#![allow(unused)]
2+
#![allow(clippy::redundant_closure_call, unused)]
33
#![warn(clippy::single_call_fn)]
44
#![no_main]
55

@@ -9,6 +9,23 @@ extern crate proc_macros;
99
// Do not lint since it's public
1010
pub fn f() {}
1111

12+
fn i() {}
13+
fn j() {}
14+
15+
fn h() {
16+
// Linted
17+
let a = i;
18+
// Do not lint closures
19+
let a = (|| {
20+
// Not linted
21+
a();
22+
// Imo, it's reasonable to lint this as the function is still only being used once. Just in
23+
// a closure.
24+
j();
25+
});
26+
a();
27+
}
28+
1229
fn g() {
1330
f();
1431
}

tests/ui/single_call_fn.stderr

+38-14
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,55 @@
11
error: this function is only used once
2-
--> $DIR/single_call_fn.rs:26:1
2+
--> $DIR/single_call_fn.rs:33:1
3+
|
4+
LL | / fn c() {
5+
LL | | println!("really");
6+
LL | | println!("long");
7+
LL | | println!("function...");
8+
LL | | }
9+
| |_^
10+
|
11+
help: used here
12+
--> $DIR/single_call_fn.rs:40:5
13+
|
14+
LL | c();
15+
| ^
16+
= note: `-D clippy::single-call-fn` implied by `-D warnings`
17+
18+
error: this function is only used once
19+
--> $DIR/single_call_fn.rs:12:1
20+
|
21+
LL | fn i() {}
22+
| ^^^^^^^^^
23+
|
24+
help: used here
25+
--> $DIR/single_call_fn.rs:17:13
26+
|
27+
LL | let a = i;
28+
| ^
29+
30+
error: this function is only used once
31+
--> $DIR/single_call_fn.rs:43:1
332
|
433
LL | fn a() {}
534
| ^^^^^^^^^
635
|
736
help: used here
8-
--> $DIR/single_call_fn.rs:29:5
37+
--> $DIR/single_call_fn.rs:46:5
938
|
1039
LL | a();
1140
| ^
12-
= note: `-D clippy::single-call-fn` implied by `-D warnings`
1341

1442
error: this function is only used once
15-
--> $DIR/single_call_fn.rs:16:1
43+
--> $DIR/single_call_fn.rs:13:1
1644
|
17-
LL | / fn c() {
18-
LL | | println!("really");
19-
LL | | println!("long");
20-
LL | | println!("function...");
21-
LL | | }
22-
| |_^
45+
LL | fn j() {}
46+
| ^^^^^^^^^
2347
|
2448
help: used here
25-
--> $DIR/single_call_fn.rs:23:5
49+
--> $DIR/single_call_fn.rs:24:9
2650
|
27-
LL | c();
28-
| ^
51+
LL | j();
52+
| ^
2953

30-
error: aborting due to 2 previous errors
54+
error: aborting due to 4 previous errors
3155

0 commit comments

Comments
 (0)