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

multiline_parameters false positive when closures have default values #1912

Closed
allen-zeng opened this Issue Oct 18, 2017 · 4 comments

Comments

Projects
None yet
3 participants
@allen-zeng
Contributor

allen-zeng commented Oct 18, 2017

New Issue Checklist

Bug Report

When there is a default parameter for a closure, linting fails.

Complete output when running SwiftLint, including the stack trace and command used
$ echo "func function(\nparam1: Int,\nparam2: @escaping (Int, Int) -> Void = { _, _ in }\n) {\n}" | swiftlint lint --use-stdin
Loading configuration from '.swiftlint.yml'
<nopath>:1:6: warning: Multiline Parameters Violation: Functions and methods parameters should be either on the same line, or one per line. (multiline_parameters)
Done linting! Found 1 violation, 0 serious in 1 file.

Environment

  • SwiftLint version (run swiftlint version to be sure)? 0.23.1
  • Installation method used (Homebrew, CocoaPods, building from source, etc)? Homebrew or CocoaPods, happens on both
  • Paste your configuration file:
disabled_rules:
  - closing_brace
  - cyclomatic_complexity
  - force_cast
  - force_try
  - identifier_name
  - line_length
  - nesting
  - notification_center_detachment
  - todo
  - trailing_comma
  - trailing_whitespace
  - type_name
  - weak_delegate
  - file_length
excluded:
  - Carthage
  - Pods
colon:
  flexible_right_spacing: true
line_length: 400
type_body_length:
  - 2048
  - 2048
function_body_length:
  - 400
  - 400
force_try:
  severity: warning
reporter: "xcode"
large_tuple:
  - 4
  - 4
opt_in_rules:
  - closure_end_indentation
  - closure_spacing
  - empty_count
  - explicit_init
  - fatal_error_message
  - first_where
  - joined_default_parameter
  - multiline_parameters
  - overridden_super_call
  - prohibited_super_call
  - vertical_parameter_alignment_on_call
  • Which Xcode version are you using (check xcode-select -p)? 9.0
// This triggers a violation:
func function(
    param1: Int,
    param2: @escaping (Int, Int) -> Void = { _, _ in }
) {
}

func function(
    param1: Int,
    param2: @escaping (Int) -> Void = { _ in }
) {
}

@allen-zeng allen-zeng changed the title from multiline_parameters false positive when default values is a closure and contains commas to multiline_parameters false positive when closures have default values Oct 18, 2017

@ornithocoder

This comment has been minimized.

Show comment
Hide comment
@ornithocoder

ornithocoder Oct 18, 2017

Contributor

That's a nice catch, @allen-zeng. Thanks! For debugging purposes, here's the report from sourcekitten

func function(param1: Int,
              param2: @escaping (Int, Int, Int) -> Void = { _, _, _ in }) { }
sourcekitten structure --file ~/Desktop/file.swift 
{
  "key.diagnostic_stage" : "source.diagnostic.stage.swift.parse",
  "key.substructure" : [
    {
      "key.bodyoffset" : 102,
      "key.nameoffset" : 5,
      "key.accessibility" : "source.lang.swift.accessibility.internal",
      "key.length" : 104,
      "key.substructure" : [
        {
          "key.nameoffset" : 14,
          "key.typename" : "Int",
          "key.length" : 11,
          "key.name" : "param1",
          "key.kind" : "source.lang.swift.decl.var.parameter",
          "key.offset" : 14,
          "key.namelength" : 6
        },
        {
          "key.nameoffset" : 41,
          "key.typename" : "@escaping (Int, Int, Int) -> Void",
          "key.length" : 58,
          "key.name" : "param2",
          "key.kind" : "source.lang.swift.decl.var.parameter",
          "key.offset" : 41,
          "key.namelength" : 6
        },
        {
          "key.nameoffset" : 0,
          "key.kind" : "source.lang.swift.decl.var.parameter",
          "key.offset" : 87,
          "key.length" : 1,
          "key.namelength" : 0
        },
        {
          "key.nameoffset" : 0,
          "key.kind" : "source.lang.swift.decl.var.parameter",
          "key.offset" : 90,
          "key.length" : 1,
          "key.namelength" : 0
        },
        {
          "key.nameoffset" : 0,
          "key.kind" : "source.lang.swift.decl.var.parameter",
          "key.offset" : 93,
          "key.length" : 1,
          "key.namelength" : 0
        },
        {
          "key.namelength" : 0,
          "key.nameoffset" : 0,
          "key.length" : 14,
          "key.bodyoffset" : 86,
          "key.offset" : 85,
          "key.bodylength" : 12,
          "key.kind" : "source.lang.swift.stmt.brace"
        }
      ],
      "key.name" : "function(param1:param2:)",
      "key.kind" : "source.lang.swift.decl.function.free",
      "key.bodylength" : 1,
      "key.namelength" : 95,
      "key.offset" : 0
    }
  ],
  "key.offset" : 0,
  "key.length" : 105
}
Contributor

