Skip to content

Conversation

oreflow
Copy link
Collaborator

@oreflow oreflow commented Jun 24, 2025

When detecting whether symbols are used or not, variables referenced as argument defaults of the inner def would count as "use" in the inner scope, and hence falsely trigger "unused" warning.

Example:

def outer_def(name, foo = "foo"):
    def inner_def(foo = foo):
        print(foo)  # buildifier: disable=print

    inner_def()

Unused variable check for the above function would

  1. Check outer_def for variable usage
  2. Recursively check inner_def for variable usage
  • Detect that foo is used
  • Omit foo from returned usedSymbolsFromOuterScope since foo is a local variable
  1. Receive no "usedSymbol" for foo
  2. Output "unused-variable" warning

If there are RHS idents in a def statement, these idents are always from an outer scope, so these can check the later inner-scope definedSymbols check.

When detecting whether symbols are used or not, variables referenced as argument defaults of the inner def would count as "use" in the inner scope, and hence falsely trigger "unused" warning.

Example:

```
def outer_def(name, foo = "foo"):
    def inner_def(foo = foo):
        print(foo)  # buildifier: disable=print

    inner_def()
```

Unused variable check for the above function would
1. Check outer_def for variable usage
2. Recursively check inner_def for variable usage
  - Detect that `foo` is used
  - Omit `foo` from returned usedSymbolsFromOuterScope since `foo` is a local variable
3. Receive no "usedSymbol" for `foo`
4. Output "unused-variable" warning

If there are RHS idents in a def statement, these idents are always from an outer scope, so these can check the later inner-scope definedSymbols check.
@oreflow oreflow enabled auto-merge (squash) June 24, 2025 11:56
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.

1 participant