Skip to content

Bugfix false positive for "unused" variable for nested defs#1376

Merged
oreflow merged 1 commit intomainfrom
nested-defs
Nov 7, 2025
Merged

Bugfix false positive for "unused" variable for nested defs#1376
oreflow merged 1 commit intomainfrom
nested-defs

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.

@oreflow oreflow enabled auto-merge (squash) June 24, 2025 11:56
@AnnaSvalova AnnaSvalova requested review from AnnaSvalova and removed request for pmbethe09 November 6, 2025 14:30
},
scopeEverywhere)

checkFindings(t, "unused-variable", `
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you split this into 2 different test cases please? Current ":1: Variable "foo" is unused." is error prone, as it depends on the order. Also it's not clear without enough context what this :1 is.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, actually the first and second test case here seem to have nothing to do with each other 🤔 added comments to describe more detail on what is tested, and renamed the second inner param to "bar" since the name overlap had nothing to do with the test (if I understand this correctly). PTAL

@oreflow oreflow force-pushed the nested-defs branch 2 times, most recently from fdb700f to 05dd2f8 Compare November 7, 2025 10:55
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 merged commit e879524 into main Nov 7, 2025
5 checks passed
@oreflow oreflow deleted the nested-defs branch November 7, 2025 11:24
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.

2 participants