ornithocoder commented Oct 18, 2017

That's a nice catch, @allen-zeng. Thanks! For debugging purposes, here's the report from sourcekitten

func function(param1: Int,
              param2: @escaping (Int, Int, Int) -> Void = { _, _, _ in }) { }
sourcekitten structure --file ~/Desktop/file.swift 
{
  "key.diagnostic_stage" : "source.diagnostic.stage.swift.parse",
  "key.substructure" : [
    {
      "key.bodyoffset" : 102,
      "key.nameoffset" : 5,
      "key.accessibility" : "source.lang.swift.accessibility.internal",
      "key.length" : 104,
      "key.substructure" : [
        {
          "key.nameoffset" : 14,
          "key.typename" : "Int",
          "key.length" : 11,
          "key.name" : "param1",
          "key.kind" : "source.lang.swift.decl.var.parameter",
          "key.offset" : 14,
          "key.namelength" : 6
        },
        {
          "key.nameoffset" : 41,
          "key.typename" : "@escaping (Int, Int, Int) -> Void",
          "key.length" : 58,
          "key.name" : "param2",
          "key.kind" : "source.lang.swift.decl.var.parameter",
          "key.offset" : 41,
          "key.namelength" : 6
        },
        {
          "key.nameoffset" : 0,
          "key.kind" : "source.lang.swift.decl.var.parameter",
          "key.offset" : 87,
          "key.length" : 1,
          "key.namelength" : 0
        },
        {
          "key.nameoffset" : 0,
          "key.kind" : "source.lang.swift.decl.var.parameter",
          "key.offset" : 90,
          "key.length" : 1,
          "key.namelength" : 0
        },
        {
          "key.nameoffset" : 0,
          "key.kind" : "source.lang.swift.decl.var.parameter",
          "key.offset" : 93,
          "key.length" : 1,
          "key.namelength" : 0
        },
        {
          "key.namelength" : 0,
          "key.nameoffset" : 0,
          "key.length" : 14,
          "key.bodyoffset" : 86,
          "key.offset" : 85,
          "key.bodylength" : 12,
          "key.kind" : "source.lang.swift.stmt.brace"
        }
      ],
      "key.name" : "function(param1:param2:)",
      "key.kind" : "source.lang.swift.decl.function.free",
      "key.bodylength" : 1,
      "key.namelength" : 95,
      "key.offset" : 0
    }
  ],
  "key.offset" : 0,
  "key.length" : 105
}
@ornithocoder

This comment has been minimized.

Show comment
Hide comment
@ornithocoder

ornithocoder Oct 18, 2017

Contributor

@marcelofabri would checking if "key.typename" is present would enough for this one?

Contributor

ornithocoder commented Oct 18, 2017

@marcelofabri would checking if "key.typename" is present would enough for this one?

ornithocoder added a commit to ornithocoder/personal-fork-swiftlint that referenced this issue Oct 18, 2017

Fix false positive in `multiline_parameters` rule
when parameter is a closure has default value. Fixes realm#1912.
@allen-zeng

This comment has been minimized.

Show comment
Hide comment
@allen-zeng

allen-zeng Oct 18, 2017

Contributor

That was quick! Thanks @ornithocoder !

Contributor

allen-zeng commented Oct 18, 2017

That was quick! Thanks @ornithocoder !

@ornithocoder

This comment has been minimized.

Show comment
Hide comment
@ornithocoder

ornithocoder Oct 18, 2017

Contributor

You're welcome @allen-zeng :D

Contributor

ornithocoder commented Oct 18, 2017

You're welcome @allen-zeng :D

@marcelofabri marcelofabri added the bug label Oct 19, 2017

ornithocoder added a commit to ornithocoder/personal-fork-swiftlint that referenced this issue Oct 21, 2017

Fix false positive in `multiline_parameters` rule
when parameter is a closure has default value. Fixes realm#1912.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment