Repository navigation
Add a check for unused private declarations #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
34292c2
b373081
289e00a
c3b8979
ce34f00
9579dd5
d149122
ca5bb38
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,63 @@ | ||||||
| const std = @import("std"); | ||||||
|
|
||||||
| const main = @import("main"); | ||||||
|
|
||||||
| pub fn check(ctx: main.Context) void { | ||||||
| const ast = ctx.ast orelse return; | ||||||
|
|
||||||
| var identifiers: std.StringHashMapUnmanaged(void) = .empty; | ||||||
| defer identifiers.deinit(main.allocator); | ||||||
|
|
||||||
| identifiers.put(main.allocator, "std", {}) catch @panic("OOM"); | ||||||
| identifiers.put(main.allocator, "main", {}) catch @panic("OOM"); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Without this, this also works.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, now that I ignore all imports anyways this is no longer needed |
||||||
|
|
||||||
| for (0..ast.nodes.len) |nodeIndex| { | ||||||
| const node: std.zig.Ast.Node.Index = @enumFromInt(nodeIndex); | ||||||
| switch (ast.nodeTag(node)) { | ||||||
| .identifier => { | ||||||
| _ = identifiers.getOrPut(main.allocator, ast.getNodeSource(node)) catch @panic("OOM"); | ||||||
| }, | ||||||
| .field_access => { | ||||||
| const token = ast.nodeData(node).node_and_token[1]; | ||||||
| const name = ast.tokenSlice(token); | ||||||
| _ = identifiers.getOrPut(main.allocator, name) catch @panic("OOM"); | ||||||
| }, | ||||||
| .enum_literal => { | ||||||
| const name = ast.tokenSlice(ast.nodeMainToken(node)); | ||||||
| _ = identifiers.getOrPut(main.allocator, name) catch @panic("OOM"); | ||||||
| }, | ||||||
| else => continue, | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| for (0..ast.nodes.len) |nodeIndex| { | ||||||
| const node: std.zig.Ast.Node.Index = @enumFromInt(nodeIndex); | ||||||
| switch (ast.nodeTag(node)) { | ||||||
| .simple_var_decl => { | ||||||
| if (main.rules.imports.isImport(ctx, ast, node, .ignoreAliasNameMismatch)) continue; | ||||||
| const varDec = ast.simpleVarDecl(node); | ||||||
| if (varDec.visib_token != null) continue; | ||||||
| const name = ast.tokenSlice(varDec.ast.mut_token + 1); | ||||||
| if (!identifiers.contains(name)) { | ||||||
| ctx.printError("Unused declaration", ast.tokenStart(varDec.firstToken())); | ||||||
| } | ||||||
| }, | ||||||
| .fn_proto, .fn_proto_simple, .fn_proto_one, .fn_proto_multi => |tag| { | ||||||
| var buffer: [1]std.zig.Ast.Node.Index = undefined; | ||||||
| const functionProto = switch (tag) { | ||||||
| .fn_proto => ast.fnProto(node), | ||||||
| .fn_proto_simple => ast.fnProtoSimple(&buffer, node), | ||||||
| .fn_proto_one => ast.fnProtoOne(&buffer, node), | ||||||
| .fn_proto_multi => ast.fnProtoMulti(node), | ||||||
| else => unreachable, | ||||||
| }; | ||||||
| if (functionProto.visib_token != null) continue; | ||||||
| const name = ast.tokenSlice(functionProto.name_token orelse continue); | ||||||
| if (!identifiers.contains(name)) { | ||||||
| ctx.printError("Unused declaration", ast.tokenStart(functionProto.firstToken())); | ||||||
| } | ||||||
| }, | ||||||
| else => continue, | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
Uh oh!
There was an error while loading. Please reload this page.