diff --git a/README.md b/README.md index 13d10de..2677dd4 100644 --- a/README.md +++ b/README.md @@ -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::` 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). diff --git a/pico_lint.py b/pico_lint.py index 4056c01..dff8777 100644 --- a/pico_lint.py +++ b/pico_lint.py @@ -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 @@ -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: diff --git a/test_compare/bad-tab.txt b/test_compare/bad-tab.txt index 7fdfde1..874119f 100644 --- a/test_compare/bad-tab.txt +++ b/test_compare/bad-tab.txt @@ -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 diff --git a/test_compare/bad.txt b/test_compare/bad.txt index 3eaa6dd..7b6f343 100644 --- a/test_compare/bad.txt +++ b/test_compare/bad.txt @@ -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 diff --git a/test_compare/badcount.txt b/test_compare/badcount.txt index 8198a51..2a19300 100644 --- a/test_compare/badcount.txt +++ b/test_compare/badcount.txt @@ -1,3 +1,3 @@ -tokens: 185 2% -chars: 1167 2% -compressed: 493 3% +tokens: 190 2% +chars: 1272 2% +compressed: 538 3% diff --git a/test_input/bad.p8 b/test_input/bad.p8 index d2453f0..b4f5075 100644 --- a/test_input/bad.p8 +++ b/test_input/bad.p8 @@ -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