Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@

### Bug Fixes

* Make `unowned_variable_capture` ignore `unowned(unsafe)` captures by
default and add an `include_unsafe` option for projects that still want them
reported.
[Yurii Bakurov](https://github.com/Yurii201811)
[#6817](https://github.com/realm/SwiftLint/issues/6817)

* Fix baseline writing to store file locations as paths relative to the current working directory,
restoring baseline portability and avoiding absolute `file://` paths in generated baseline files.
[SimplyDanny](https://github.com/SimplyDanny)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import SwiftSyntax

@SwiftSyntaxRule(optIn: true)
struct UnownedVariableCaptureRule: Rule {
var configuration = SeverityConfiguration<Self>(.warning)
var configuration = UnownedVariableCaptureConfiguration()

static let description = RuleDescription(
identifier: "unowned_variable_capture",
Expand All @@ -15,6 +15,7 @@ struct UnownedVariableCaptureRule: Rule {
"foo { [weak self] param in _ }",
"foo { [weak bar] in _ }",
"foo { [weak bar] param in _ }",
"foo { [unowned(unsafe) self] in _ }",
"foo { bar in _ }",
"foo { $0 }",
"""
Expand All @@ -30,15 +31,24 @@ struct UnownedVariableCaptureRule: Rule {
triggeringExamples: #examples([
"foo { [↓unowned self] in _ }",
"foo { [↓unowned bar] in _ }",
"foo { [↓unowned(safe) self] in _ }",
"foo { [bar, ↓unowned self] in _ }",
"foo { [↓unowned(unsafe) self] in _ }"
.asExample(configuration: ["include_unsafe": true]),
])
)
}

private extension UnownedVariableCaptureRule {
final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> {
override func visitPost(_ node: TokenSyntax) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's have a visitPost(_ node: ClosureCaptureSpecifierSyntax) instead which allows us to check for specifier and detail directly.

if case .keyword(.unowned) = node.tokenKind, node.parent?.is(ClosureCaptureSpecifierSyntax.self) == true {
guard case .keyword(.unowned) = node.tokenKind,
let specifier = node.parent?.as(ClosureCaptureSpecifierSyntax.self) else {
return
}

let isUnsafe = specifier.tokens(viewMode: .sourceAccurate).contains { $0.text == "unsafe" }
if !isUnsafe || configuration.includeUnsafe {
violations.append(node.positionAfterSkippingLeadingTrivia)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import SwiftLintCore

@AutoConfigParser
struct UnownedVariableCaptureConfiguration: SeverityBasedRuleConfiguration {
@ConfigurationElement(key: "severity")
private(set) var severityConfiguration = SeverityConfiguration<Parent>(.warning)
@ConfigurationElement(key: "include_unsafe")
private(set) var includeUnsafe = false

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's call it allow_explicit_unsafe_unowned. It should be false by default to match the current behavior. It should be opt-in.

}
Original file line number Diff line number Diff line change
Expand Up @@ -1335,6 +1335,7 @@ unneeded_throws_rethrows:
correctable: true
unowned_variable_capture:
severity: warning
include_unsafe: false
meta:
opt-in: true
correctable: false
Expand Down