Skip to content

Commit 3f08055

Browse files
committed
unified: Fix extraction bug
The special-cased labelExpr rules were problematic. They are now coveered by a combination of more general rules.
1 parent 5a2ec48 commit 3f08055

4 files changed

Lines changed: 22 additions & 34 deletions

File tree

‎unified/extractor/src/languages/swift/swift.rs‎

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,11 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
523523
=>
524524
(identifier #{name})
525525
),
526+
rule!(
527+
(patternExpr pattern: @p)
528+
=>
529+
expr { p }
530+
),
526531
// A `let`/`var` value-binding pattern (`let x`) inside a case or `if case`
527532
// preserves the binding specifier around its inner pattern.
528533
rule!(
@@ -672,39 +677,11 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
672677
tree!((call_expr callee: {callee} argument: {args}))
673678
}
674679
),
675-
// A call or enum-case pattern argument. Both use the shared `argument`
676-
// shape, preserving the optional label as `name` and the child as `value`.
677-
// The pattern-only shapes (`patternExpr`, `discardAssignmentExpr`) are
678-
// matched first; they never occur as ordinary call arguments.
679-
rule!(
680-
(labeledExpr
681-
label: _? @@lbl
682-
expression: (functionCallExpr
683-
calledExpression: @constructor
684-
arguments: _* @elements))
685-
=>
686-
argument {
687-
tree!((argument
688-
name_node: (identifier #{lbl})?
689-
value: (call_expr callee: {constructor} argument: {elements})))
690-
}
691-
),
692-
rule!(
693-
(labeledExpr label: _? @@lbl expression: (patternExpr pattern: @p))
694-
=>
695-
(argument name_node: (identifier #{lbl})? value: {p})
696-
),
697-
rule!(
698-
(labeledExpr label: _? @@lbl expression: (discardAssignmentExpr) @@wildcard)
699-
=>
700-
(argument name_node: (identifier #{lbl})? value: (identifier #{wildcard}))
701-
),
680+
// A call or enum-case pattern argument.
702681
rule!(
703682
(labeledExpr label: _? @@lbl expression: @val)
704683
=>
705-
argument {
706-
tree!((argument name_node: (identifier #{lbl})? value: {val}))
707-
}
684+
(argument name_node: (identifier #{lbl})? value: {val})
708685
),
709686
// Member access (`list.append`). The `declName` is itself a
710687
// `declReferenceExpr`; pull its `baseName` out as the member identifier.

‎unified/extractor/tests/corpus/swift/closures/nested-trailing-closure.output‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,19 @@ top_level source="⟨body⟩"
5858
argument:
5959
argument source="⟨value⟩"
6060
value:
61-
call_expr source="⟨callee⟩ { $0 * 2 }"
61+
call_expr source="⟨callee⟩⟨argument⟩"
6262
callee:
6363
member_access_expr source="⟨base⟩.⟨member_name_node⟩"
6464
base: identifier "xs" source="xs"
6565
member_name_node: identifier "map" source="map"
66+
argument:
67+
argument source="xs.map ⟨value⟩"
68+
value:
69+
function_expr source="⟨body⟩"
70+
body:
71+
block source="{ ⟨stmt⟩ }"
72+
stmt:
73+
binary_expr source="⟨left⟩ ⟨operator⟩ ⟨right⟩"
74+
left: identifier "$0" source="$0"
75+
operator: infix_operator "*" source="*"
76+
right: int_literal "2" source="2"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
| test.swift:2:9:2:9 | a | Unused local variable 'a' |
2-
| test.swift:12:9:12:9 | a | Unused local variable 'a' |
2+
| test.swift:16:13:16:13 | b | Unused local variable 'b' |

‎unified/ql/test/query-tests/unusedentities/test.swift‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ func t2() -> Int {
1010
return "df"
1111
}
1212
// Note: This currently fails because the trailing closure is not extracted correctly
13-
let a = 1 // $ SPURIOUS: Alert
13+
let a = 1
1414
print(foo() { _ in
1515
print(a)
16-
let b = 2 // $ MISSING: Alert[unified/unused-variable]
16+
let b = 2 // $ Alert[unified/unused-variable]
1717
})
1818
}

0 commit comments

Comments
 (0)