about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCentri3 <114838443+Centri3@users.noreply.github.com>2023-06-14 07:16:56 -0500
committerCentri3 <114838443+Centri3@users.noreply.github.com>2023-06-17 20:01:15 -0500
commita8605269bde2c82b5359997fc8cfdfef6a346ccf (patch)
tree2ec792b20f531f4f433aebfefaf2a9dfa53d0160
parent07dbcbda12cacb924c2d95719ad598ee9de8738d (diff)
downloadrust-a8605269bde2c82b5359997fc8cfdfef6a346ccf.tar.gz
rust-a8605269bde2c82b5359997fc8cfdfef6a346ccf.zip
add test for closures
-rw-r--r--clippy_lints/src/single_call_fn.rs8
-rw-r--r--clippy_lints/src/utils/conf.rs2
-rw-r--r--tests/ui/single_call_fn.rs19
-rw-r--r--tests/ui/single_call_fn.stderr52
4 files changed, 61 insertions, 20 deletions
diff --git a/clippy_lints/src/single_call_fn.rs b/clippy_lints/src/single_call_fn.rs
index d0fbedee832..55a19b813b4 100644
--- a/clippy_lints/src/single_call_fn.rs
+++ b/clippy_lints/src/single_call_fn.rs
@@ -88,14 +88,14 @@ impl<'tcx> LateLintPass<'tcx> for SingleCallFn {
         cx.tcx.hir().visit_all_item_likes_in_crate(&mut v);
 
         for usage in self.def_id_to_usage.values() {
-            let fn_span = usage.0;
-            if let [usage] = *usage.1 {
+            let single_call_fn_span = usage.0;
+            if let [caller_span] = *usage.1 {
                 span_lint_and_help(
                     cx,
                     SINGLE_CALL_FN,
-                    fn_span,
+                    single_call_fn_span,
                     "this function is only used once",
-                    Some(usage),
+                    Some(caller_span),
                     "used here",
                 );
             }
diff --git a/clippy_lints/src/utils/conf.rs b/clippy_lints/src/utils/conf.rs
index e8c8d478ffc..987e2abec50 100644
--- a/clippy_lints/src/utils/conf.rs
+++ b/clippy_lints/src/utils/conf.rs
@@ -289,7 +289,7 @@ define_Conf! {
     /// arithmetic-side-effects-allowed-unary = ["SomeType", "AnotherType"]
     /// ```
     (arithmetic_side_effects_allowed_unary: rustc_data_structures::fx::FxHashSet<String> = <_>::default()),
-    /// 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.
+    /// 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.
     ///
     /// Suppress lints whenever the suggested change would cause breakage for other crates.
     (avoid_breaking_exported_api: bool = true),
diff --git a/tests/ui/single_call_fn.rs b/tests/ui/single_call_fn.rs
index 9806eaf3c42..e7f819cc8a1 100644
--- a/tests/ui/single_call_fn.rs
+++ b/tests/ui/single_call_fn.rs
@@ -1,5 +1,5 @@
 //@aux-build:proc_macros.rs
-#![allow(unused)]
+#![allow(clippy::redundant_closure_call, unused)]
 #![warn(clippy::single_call_fn)]
 #![no_main]
 
@@ -9,6 +9,23 @@ extern crate proc_macros;
 // Do not lint since it's public
 pub fn f() {}
 
+fn i() {}
+fn j() {}
+
+fn h() {
+    // Linted
+    let a = i;
+    // Do not lint closures
+    let a = (|| {
+        // Not linted
+        a();
+        // Imo, it's reasonable to lint this as the function is still only being used once. Just in
+        // a closure.
+        j();
+    });
+    a();
+}
+
 fn g() {
     f();
 }
diff --git a/tests/ui/single_call_fn.stderr b/tests/ui/single_call_fn.stderr
index decf561eed4..9ef8c487844 100644
--- a/tests/ui/single_call_fn.stderr
+++ b/tests/ui/single_call_fn.stderr
@@ -1,31 +1,55 @@
 error: this function is only used once
-  --> $DIR/single_call_fn.rs:26:1
+  --> $DIR/single_call_fn.rs:33:1
+   |
+LL | / fn c() {
+LL | |     println!("really");
+LL | |     println!("long");
+LL | |     println!("function...");
+LL | | }
+   | |_^
+   |
+help: used here
+  --> $DIR/single_call_fn.rs:40: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
+   |
+LL | fn i() {}
+   | ^^^^^^^^^
+   |
+help: used here
+  --> $DIR/single_call_fn.rs:17:13
+   |
+LL |     let a = i;
+   |             ^
+
+error: this function is only used once
+  --> $DIR/single_call_fn.rs:43:1
    |
 LL | fn a() {}
    | ^^^^^^^^^
    |
 help: used here
-  --> $DIR/single_call_fn.rs:29:5
+  --> $DIR/single_call_fn.rs:46:5
    |
 LL |     a();
    |     ^
-   = note: `-D clippy::single-call-fn` implied by `-D warnings`
 
 error: this function is only used once
-  --> $DIR/single_call_fn.rs:16:1
+  --> $DIR/single_call_fn.rs:13:1
    |
-LL | / fn c() {
-LL | |     println!("really");
-LL | |     println!("long");
-LL | |     println!("function...");
-LL | | }
-   | |_^
+LL | fn j() {}
+   | ^^^^^^^^^
    |
 help: used here
-  --> $DIR/single_call_fn.rs:23:5
+  --> $DIR/single_call_fn.rs:24:9
    |
-LL |     c();
-   |     ^
+LL |         j();
+   |         ^
 
-error: aborting due to 2 previous errors
+error: aborting due to 4 previous errors