Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-2849] Add a warning for unused parameters #45443

Open
swift-ci opened this issue Oct 4, 2016 · 6 comments
Open

[SR-2849] Add a warning for unused parameters #45443

swift-ci opened this issue Oct 4, 2016 · 6 comments

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Oct 4, 2016

Previous ID SR-2849
Radar rdar://problem/31844597
Original Reporter Alex (JIRA User)
Type New Feature
Status In Progress
Resolution
Additional Detail from JIRA
Votes 8
Component/s Compiler
Labels New Feature
Assignee @theblixguy
Priority Medium

md5: a0372c6b5db167e925ad73f87d7f903e

is duplicated by:

Issue Description:

When _ GCC_WARN_UNUSED_PARAMETER_ is enabled unused parameters does not produce warning.

How to reproduce:

  1. Set Yes to Build Settings/Apple LLVM 8.0 Warnings - All languages/_Unused
    Parameters_
  2. Add methods or blocks with unused parameters e.g.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // There is usually no need to use `tableView` or `section` parameters for simple tables
    return dataSource.count 
}

or

NotificationCenter.default.addObserver(forName: .UIApplicationWillTerminate, object: nil, queue: nil) { notification in
    // Prepare for app termination
}

Result:
No warnings.

Expected Result:
Xcode should warn about unused parameters: tableView, section and notification.
Also Xcode should suggest to replace these parameters with `_` which is already possible to do:

func tableView(_: UITableView, numberOfRowsInSection _: Int) -> Int {
    return dataSource.count 
}

NotificationCenter.default.addObserver(forName: .UIApplicationWillTerminate, object: nil, queue: nil) { _ in
    // Prepare for app termination
}

This setting works as expected in Objective-C (requires __unused attribute to suppress the warning).

@belkadan
Copy link
Contributor

belkadan commented Oct 5, 2016

Swift doesn't have a warning for this at all, but it's a reasonable request (other than Swift not having any toggleable warnings at all). And yes, once it's implemented it would probably make sense for it to follow the same Xcode build setting, though that's not part of the Swift project.

@belkadan
Copy link
Contributor

belkadan commented Nov 15, 2017

Comment of mine from dup:

I think I prefer the "_ = y" idiom, since replacing the local name with _ means you can't document the parameter.

@jckarter
Copy link
Member

jckarter commented Jul 19, 2018

We could conceivably allow either idiom. Forcing you to name the argument and ignore it in the function body feels excessive to me if there's no documentation to hang off of it.

@belkadan
Copy link
Contributor

belkadan commented Jul 19, 2018

We already allow either idiom; this would just be about what the fix-it does.

@swift-ci
Copy link
Collaborator Author

swift-ci commented Aug 7, 2018

Comment by Mathäus Zingerle (JIRA)

Upvoted.

Yet I would suggest to use an own setting for this, analogously to "treat warnings as errors":

GCC_TREAT_WARNINGS_AS_ERRORS = YES
SWIFT_TREAT_WARNINGS_AS_ERRORS = YES

=>

GCC_WARN_UNUSED_PARAMETER = YES
SWIFT_WARN_UNUSED_PARAMETER = YES

@theblixguy
Copy link
Collaborator

theblixguy commented Dec 17, 2019

Here's a PR to enable this functionality (hidden behind a frontend flag): #28839

Let me know if you have any feedback!

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants