Skip to content

SwiftFixIt: Skip duplicate insertion fix-its #8892

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

Merged
merged 2 commits into from
Jul 9, 2025
Merged
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
protocol P {
}
protocol Q {
}

func test1(_: any P) {
}
Expand All @@ -14,3 +16,6 @@ func test3() {
func test4() {
var x = 42
}

func test5(_: any P & Q) {
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
protocol P {
}
protocol Q {
}

func test1(_: P) {
}
Expand All @@ -14,3 +16,6 @@ func test3() {
func test4() {
var x = 42
}

func test5(_: P & Q) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
protocol P {}
protocol Q {}

@MainActor
struct S: nonisolated P & Q {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
protocol P {}
protocol Q {}

@MainActor
struct S: P & Q {}
6 changes: 5 additions & 1 deletion Sources/SwiftFixIt/SwiftFixIt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,11 @@ extension SwiftFixIt {
}
}

let result = SwiftIDEUtils.FixItApplier.apply(edits: consume edits, to: sourceFile.syntax)
let result = SwiftIDEUtils.FixItApplier.apply(
edits: consume edits,
to: sourceFile.syntax,
allowDuplicateInsertions: false
)

try self.fileSystem.writeFileContents(sourceFile.path, string: consume result)
}
Expand Down
8 changes: 5 additions & 3 deletions Tests/CommandsTests/PackageCommandTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2112,7 +2112,7 @@ class PackageCommandTestCase: CommandsBuildProviderTestCase {
"skipping because test environment compiler doesn't support `-print-supported-features`"
)

func doMigration(featureName: String, expectedSummary: String) async throws {
func doMigration(featureName: String, expectedSummary: String) async throws {
try await fixture(name: "SwiftMigrate/\(featureName)Migration") { fixturePath in
let sourcePaths: [AbsolutePath]
let fixedSourcePaths: [AbsolutePath]
Expand Down Expand Up @@ -2151,9 +2151,11 @@ class PackageCommandTestCase: CommandsBuildProviderTestCase {
}
}

try await doMigration(featureName: "ExistentialAny", expectedSummary: "Applied 3 fix-its in 1 file")
// When updating these, make sure we keep testing both the singular and
// plural forms of the nouns in the summary.
try await doMigration(featureName: "ExistentialAny", expectedSummary: "Applied 5 fix-its in 1 file")
try await doMigration(featureName: "StrictMemorySafety", expectedSummary: "Applied 1 fix-it in 1 file")
try await doMigration(featureName: "InferIsolatedConformances", expectedSummary: "Applied 1 fix-it in 1 file")
try await doMigration(featureName: "InferIsolatedConformances", expectedSummary: "Applied 3 fix-its in 2 files")
}

func testBuildToolPlugin() async throws {
Expand Down
15 changes: 4 additions & 11 deletions Tests/SwiftFixItTests/FilteringTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -412,19 +412,12 @@ struct FilteringTests {
}
}

@Test
func testDuplicateInsertionFixIts() throws {
withKnownIssue("FIXME: Filter out duplicate insertion fix-its") {
try self._testDuplicateInsertionFixIts()
}
}

func _testDuplicateInsertionFixIts() throws {
try testAPI1File { path in
.init(
edits: .init(input: "var x = 1", result: "@W var yx = 21"),
summary: .init(
// 4 because skipped by SwiftIDEUtils.FixItApplier, not SwiftFixIt.
// 6 because skipped by SwiftIDEUtils.FixItApplier, not SwiftFixIt.
numberOfFixItsApplied: 6,
numberOfFilesChanged: 1
),
Expand Down Expand Up @@ -470,7 +463,7 @@ struct FilteringTests {
text: "error3_fixit1",
location: .init(path: path, line: 1, column: 3),
fixIts: [
// FIXME: Should be skipped.
// Skipped, duplicate insertion.
.init(
start: .init(path: path, line: 1, column: 1),
end: .init(path: path, line: 1, column: 1),
Expand Down Expand Up @@ -500,7 +493,7 @@ struct FilteringTests {
text: "error5_note1",
location: .init(path: path, line: 1, column: 9),
fixIts: [
// FIXME: Should be skipped.
// Skipped, duplicate insertion.
.init(
start: .init(path: path, line: 1, column: 9),
end: .init(path: path, line: 1, column: 9),
Expand All @@ -519,7 +512,7 @@ struct FilteringTests {
text: "error6_note1",
location: .init(path: path, line: 1, column: 5),
fixIts: [
// FIXME: Should be skipped.
// Skipped, duplicate insertion.
.init(
start: .init(path: path, line: 1, column: 5),
end: .init(path: path, line: 1, column: 5),
Expand Down