# Add warnings for unused function arguments by default

**URL:** <https://forums.swift.org/t/add-warnings-for-unused-function-arguments-by-default/47271>\
**Category:** Discussion\
**Created:** [April 9, 2021, 10:11am UTC](https://forums.swift.org/t/add-warnings-for-unused-function-arguments-by-default/47271 "2021-04-09T10:11:50Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![hassila](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hassila/32/22647_2.png) [@hassila](https://forums.swift.org/u/hassila)\
**Post date:** [April 9, 2021, 10:11am UTC](https://forums.swift.org/t/add-warnings-for-unused-function-arguments-by-default/47271/1 "2021-04-09T10:11:50Z")

</div>

I think it would make sense to add a new warning to the Swift language for unused function arguments.

This is analogous to

> <https://github.com/apple/swift-evolution/blob/master/proposals/0047-nonvoid-warn.md>

And created this (trivial, but an example) bug:  
[https://github.com/apple/swift-nio/pull/1797#issuecomment-816520124](https://github.com/apple/swift-nio/pull/1797#issuecomment-816520124)

If people think it makes sense I could write up a formal pitch.

I am thinking along the lines of adding the warning by default and then have some decoration (@unused?) for function arguments which are unused.

This can be on purposes in e.g. subclasses or protocol implementers that don't need to consume all arguments, but I would argue a majority of functions do want to consume all arguments and it would make sense to warn if you aren't doing it.

Code that specifically don't need an argument can just decorate it accordingly.

This would eliminate one class of bugs reasonably cheaply.

---

<div class="post-metadata">

**Author:** ![cukr](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/cukr/32/889_2.png) [@cukr](https://forums.swift.org/u/cukr)\
**Post date:** [April 9, 2021, 10:24am UTC](https://forums.swift.org/t/add-warnings-for-unused-function-arguments-by-default/47271/2 "2021-04-09T10:24:45Z")

</div>

Warning like that would be great!  
I think the better solution for silencing the warning would be to use underscore, because it's already used for discarding values in swift

```swift
func test(a: Int) {} // warning
func test(a b: Int) {} // warning

func test(_: Int) {} // no warning
func test(a _: Int) {} // no warning

```

Similar solution is used by rust today

```swift
fn test1(arg: i32) {} // warning
fn test2(_: i32) {} // no warning
fn test3(_arg: i32) {} // no warning

```

---

<div class="post-metadata">

**Author:** ![hassila](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hassila/32/22647_2.png) [@hassila](https://forums.swift.org/u/hassila)\
**Post date:** [April 9, 2021, 10:32am UTC](https://forums.swift.org/t/add-warnings-for-unused-function-arguments-by-default/47271/3 "2021-04-09T10:32:12Z")

</div>

It could be a bit confusing as its already used for omitting function label names?

> Omitting Argument Labels
> 
> If you don’t want an argument label for a parameter, write an underscore ( `_` ) instead of an explicit argument label for that parameter.  
> (from [The Swift Programming Language: Redirect](https://docs.swift.org/swift-book/LanguageGuide/Functions.html))

If using e.g. @unused it could look like:

```swift
func test(a: Int) {} // warning normal
func test(a b: Int) {} // warning using argument label
func test(_ a: Int) {} // warning suppressed name
// And same three without warnings would be:
func test(@unused a: Int) {} 
func test(@unused a b: Int) {}
func test(@unused _ a: Int) {}

```

But the first question is whether the community thinks its a good idea, can hash out details later in that case.

---

<div class="post-metadata">

**Author:** ![ensan-hcl](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ensan-hcl/32/15186_2.png) [@ensan-hcl](https://forums.swift.org/u/ensan-hcl)\
**Post date:** [April 9, 2021, 12:20pm UTC](https://forums.swift.org/t/add-warnings-for-unused-function-arguments-by-default/47271/4 "2021-04-09T12:20:47Z")

</div>

I like this idea!

I, too, sometimes face cases where arguments that I thought using are not used in the function, and do not behave as expected like the example. This addition would reduce such bugs.

---

<div class="post-metadata">

**Author:** ![Pampel](https://avatars.discourse-cdn.com/v4/letter/p/3da27b/32.png) [@Pampel](https://forums.swift.org/u/Pampel)\
**Post date:** [April 9, 2021, 12:44pm UTC](https://forums.swift.org/t/add-warnings-for-unused-function-arguments-by-default/47271/5 "2021-04-09T12:44:50Z")

</div>

I'd be strongly +1 on this, but only if this warning does not show when the function is part of protocol conformance. Adding even more decorations/attributes to the language to call out an argument as genuinely not needed seems like adding more noise for very little gain, especially given that unused arguments are not unsafe in themselves.

---

<div class="post-metadata">

**Author:** ![hassila](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hassila/32/22647_2.png) [@hassila](https://forums.swift.org/u/hassila)\
**Post date:** [April 9, 2021, 12:49pm UTC](https://forums.swift.org/t/add-warnings-for-unused-function-arguments-by-default/47271/6 "2021-04-09T12:49:27Z")

</div>

Yeah, agree with that. I can see not wanting the behavior for protocol conformances, that would seem a reasonable exception. Then it'd only be subclasses overriding a function that might need decoration, for everyone else it'd likely be an actual issue.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [April 9, 2021, 1:53pm UTC](https://forums.swift.org/t/add-warnings-for-unused-function-arguments-by-default/47271/7 "2021-04-09T13:53:28Z")

</div>

> [@hassila](#):
>
> If people think it makes sense I could write up a formal pitch.
> 
> I am thinking along the lines of adding the warning by default and then have some decoration (@unused?) for function arguments which are unused.

Improving diagnostics does not require a pitch, but adding some decoration does.

There is no need for a decoration, because we already have a way of spelling an ignored argument, and it's already used for this purpose, so any new diagnostics must account for this and not warn:

In `func f(a: Int, b _: Int)`, `b` is intentionally ignored, since there is no way to refer to it. [Edit: I see @cukr has already pointed this out.]

Moreover, it's possible to silence a warning for any named argument by writing, for example, `_ = a` in the body of the function.

> [@hassila](#):
>
> It could be a bit confusing as its already used for omitting function label names?

Argument labels are distinct from internal parameter names. @cukr is referring to the latter.

> [@hassila](#):
>
> Yeah, agree with that. I can see not wanting the behavior for protocol conformances, that would seem a reasonable exception. Then it'd only be subclasses overriding a function that might need decoration, for everyone else it'd likely be an actual issue.

No decoration is required either for protocol conformance or subclassing: it is permitted to name your internal parameter name anything you want in either circumstance, and best practice even now should be that unused arguments have their parameters internally named `_`.

---

<div class="post-metadata">

**Author:** ![hassila](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hassila/32/22647_2.png) [@hassila](https://forums.swift.org/u/hassila)\
**Post date:** [April 9, 2021, 1:57pm UTC](https://forums.swift.org/t/add-warnings-for-unused-function-arguments-by-default/47271/8 "2021-04-09T13:57:29Z")

</div>

Thanks @xwu for explaining - then it feels this could be a clean diagnostic improvement. Not sure what the proper way to try to get that added would be though.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [April 9, 2021, 2:00pm UTC](https://forums.swift.org/t/add-warnings-for-unused-function-arguments-by-default/47271/9 "2021-04-09T14:00:18Z")

</div>

You can follow the instructions in the main Swift repository for getting started with making edits to the compiler, and then create your first pull request when you're ready! Seems it should be straightforward to build on what's already there for unused variable checking, but I haven't looked into it myself :)

---

<div class="post-metadata">

**Author:** ![Jumhyn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jumhyn/32/3891_2.png) [@Jumhyn](https://forums.swift.org/u/Jumhyn)\
**Post date:** [April 9, 2021, 2:05pm UTC](https://forums.swift.org/t/add-warnings-for-unused-function-arguments-by-default/47271/10 "2021-04-09T14:05:30Z")

</div>

I agree that the `func f(a _: Int)` is the natural spelling to silence such a warning, but IMO such a dramatic shift in diagnostic behavior probably deserves to go though evolution review. I suspect that there is _lots_ of code that would suddenly hit this warning, which would represent a non-trivial disruption to existing code bases. We should be confident that the tradeoff is justified!

Of course, that's just my opinion—the Core Team is the final arbiter of what can be considered a bug fix vs. what needs to go through evolution review.

---

<div class="post-metadata">

**Author:** ![suyashsrijan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/suyashsrijan/32/5292_2.png) [@suyashsrijan](https://forums.swift.org/u/suyashsrijan)\
**Post date:** [April 9, 2021, 2:22pm UTC](https://forums.swift.org/t/add-warnings-for-unused-function-arguments-by-default/47271/11 "2021-04-09T14:22:33Z")

</div>

I put up an implementation for this a long time ago - [[Diagnostics] Emit a diagnostic if a function parameter is unused in the body by theblixguy · Pull Request #28839 · apple/swift · GitHub](https://github.com/apple/swift/pull/28839) (although it was behind a flag and not enabled by default). I agree with what @Jumhyn says, I think this change would benefit from going through evolution.

---

<div class="post-metadata">

**Author:** ![hassila](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hassila/32/22647_2.png) [@hassila](https://forums.swift.org/u/hassila)\
**Post date:** [April 9, 2021, 2:29pm UTC](https://forums.swift.org/t/add-warnings-for-unused-function-arguments-by-default/47271/12 "2021-04-09T14:29:19Z")

</div>

@suyashsrijan that's excellent, thank you. Do you want me to write a pitch for it and try to get it through evolution or do you want to yourself?

---

<div class="post-metadata">

**Author:** ![suyashsrijan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/suyashsrijan/32/5292_2.png) [@suyashsrijan](https://forums.swift.org/u/suyashsrijan)\
**Post date:** [April 9, 2021, 2:32pm UTC](https://forums.swift.org/t/add-warnings-for-unused-function-arguments-by-default/47271/13 "2021-04-09T14:32:33Z")

</div>

Free free to pitch it! The implementation is already there (bit old so probably need to fix conflicts, tidy it up and perhaps adjust how it works depending on what rules are being proposed) so if the pitch gathers support and momentum you can probably leverage that existing implementation (I would be happy to help if needed).

---

<div class="post-metadata">

**Author:** ![Braden\_Scothern](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/braden_scothern/32/1580_2.png) [@Braden\_Scothern](https://forums.swift.org/u/Braden_Scothern)\
**Post date:** [April 9, 2021, 4:18pm UTC](https://forums.swift.org/t/add-warnings-for-unused-function-arguments-by-default/47271/14 "2021-04-09T16:18:57Z")

</div>

Here is a concern I have. It is that when you add documentation to a function you document the parameter name not the label and you can’t document \_ at the moment. I have unused variables like this when I am working with generics and I want a type not an instance. I personally prefer referring to the generic type rather than the parameter so all the type references are spelled the same.

Other than that it would certainly need to not want it for protocol conformance.

(Fixed typos thanks to autocorrect)

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [April 9, 2021, 4:27pm UTC](https://forums.swift.org/t/add-warnings-for-unused-function-arguments-by-default/47271/15 "2021-04-09T16:27:08Z")

</div>

> [@Braden\_Scothern](#):
>
> Other than that it would certainly need to not earn for protocol conformance.

For documentation purposes or other reasons?

---

<div class="post-metadata">

**Author:** ![Braden\_Scothern](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/braden_scothern/32/1580_2.png) [@Braden\_Scothern](https://forums.swift.org/u/Braden_Scothern)\
**Post date:** [April 9, 2021, 4:34pm UTC](https://forums.swift.org/t/add-warnings-for-unused-function-arguments-by-default/47271/16 "2021-04-09T16:34:45Z")

</div>

For other reasons. Sometimes it makes sense to have an argument on a protocol function but your implementation might not need it.

---

<div class="post-metadata">

**Author:** ![Jumhyn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jumhyn/32/3891_2.png) [@Jumhyn](https://forums.swift.org/u/Jumhyn)\
**Post date:** [April 9, 2021, 4:42pm UTC](https://forums.swift.org/t/add-warnings-for-unused-function-arguments-by-default/47271/17 "2021-04-09T16:42:49Z")

</div>

> [@Braden\_Scothern](#):
>
> For other reasons. Sometimes it makes sense to have an argument on a protocol function but your implementation might not need it.

Are there situations that aren't covered by giving the argument an _external_ label, while discarding the _internal_ name? E.g.,

```swift
protocol Foo {
  func someRequirement(importantArg: Int)
}

struct S: Foo {
  func someRequirement(importantArg _: Int) {
    // This space intentionally left blank -Jumhyn
  }
}

```

---

<div class="post-metadata">

**Author:** ![Braden\_Scothern](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/braden_scothern/32/1580_2.png) [@Braden\_Scothern](https://forums.swift.org/u/Braden_Scothern)\
**Post date:** [April 9, 2021, 5:16pm UTC](https://forums.swift.org/t/add-warnings-for-unused-function-arguments-by-default/47271/18 "2021-04-09T17:16:04Z")

</div>

The only thing I see is if you customize the documentation rather than inheriting it from the protocols definition. Then you cans document the \_. But that is the only issue I see.

Also overall I like the idea of the pitch I just want to make sure we get it right if it is pushed forward!

---

<div class="post-metadata">

**Author:** ![typesanitizer](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/typesanitizer/32/8661_2.png) [@typesanitizer](https://forums.swift.org/u/typesanitizer)\
**Post date:** [April 9, 2021, 5:24pm UTC](https://forums.swift.org/t/add-warnings-for-unused-function-arguments-by-default/47271/19 "2021-04-09T17:24:43Z")

</div>

I'm really surprised we didn't add this warning. 😕 When I was working on a Swift side-project a few weeks back, I hit two places where this would've helped me within the span of 1 week:

1. I updated a protocol method to take more arguments, but forgot to use all the arguments in one of the implementations.
2. I had a hard-coded value in a method. → I added an accidentally unused parameter with a default value matching the hard-coded value, and forgot to delete the hard-coded value. → I forgot to test the non-default case. → Hit the bug a few days later while investigating another bug.

Spelling-wise, if we used a spelling `_parameterName` instead of `_`, that gives the reader a clue as to what the thing is supposed to be. This is what Rust does. (`_` is also permitted though.)

---

<div class="post-metadata">

**Author:** ![Jon\_Shier](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_shier/32/921_2.png) [@Jon\_Shier](https://forums.swift.org/u/Jon_Shier)\
**Post date:** [April 9, 2021, 5:26pm UTC](https://forums.swift.org/t/add-warnings-for-unused-function-arguments-by-default/47271/20 "2021-04-09T17:26:14Z")

</div>

Any warning that merely requires rote removal of autocomplete code to silence just seems like overhead. This warning is awful in Obj-C, it would be awful in Swift (by default). I do think it's a good candidate for Swift's first optional warning though.

[Next page](https://forums.swift.org/t/add-warnings-for-unused-function-arguments-by-default/47271.md?page=2)
