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

Enable implicit_return and fix all violations #5658

Merged

Conversation

mildm8nnered
Copy link
Collaborator

The only changes here are

  1. Enabling (or not disabling) implicit_return
  2. Fix all existing 300+ violations
  3. Whitespace adjustments for parameter alignment where removing return threw things out.
@SwiftLintBot
Copy link

SwiftLintBot commented Jul 8, 2024

2 Warnings
⚠️ Big PR
⚠️ If this is a user-facing change, please include a CHANGELOG entry to credit yourself!
You can find it at CHANGELOG.md.
17 Messages
📖 Linting Aerial with this PR took 0.99s vs 0.99s on main (0% slower)
📖 Linting Alamofire with this PR took 1.36s vs 1.37s on main (0% faster)
📖 Linting Brave with this PR took 8.08s vs 8.08s on main (0% slower)
📖 Linting DuckDuckGo with this PR took 4.48s vs 4.52s on main (0% faster)
📖 Linting Firefox with this PR took 11.59s vs 11.55s on main (0% slower)
📖 Linting Kickstarter with this PR took 10.17s vs 10.15s on main (0% slower)
📖 Linting Moya with this PR took 0.56s vs 0.56s on main (0% slower)
📖 Linting NetNewsWire with this PR took 2.75s vs 2.76s on main (0% faster)
📖 Linting Nimble with this PR took 0.81s vs 0.82s on main (1% faster)
📖 Linting PocketCasts with this PR took 8.85s vs 8.73s on main (1% slower)
📖 Linting Quick with this PR took 0.46s vs 0.46s on main (0% slower)
📖 Linting Realm with this PR took 5.06s vs 5.07s on main (0% faster)
📖 Linting Sourcery with this PR took 2.52s vs 2.53s on main (0% faster)
📖 Linting Swift with this PR took 4.93s vs 4.92s on main (0% slower)
📖 Linting VLC with this PR took 1.34s vs 1.34s on main (0% slower)
📖 Linting Wire with this PR took 19.31s vs 19.26s on main (0% slower)
📖 Linting WordPress with this PR took 14.34s vs 14.35s on main (0% faster)

Here's an example of your CHANGELOG entry:

* Enable `implicit_return` and fix all violations.  
  [mildm8nnered](https://github.com/mildm8nnered)
  [#issue_number](https://github.com/realm/SwiftLint/issues/issue_number)

note: There are two invisible spaces after the entry's text.

Generated by 🚫 Danger

@mildm8nnered mildm8nnered marked this pull request as ready for review July 8, 2024 08:25
@SimplyDanny
Copy link
Collaborator

SimplyDanny commented Jul 8, 2024

  1. Whitespace adjustments for parameter alignment where removing return threw things out.

Can you give an example for such an adjustment? Is it something that the rewriter could do better?

@mildm8nnered
Copy link
Collaborator Author

5. Whitespace adjustments for parameter alignment where removing return threw things out.

Can you give an example for such an adjustment? Is it something that the rewriter could do better?

So I didn't actually use the rewriter, because I didn't spot that the rule subclassed SwiftSyntaxCorrectableRule 🤦, so much so that I even thought "Hey, I could make this rewritable while I'm here".

Trying the rewriter, it actually has a few problems:

In YamlParser.swift

    static func constructExpandingEnvVars(env: [String: String]) -> (_ scalar: Node.Scalar) -> String? {
        return { (scalar: Node.Scalar) -> String? in
            return scalar.string.expandingEnvVars(env: env)
        }
    }

becomes

    static func constructExpandingEnvVars(env: [String: String]) -> (_ scalar: Node.Scalar) -> String? {
        { (scalar: Node.Scalar) -> String? in
            return string.expandingEnvVars(env: env)
        }
    }

which fails to compile with

/Users/martin.redington/Documents/Source/SwiftLint/Source/SwiftLintCore/Models/YamlParser.swift:44:20: error: cannot find 'string' in scope
            return string.expandingEnvVars(env: env)

Manually I had

    static func constructExpandingEnvVars(env: [String: String]) -> (_ scalar: Node.Scalar) -> String? {
        { (scalar: Node.Scalar) -> String? in
            scalar.string.expandingEnvVars(env: env)
        }
    }

Rolling through the autocorrects, it looks like when we have nested implicit returns, that causes problems.

I haven't looked into what's going on, but I suspect that the offsets for the nested implicit return are off once the outer one has been corrected.

The whitespace issues were actually much more minor. In a case like SyntacticSugarRule

            return StyleViolation(ruleDescription: Self.description,
                                  severity: configuration.severity,
                                  location: Location(file: file, byteOffset: ByteCount(violation.position)),
                                  reason: violation.type.violationReason)

corrected to

            StyleViolation(ruleDescription: Self.description,
                                  severity: configuration.severity,
                                  location: Location(file: file, byteOffset: ByteCount(violation.position)),
                                  reason: violation.type.violationReason)

This will then violate one of the parameter alignment rules.

I'll write up the Rewriter issues separately - it would be nice to fix that, as this is a rule that people probably have a lot of violations of, and doing them by hand is probably a pain for most people.

Something I also missed running the auto-correction was the ability to specify a single rule to enable on the command line. Please autocorrect just this one rule - unless I'm missing something and we can already do that.

@mildm8nnered mildm8nnered force-pushed the mildm8nnered-enable-implicit-return branch from fccfec2 to 233043a Compare July 9, 2024 22:09
@mildm8nnered mildm8nnered merged commit ed10aec into realm:main Jul 10, 2024
12 checks passed
@mildm8nnered mildm8nnered deleted the mildm8nnered-enable-implicit-return branch July 10, 2024 11:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants