Bugfix false positive for "unused" variable for nested `def`s (#1376)

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.

Co-authored-by: Tim Malmström <oreflow@google.com>
2 files changed
tree: c524254a0cac20438b797301dcfb89461bab2c51
  1. .bazelci/
  2. api_proto/
  3. build/
  4. build_proto/
  5. buildifier/
  6. buildozer/
  7. bzlenv/
  8. config/
  9. convertast/
  10. deps_proto/
  11. differ/
  12. edit/
  13. extra_actions_base_proto/
  14. file/
  15. generatetables/
  16. labels/
  17. lang/
  18. release/
  19. tables/
  20. testutils/
  21. unused_deps/
  22. warn/
  23. wspace/
  24. .bazelrc
  25. .bazelversion
  26. .gitignore
  27. .mailmap
  28. .pre-commit-config.yaml
  29. BUILD.bazel
  30. CODEOWNERS
  31. CONTRIBUTING.md
  32. CONTRIBUTORS
  33. go.mod
  34. go.sum
  35. launcher.js
  36. LICENSE
  37. MODULE.bazel
  38. README.md
  39. status.py
  40. update_generated.sh
  41. WARNINGS.md
  42. WORKSPACE
  43. WORKSPACE.bzlmod
README.md

Buildtools for bazel

This repository contains developer tools for working with Google's bazel buildtool.

Build status

Setup

See instructions in each tool's directory.