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

Detect redundant extensions #5359

Open
inotetw opened this issue Nov 18, 2023 · 7 comments · May be fixed by #5388 or #5397
Open

Detect redundant extensions #5359

inotetw opened this issue Nov 18, 2023 · 7 comments · May be fixed by #5388 or #5397
Assignees
Labels
good first issue Issue to be taken up by new contributors yet unfamiliar with the project. rule-request Requests for a new rules.

Comments

@inotetw
Copy link

inotetw commented Nov 18, 2023

New rule request

extension without any function will be avoid

Bad

extension Dog {}
@SimplyDanny SimplyDanny added rule-request Requests for a new rules. discussion Topics that cannot be categorized as bugs or enhancements yet. They require further discussions. labels Nov 18, 2023
@SimplyDanny
Copy link
Collaborator

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

@inotetw
Copy link
Author

inotetw commented Nov 21, 2023

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

// MARK: UITableViewDelegate
extension TableVM: UITableViewDelegate {}

@SimplyDanny SimplyDanny added good first issue Issue to be taken up by new contributors yet unfamiliar with the project. and removed discussion Topics that cannot be categorized as bugs or enhancements yet. They require further discussions. labels Nov 21, 2023
@KaitoMuraoka
Copy link
Contributor

I will take on this Issue.

@mzeeshanid
Copy link
Contributor

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?

@tonell-m
Copy link
Contributor

@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 🙂

@mzeeshanid
Copy link
Contributor

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

@tonell-m
Copy link
Contributor

Oh neat. Good one! 👏

mzeeshanid added a commit to mzeeshanid/SwiftLint that referenced this issue Dec 17, 2023
mzeeshanid added a commit to mzeeshanid/SwiftLint that referenced this issue Dec 17, 2023
… into redundant_extension

* 'redundant_extension' of github.com:mzeeshanid/SwiftLint:
  - change log updated
  - change log updated
  - new example added
  - simplified logic - new case added in non triggering example
  - rule added for avoiding redundant extensions resolve realm#5359
@mzeeshanid mzeeshanid linked a pull request Dec 18, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Issue to be taken up by new contributors yet unfamiliar with the project. rule-request Requests for a new rules.
Projects
None yet
6 participants
@mzeeshanid @SimplyDanny @tonell-m @inotetw @KaitoMuraoka and others