Skip to content

Conversation

@travisstaloch
Copy link
Contributor

this patch makes autofix add discards for unused loop and switch case captures which have curlies. it prevents adding duplicate 'remove capture' actions by checking previous action ranges.

it removes special casing of index captures now that multi for loops have arrived.

  • make getCaptureLoc() return only single Loc
  • remove CaptureLocs struct which is no longer used
  • add DiagnosticKind.@"switch tag capture" to handle 'inline else => |x, tag|' discards
  • add test "getCaptureLoc"

this patch makes autofix add discards for unused loop and switch case
captures which have curlies.  it prevents adding duplicate
'remove capture' actions by checking previous action ranges.

it removes special casing of index captures now that multi for loops
have arrived.

* make getCaptureLoc() return only single Loc
* remove CaptureLocs struct which is no longer used
* add DiagnosticKind.@"switch tag capture" to handle
  'inline else => |x, tag|' discards
* add test "getCaptureLoc"
@travisstaloch
Copy link
Contributor Author

travisstaloch commented May 27, 2023

addresses #689

tested with

test {
    for (0..10, 0..10, 0..10) |i, j, k| {}
    switch (union(enum) {}{}) {
        inline .a => |cap, tag| {},
    }
    if (null) |x| {}
    if (null) |v| {} else |e| {}
    _ = null catch |e| {};
}

this becomes:

test {
    for (0..10, 0..10, 0..10) |i, j, k| {
        _ = k;
        _ = j;
        _ = i;
    }
    switch (union(enum) {}{}) {
        inline .a => |cap, tag| {
            _ = tag;
            _ = cap;
        },
    }
    if (null) |x| {
        _ = x;
    }
    if (null) |v| {
        _ = v;
    } else |e| {
        _ = e;
    }
    _ = null catch |e| {
        _ = e;
    };
}

* reject missing opening '|'
* reject capture group of only spaces
* add tests for these cases
@leecannon
Copy link
Member

This looks good 👍

@leecannon leecannon merged commit 0de4541 into zigtools:master May 27, 2023
@travisstaloch travisstaloch deleted the issue-689-discard-caps branch May 27, 2023 11:36
@Techatrix Techatrix mentioned this pull request May 27, 2023
24 tasks
@travisstaloch
Copy link
Contributor Author

interesting note: just tested a release-fast build and the discard order is reversed back to source order.

test {
    for (0..10, 0..10, 0..10) |i, j, k| {
        _ = i;
        _ = j;
        _ = k;
    }
...

glad to see they're in source order. not sure why they were reversed in the debug build.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants