Skip to content

Line break incorrectly inserted before infix operator when trying to limit line length #1141

@BruceMcRooster

Description

@BruceMcRooster

Consider this example code.

let startVariableWithLongName = 21
let endVariableWithLongName = 42

let offsetVariableWithLongNameToMakeLinesSuperLong = 25

let myOpenEndedRangeThatHasAReallyLongName = (startVariableWithLongName + offsetVariableWithLongNameToMakeLinesSuperLong)..<(endVariableWithLongName + offsetVariableWithLongNameToMakeLinesSuperLong)

The final line is about 200 characters, which swift format attempts to fix to 100 by default (I believe).
Running swift format using Swift 6.2.3 results in the following:

let startVariableWithLongName = 21
let endVariableWithLongName = 42

let offsetVariableWithLongNameToMakeLinesSuperLong = 25

let myOpenEndedRangeThatHasAReallyLongName =
  (startVariableWithLongName + offsetVariableWithLongNameToMakeLinesSuperLong)
  ..<(endVariableWithLongName + offsetVariableWithLongNameToMakeLinesSuperLong)

which results in an error, since ..< is now treated as a prefix operator rather than the infix it should be.

This example is contrived, but this is quite realistic when code is deeply nested with shorter variable names.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions