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

Rule Request: Unused variables in functions and classes #2120

Closed
2 tasks done
varunpm1 opened this issue Mar 29, 2018 · 17 comments
Closed
2 tasks done

Rule Request: Unused variables in functions and classes #2120

varunpm1 opened this issue Mar 29, 2018 · 17 comments
Labels
rule-request Requests for a new rules.

Comments

@varunpm1
Copy link

New Issue Checklist

Rule Request

  1. Why should this rule be added?

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 -

func testSomething(a: Int, b: Int, c: Int) -> Int { // c parameter isn't used at all
   let sum = a + b
   
    return sum
}
class Sample {
    var var1: Int = 0
    @IBOutlet weak var sampleCollectionView: UICollectionView!
    // since var1 and sampleCollectionView isn't being read or written into in entire class
}

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 :)

@SDGGiesbrecht
Copy link
Contributor

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’m not sure if this is a feasible feature but [it] would be great if it [were] present.

I agree with both thoughts. Thank you for the suggestion, @varunpm1.

@marcelofabri marcelofabri added the rule-request Requests for a new rules. label Mar 30, 2018
@marcelofabri
Copy link
Collaborator

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 private and fileprivate properties by now.

@acecilia
Copy link
Contributor

acecilia commented May 16, 2018

@marcelofabri some compilers allow to enable those warnings: for example in GCC by passing the flag -Wunused (see: https://stackoverflow.com/questions/386220/how-can-i-hide-defined-but-not-used-warnings-in-gcc). Do you think it should be a compiler feature or a Swiftlint feature?

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.

@mathaeus
Copy link

mathaeus commented Aug 7, 2018

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.

@Moximillian
Copy link

Xcode supports checking for unused function parameters when using Objective-C, but not for Swift. This would be very nice addition to SwiftLint.

@keith
Copy link
Contributor

keith commented Dec 23, 2019

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

@kirhgoff
Copy link

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.

@agustr
Copy link

agustr commented May 15, 2021

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.

@Ogerets
Copy link

Ogerets commented Aug 26, 2021

+1

3 similar comments
@NilayDagdemir
Copy link

+1

@olexth
Copy link

olexth commented Feb 10, 2022

+1

@cassianomonteiro
Copy link

+1

@SimplyDanny
Copy link
Collaborator

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.

@agustr
Copy link

agustr commented Apr 6, 2022

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!

@SimplyDanny
Copy link
Collaborator

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. 😉

@N-ihad
Copy link

N-ihad commented Jun 26, 2024

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

@SimplyDanny
Copy link
Collaborator

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 unused_parameter and available in main and hopefully in the not too far away upcoming next release.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule-request Requests for a new rules.
Projects
None yet
Development

No branches or pull requests