Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

syntactic_sugar flags var declarations that can't be fixed #928

Closed
ZevEisenberg opened this issue Dec 5, 2016 · 4 comments
Closed

syntactic_sugar flags var declarations that can't be fixed #928

ZevEisenberg opened this issue Dec 5, 2016 · 4 comments
Labels
enhancement Ideas for improvements of existing features and rules.

Comments

@ZevEisenberg
Copy link
Contributor

ZevEisenberg commented Dec 5, 2016

I have this line:

fileprivate var currentIndex: Array<OnboardingPage>.Index?

because I want a variable of the same type as the index of an array of OnboardingPages. However, if I change it to [OnboardingPage].Index?, I get this from Xcode:

path/to/OnboardingViewController.swift:35:51: error: consecutive declarations on a line must be separated by ';'
    fileprivate var currentIndex: [OnboardingPage].Index?
                                                  ^
                                                  ;
path/to/OnboardingViewController.swift:35:51: error: expected declaration
    fileprivate var currentIndex: [OnboardingPage].Index?
                                                  ^
path/to/OnboardingViewController.swift:13:13: note: in declaration of 'OnboardingViewController'
final class OnboardingViewController: UIViewController {

I suppose this should also be considered a Swift bug, but SwiftLint shouldn't report issues that can't be fixed. For now, I'll use // swiftlint:disable:next syntactic_sugar

@marcelofabri marcelofabri added the enhancement Ideas for improvements of existing features and rules. label Dec 5, 2016
@marcelofabri
Copy link
Collaborator

I agree we should handle this, but I'm curious on why not just using Int 😅.

@ZevEisenberg
Copy link
Contributor Author

I was going to say that Int wasn't right, but I think that may have been a carryover from Swift 2. I just changed it to Int?, and looks like it works, so yay! Thanks for the suggestion. Pretty sure I had a valid reason for this in Swift 2, but who even knows any more.

@marcelofabri
Copy link
Collaborator

I don't know how we would catch this case vs using a valid static function.

Should we loose the rule and just skip if there's a dot after the type? In this case we would have a false positive. We'd just need to add a negative lookahead in the regex: (?!\\.).

For example:

func defaultStringArray(_ object: Any?) -> [String] {
    // wouldn't trigger, but it should
    return Array<String>.array(of: object) ?? []
}

Another option would be check if it's a var declaration/parameter, but this wouldn't prevent this issue in return types, for example:

// would trigger
func x(a: [Int], b: Int) -> Array<Int>.Index {
 // ..
}

However, this last case can be ignored if there's an arrow before the type. Am I missing any other cases/options?

@jpsim
Copy link
Collaborator

jpsim commented Dec 7, 2016

I think there's a way to further refine the rule to avoid false positives based on the ASTRule results, but that would require more research than I have time to do at the moment.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement Ideas for improvements of existing features and rules.
Projects
None yet
Development

No branches or pull requests

3 participants