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

False positive on closure_parameter_position with Swift 2.3 #1019

Closed
jrmybrault opened this issue Dec 21, 2016 · 10 comments · Fixed by #1081
Closed

False positive on closure_parameter_position with Swift 2.3 #1019

jrmybrault opened this issue Dec 21, 2016 · 10 comments · Fixed by #1081
Labels
bug Unexpected and reproducible misbehavior.

Comments

@jrmybrault
Copy link

jrmybrault commented Dec 21, 2016

Hey,

the following code triggers a parameter position violation for the completion closure :

UIView.animateWithDuration(0.3, animations: {
  self.disableInteractionRightView.alpha = 0
  }, completion: { _ in
    self.disableInteractionRightView.hidden = true
})

Do you have any idea why ?

@marcelofabri
Copy link
Collaborator

Are you sure this isolated example triggers? I wasn't able to reproduce it.

@BluMist
Copy link

BluMist commented Dec 21, 2016

trivial example: if you add a () inside the completion closure, it would trigger.

@marcelofabri
Copy link
Collaborator

Like this?

UIView.animateWithDuration(0.3, animations: {
   self.disableInteractionRightView.alpha = 0
}, completion: { _ in
   self.something()
})

I can't reproduce it this way as well. Can you provide a full example?

@BluMist
Copy link

BluMist commented Dec 21, 2016

oh, misread the issue, wrong rule, I was thinking empty_parentheses_with_trailing_closure, will create a new issue separately

@AliSoftware
Copy link
Contributor

@marcelofabri To be more specific about the original issue, I saw it wth my eyes in @jrmybrault 's Xcode, the warning was about a violation of the closure_parameter_position rule and it was located on the letter c of completion:

Didn't try locally on my own Xcode though.
iirc, @jrmybrault did update to latest swiftlint very recently, like the same morning or something.

@marcelofabri
Copy link
Collaborator

Maybe something else in the file is affecting this. If you could reduce to a triggering example, it would be great 💯

This rule is new in SwiftLint 0.14 which as released on Homebrew today.

@AliSoftware
Copy link
Contributor

@marcelofabri We actually don't work tomorrow nor the day after (Xmas period), And idk if @jrmybrault works next week tbh 😄 but will keep that in mind

@marcelofabri marcelofabri added the repro-needed Issues that cannot be reproduced or miss proper descriptive examples. label Dec 23, 2016
@jrmybrault
Copy link
Author

jrmybrault commented Dec 27, 2016

@marcelofabri : Actually, I reproduce it only on Xcode 7.x.x, it seems fine on Xcode 8.x. I thought it could be related to spaces & indentations but the settings are identical.

Xcode 7.3.1 : triggers

import UIKit

final class EmptyViewController: UIViewController {

  override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    UIView.animateWithDuration(0.3, animations: {
      self.view.alpha = 0
      }, completion: { _ in
        self.view.hidden = true
    })
  }
}

Xcode 8.2 : does not trigger

import UIKit

final class EmptyViewController: UIViewController {

  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    UIView.animate(withDuration: 0.3, animations: {
      self.view.alpha = 0
    }, completion: { _ in
      self.view.isHidden = true
    })
  }
}

@marcelofabri
Copy link
Collaborator

@jrmybrault Thanks for doing this! I'm downloading Xcode 7.3.1 right now and will verify it later.

My theory is that SourceKit is returning different structures for this file.

@marcelofabri
Copy link
Collaborator

The structures are a bit different, which may explain the false positive:

Xcode 7.3.1
{
	"key.namelength": 30,
	"key.nameoffset": 83,
	"key.bodylength": 161,
	"key.accessibility": "source.lang.swift.accessibility.internal",
	"key.length": 199,
	"key.substructure": [{
		"key.nameoffset": 0,
		"key.typename": "Bool",
		"key.length": 14,
		"key.name": "animated",
		"key.kind": "source.lang.swift.decl.var.parameter",
		"key.namelength": 0,
		"key.offset": 98
	}, {
		"key.offset": 117,
		"key.nameoffset": 117,
		"key.length": 30,
		"key.name": "super.viewWillAppear",
		"key.bodyoffset": 138,
		"key.kind": "source.lang.swift.expr.call",
		"key.namelength": 20,
		"key.bodylength": 8
	}, {
		"key.bodylength": 95,
		"key.nameoffset": 150,
		"key.substructure": [{
			"key.offset": 177,
			"key.nameoffset": 0,
			"key.length": 3,
			"key.bodyoffset": 177,
			"key.bodylength": 3,
			"key.kind": "source.lang.swift.decl.var.parameter",
			"key.namelength": 0
		}, {
			"key.offset": 182,
			"key.nameoffset": 182,
			"key.length": 41,
			"key.name": "animations",
			"key.bodyoffset": 194,
			"key.kind": "source.lang.swift.decl.var.parameter",
			"key.namelength": 10,
			"key.bodylength": 29
		}, {
			"key.bodylength": 35,
			"key.nameoffset": 225,
			"key.substructure": [{
				"key.typename": "_",
				"key.nameoffset": 0,
				"key.kind": "source.lang.swift.decl.var.parameter",
				"key.length": 1,
				"key.namelength": 0,
				"key.offset": 239
			}],
			"key.length": 47,
			"key.name": "completion",
			"key.bodyoffset": 237,
			"key.kind": "source.lang.swift.decl.var.parameter",
			"key.offset": 225,
			"key.namelength": 10
		}],
		"key.length": 123,
		"key.name": "UIView.animateWithDuration",
		"key.bodyoffset": 177,
		"key.kind": "source.lang.swift.expr.call",
		"key.offset": 150,
		"key.namelength": 26
	}],
	"key.name": "viewWillAppear(_:)",
	"key.bodyoffset": 115,
	"key.attributes": [{
		"key.attribute": "source.decl.attribute.override"
	}],
	"key.kind": "source.lang.swift.decl.function.method.instance",
	"key.offset": 78
}

