Skip to content

Commit e3bbe46

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 373ec13 commit e3bbe46

4 files changed

Lines changed: 22 additions & 39 deletions

File tree

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

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,11 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
533533
=>
534534
(identifier #{name})
535535
),
536+
rule!(
537+
(patternExpr pattern: @p)
538+
=>
539+
expr { p }
540+
),
536541
// A `let`/`var` value-binding pattern (`let x`) inside a case or `if case`
537542
// preserves the binding specifier around its inner pattern.
538543
rule!(
@@ -696,44 +701,11 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
696701
tree!((call_expr callee: {callee} argument: {args}))
697702
}
698703
),
699-
// A call or enum-case pattern argument. Both use the shared `argument`
700-
// shape, preserving the optional label as `name` and the child as `value`.
701-
// The pattern-only shapes (`patternExpr`, `discardAssignmentExpr`) are
702-
// matched first; they never occur as ordinary call arguments.
703-
rule!(
704-
(labeledExpr
705-
label: _? @@lbl
706-
expression: (functionCallExpr
707-
calledExpression: @constructor
708-
arguments: _* @elements) @@call)
709-
=>
710-
argument {
711-
let value = tree_at!(
712-
ctx,
713-
call,
714-
(call_expr callee: {constructor} argument: {elements})
715-
);
716-
tree!((argument
717-
name_node: (identifier #{lbl})?
718-
value: {value}))
719-
}
720-
),
721-
rule!(
722-
(labeledExpr label: _? @@lbl expression: (patternExpr pattern: @p))
723-
=>
724-
(argument name_node: (identifier #{lbl})? value: {p})
725-
),
726-
rule!(
727-
(labeledExpr label: _? @@lbl expression: (discardAssignmentExpr) @@wildcard)
728-
=>
729-
(argument name_node: (identifier #{lbl})? value: (identifier #{wildcard}))
730-
),
704+
// A call or enum-case pattern argument.
731705
rule!(
732706
(labeledExpr label: _? @@lbl expression: @val)
733707
=>
734-
argument {
735-
tree!((argument name_node: (identifier #{lbl})? value: {val}))
736-
}
708+
(argument name_node: (identifier #{lbl})? value: {val})
737709
),
738710
// Member access (`list.append`). The `declName` is itself a
739711
// `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="⟨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)