Detect unused extensions

I use a lot of extension in my code to make various APIs nicer to use. Are there any tools that can detect unused extensions and extension methods/properties?

Say I have an extension extension NSView { func foo() {} } and I stop using it in code, I would like a warning about it so I can remove the extension too.

1 Like

This has been a feature request for Xcode for 10 years now, but feel free to report it again to As for Swift's open tooling, it seems possible we'll see one of the SourceKit libraries be able to do this eventually, but so far the compiler team seems reluctant to work on or integrate any sort of linting feature. But yes, unused functions, imports, extensions, they should all be visible.


This is really something that has to be in Xcode, and tools like it. The notion of a project only exists at this level, and you can't examine only the current module, except for non-public types, and non-public extensions can be omitted by the compiler without you having to delete the code.

Sure, except the compiler knows all of that information, and there are optimization passes at some level that get rid of unused code. All that's needed would be exposing the results of those passes. Of course, I have no idea what the complexity or other issues would be with that, and it would likely be simpler at a higher level. But I have no real hope of Apple actually building such a tool after a decade of Objective-C not having it.

Objective-C is entirely different, because you can invoke any message using a string assembled at runtime. The compiler and linker's hands are tied much more tightly when it comes to dead-code removal.

With Swift, the linker could remove such dead code, and probably already does some of that. What the OP wants, though, is a tool to help identify the code itself, to be removed from the source file. That's not something the compiler is going to do, because it has limited visibility. The linker does have that visibility, but this sounds like an abuse of the linker.

I still think the best place for such a tool is within the project managers, such as Xcode, AppCode or even SPM.

1 Like

I do this in Xcode with the Assistant editor set to show “Callers”. Just put the cursor in a function and it shows you everything, if anything, that calls it.

It’s not automated discovery, but it works well enough when doing this sort of hide and seek.


I do that, too, when I am in clean-up mode. It's still rather a manual process for something so easily automated for Swift code. I still really miss Eclipse's function for the same thing, which gives the callers as a list of trees that you can easily navigate.

Have you tried using SwiftLint? It's a pretty robust project, and it runs both on Darwin and Linux. It hooks up to SourceKit to parse and analyze your code.

IIRC, there's a format in which you can express a Linting Rule for SwiftLint to include in its dictionaries of rules. If so, you could probably make a custom rule for your needs :slight_smile:

I've used to great success. It's developed by an independent developer, I think, and maybe a little rough around the edges, but frequently updated and well worth the money. It has helped me find a bunch of unused code.

1 Like