Xcode 8.2.1
{
	"key.namelength": 32,
	"key.nameoffset": 83,
	"key.bodylength": 164,
	"key.accessibility": "source.lang.swift.accessibility.internal",
	"key.length": 204,
	"key.substructure": [{
		"key.nameoffset": 0,
		"key.typename": "Bool",
		"key.length": 16,
		"key.name": "animated",
		"key.kind": "source.lang.swift.decl.var.parameter",
		"key.namelength": 0,
		"key.offset": 98
	}, {
		"key.offset": 119,
		"key.nameoffset": 119,
		"key.length": 30,
		"key.name": "super.viewWillAppear",
		"key.bodyoffset": 140,
		"key.kind": "source.lang.swift.expr.call",
		"key.namelength": 20,
		"key.bodylength": 8
	}, {
		"key.bodylength": 110,
		"key.nameoffset": 152,
		"key.substructure": [{
			"key.offset": 167,
			"key.nameoffset": 167,
			"key.length": 17,
			"key.name": "withDuration",
			"key.bodyoffset": 181,
			"key.kind": "source.lang.swift.expr.argument",
			"key.namelength": 12,
			"key.bodylength": 3
		}, {
			"key.offset": 186,
			"key.nameoffset": 186,
			"key.length": 39,
			"key.name": "animations",
			"key.bodyoffset": 198,
			"key.kind": "source.lang.swift.expr.argument",
			"key.namelength": 10,
			"key.bodylength": 27
		}, {
			"key.bodylength": 38,
			"key.nameoffset": 227,
			"key.substructure": [{
				"key.nameoffset": 0,
				"key.kind": "source.lang.swift.decl.var.parameter",
				"key.namelength": 0,
				"key.offset": 241,
				"key.length": 1
			}],
			"key.length": 50,
			"key.name": "completion",
			"key.bodyoffset": 239,
			"key.kind": "source.lang.swift.expr.argument",
			"key.offset": 227,
			"key.namelength": 10
		}],
		"key.length": 126,
		"key.name": "UIView.animate",
		"key.bodyoffset": 167,
		"key.kind": "source.lang.swift.expr.call",
		"key.offset": 152,
		"key.namelength": 14
	}],
	"key.name": "viewWillAppear(_:)",
	"key.bodyoffset": 117,
	"key.attributes": [{
		"key.attribute": "source.decl.attribute.override"
	}],
	"key.kind": "source.lang.swift.decl.function.method.instance",
	"key.offset": 78
}

Note that in 8.x, the parameters are inside a source.lang.swift.expr.argument, while in 7.x they're inside another source.lang.swift.decl.var.parameter.

Here're the violations that were trigged:

file.swift:8:34: warning: Unused Closure Parameter Violation: Unused parameter "animations" in a closure should be replaced with _. (unused_closure_parameter)
file.swift:10:7: warning: Unused Closure Parameter Violation: Unused parameter "completion" in a closure should be replaced with _. (unused_closure_parameter)
file.swift:10:7: warning: Closure Parameter Position Violation: Closure parameters should be on the same line as opening brace. (closure_parameter_position)

@marcelofabri marcelofabri added bug Unexpected and reproducible misbehavior. and removed repro-needed Issues that cannot be reproduced or miss proper descriptive examples. labels Dec 27, 2016
@marcelofabri marcelofabri changed the title Triggered Closure Parameter Position Violation False positive on closure_parameter_position with Swift 2.3 Dec 29, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Unexpected and reproducible misbehavior.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants