-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Rule Request: Unused variables in functions and classes #2120
Comments
Unused function parameters should probably ignore protocol conformances and overrides if possible. I guess otherwise unused properties might also be needed to fulfill protocol conformances.
I agree with both thoughts. Thank you for the suggestion, @varunpm1. |
I think those should be two different rules. +1 for ignoring function parameters in overrides. In protocol conformances it'll be harder given how SwiftLint works. Checking unused properties would also be hard, but we could do it at least for |
@marcelofabri some compilers allow to enable those warnings: for example in GCC by passing the flag EDIT: xcode has the GCC_WARN_UNUSED_VARIABLE build configuration, but seems that it does not check for variables in global scopes, only inside functions. |
I'd also vote for a "unused method parameter rule". We accidentally introduced a bug because a method was refactored and afterwards a parameter was unused. |
Xcode supports checking for unused function parameters when using Objective-C, but not for Swift. This would be very nice addition to SwiftLint. |
If you're interested in having a native Swift warning for unused parameters you should add your input about this here https://forums.swift.org/t/swift-and-gcc-warn-unused-parameter/14607/8 |
I spent half a day today looking for a bug. The main cause of spending so much time was the debugger showing incorrect values, but the cause of the bug was an unused parameter, which was not assigned to the field hence optional was nil. |
I would love this. I am currently combing through a project I inherited from developers that are leaving or have left and I have already found a number of functions that have unused function variables. The main problem for me in these instances is that then the function name implies dependencies and logic that simply is not there. |
+1 |
3 similar comments
+1 |
+1 |
+1 |
There is the great tool periphery which does all the things mentioned in this thread so far. So I guess, SwiftLint does not have to support them, too. |
@SimplyDanny I am having a look at periphery this instant! Freaking awesome. Thank you! 30 points to Hufflepuff! |
Happy to hear that it works out for you! Since there already is this great tool available, I don't see a reason why SwiftLint should reinvent the wheel and implement the same. Hence, I'm going to close this rule request. If anybody does not agree, please speak up or re-open the issue. 😉 |
Kinda feel a need for it as a rule, since you don’t wanna bring every other tool for different things (as with this Periphery) such as this, so it would’ve been really helpful if SwiftLint could’ve just done it as well or at least as an optimal rule to let devs decide on their own |
Given the fact that SwiftLint operates on the level of syntax only (and therefore being very fast), it's hard or impossible to reason about unused declarations in general or in a reasonable amount of time. However, function/intializer/subscript parameters are (more or less) local to a scope in a single file. This fact allows for a rule detecting unused parameters even as a fast rule in SwiftLint. So I went on to implement one. 😉 It's called Other unused elements like properties are much harder to reason about. So I don't expect a rule detecting the unused ones of this kind being added any time soon and still highly recommend other specialized tools for these cases. |
New Issue Checklist
Rule Request
I would prefer one rule to be added that can check for unused parameters passed into the function or unsued properties present inside a class or it's extensions. I understand that class variable tracking would be pretty difficult if they're public since they can be modified/accessed from other classes as well. I'm not sure if this is a feasible feature but would be great if it's present.
Triggering examples -
It can be a enabled by default since there is no use of unused variables that are passed inside a function or in a class.
It could prevent some unused variables being left out during code refactoring/cleanup, that people might miss out :)
The text was updated successfully, but these errors were encountered: