SwiftLint icon indicating copy to clipboard operation
SwiftLint copied to clipboard

Detect redundant extensions

Open inotetw opened this issue 1 year ago • 7 comments

New rule request

extension without any function will be avoid

Bad

extension Dog {}

inotetw avatar Nov 18 '23 14:11 inotetw

I would be interested in how regular you really stumble upon empty extensions. Have you seen this often, @inotetw?

SimplyDanny avatar Nov 18 '23 15:11 SimplyDanny

I often find the code such as following, may be my team coder use xcode template to generate code

// MARK: UITableViewDelegate extension TableVM: UITableViewDelegate {}

inotetw avatar Nov 21 '23 13:11 inotetw

I will take on this Issue.

KaitoMuraoka avatar Nov 28 '23 15:11 KaitoMuraoka

Its a first attempt based on variable and function declarations. There are more cases that needs to be address as well. A part from variable and function declarations, extensions can have other declarations as well. Any suggestions?

mzeeshanid avatar Dec 11 '23 14:12 mzeeshanid

@mzeeshanid you could also check for type declarations (structs, enums, classes), and perhaps associated types and type aliases?

Also idk if subscripts count as function declaration in SwiftSyntax but if not you could check for those as well 🙂

tonell-m avatar Dec 12 '23 14:12 tonell-m

I found a 1 liner solution i.e. if memberBlock is empty then the extension is redundant. See the last commit :)

mzeeshanid avatar Dec 12 '23 14:12 mzeeshanid

Oh neat. Good one! 👏

tonell-m avatar Dec 12 '23 14:12 tonell-m