Skip to content

Commit

Permalink
added used:: lint hint
Browse files Browse the repository at this point in the history
  • Loading branch information
thisismypassport committed Nov 28, 2024
1 parent 5bde644 commit 47d30c9
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 7 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,13 @@ end

If you have false positives in your cart due to globals being used via `_ENV`, you can disable this check just for globals via `--no-lint-unused-global`.

Another option is to use the `--lint: used::<var>` hint:
```lua
--lint: used::global_1, used::global_2
function global_1() end
global_2 = ""
```

## Duplicate variable lints

This lint alerts you when you declare a local with the same name as a local in a parent scope (even across functions).
Expand Down
4 changes: 4 additions & 0 deletions pico_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from pico_parse import VarKind, NodeType
from pico_parse import is_assign_target, is_function_target, is_any_assign_target, is_root_global_or_builtin_local

k_lint_used_prefix = "used::"

def lint_code(ctxt, root, lint_opts):
errors = []
builtin_globals = ctxt.builtins
Expand Down Expand Up @@ -33,6 +35,8 @@ def preprocess_tokens(token):
for value in comment.hintdata:
if "::" not in value:
custom_globals.add(value)
elif value.startswith(k_lint_used_prefix):
used_globals.add(value[len(k_lint_used_prefix):])

def preprocess_vars(node):
if node.type == NodeType.var:
Expand Down
2 changes: 1 addition & 1 deletion test_compare/bad-tab.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ test_input/bad.p8 (tab 2, line 19, col 10): Global 'maths' is only ever assigned
test_input/bad.p8 (tab 2, line 22, col 10): Global 'confess' is only ever assigned to, never used
test_input/bad.p8 (tab 2, line 22, col 18): Local 'sin' has the same name as a built-in global used elsewhere in the cart
test_input/bad.p8 (tab 2, line 22, col 23): Local 'print' has the same name as a built-in global used elsewhere in the cart
test_input/bad.p8 (tab 3, line 7, col 18): Local 'unused' isn't used
test_input/bad.p8 (tab 3, line 12, col 18): Local 'unused' isn't used
test_input/badinc.p8.png (tab 0, line 2, col 7): Local 'from_include' has the same name as a local declared in the same scope
test_input/badinc.p8.png (tab 0, line 2, col 7): Local 'from_include' isn't used
test_input/badinc.lua (tab E, line 1, col 7): Local 'inc_tab_e' isn't used
Expand Down
6 changes: 3 additions & 3 deletions test_compare/bad.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ test_input/bad.p8:49:10: Global 'maths' is only ever assigned to, never used
test_input/bad.p8:52:10: Global 'confess' is only ever assigned to, never used
test_input/bad.p8:52:18: Local 'sin' has the same name as a built-in global used elsewhere in the cart
test_input/bad.p8:52:23: Local 'print' has the same name as a built-in global used elsewhere in the cart
test_input/bad.p8:61:18: Local 'unused' isn't used
test_input/bad.p8:66:18: Local 'unused' isn't used
test_input/badinc.p8.png:2:7: Local 'from_include' has the same name as a local declared in the same scope
test_input/badinc.p8.png:2:7: Local 'from_include' isn't used
test_input/badinc.lua:29:7: Local 'inc_tab_e' isn't used
test_input/badinc.lua:19:7: Local 'inc_tab_9' isn't used
test_input/bad.p8:87:7: Local 'tab_b' isn't used
test_input/bad.p8:97:7: Local 'tab_still_f' isn't used
test_input/bad.p8:92:7: Local 'tab_b' isn't used
test_input/bad.p8:102:7: Local 'tab_still_f' isn't used
6 changes: 3 additions & 3 deletions test_compare/badcount.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
tokens: 185 2%
chars: 1167 2%
compressed: 493 3%
tokens: 190 2%
chars: 1272 2%
compressed: 538 3%
5 changes: 5 additions & 0 deletions test_input/bad.p8
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ function confess(sin, print)
sin(3); print(3)
end
-->8
-- exceptions
--lint: used::ok_unused, ok_unreferenced
function ok_unused()
return ok_unreferenced
end
-- bugs
function f3:foo()
return self
Expand Down

0 comments on commit 47d30c9

Please sign in to comment.