From 63d84855a5a3f83b6b22f95e5502b683e4604609 Mon Sep 17 00:00:00 2001 From: InKryption Date: Fri, 19 Aug 2022 13:23:54 +0200 Subject: [PATCH] Improve unused variable report accuracy --- src/Server.zig | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/Server.zig b/src/Server.zig index c0564ae..b9b53dd 100644 --- a/src/Server.zig +++ b/src/Server.zig @@ -250,7 +250,7 @@ fn publishDiagnostics(server: *Server, writer: anytype, handle: DocumentStore.Ha while (decl_iterator.next()) |decl| { var identifier_count: usize = 0; - var name_token_index = switch (decl.value_ptr.*) { + const name_token_index = switch (decl.value_ptr.*) { .ast_node => |an| s: { const an_tag = tree.nodes.items(.tag)[an]; switch (an_tag) { @@ -270,8 +270,27 @@ fn publishDiagnostics(server: *Server, writer: anytype, handle: DocumentStore.Ha const pit_start = tree.firstToken(scope_data); const pit_end = ast.lastToken(tree, scope_data); - for (tree.tokens.items(.tag)[pit_start..pit_end]) |tag, index| { - if (tag == .identifier and std.mem.eql(u8, tree.tokenSlice(pit_start + @intCast(u32, index)), tree.tokenSlice(name_token_index))) identifier_count += 1; + const tags = tree.tokens.items(.tag)[pit_start..pit_end]; + for (tags) |tag, index| { + if (tag != .identifier) continue; + if (!std.mem.eql(u8, tree.tokenSlice(pit_start + @intCast(u32, index)), tree.tokenSlice(name_token_index))) continue; + if (index -| 1 > 0 and tags[index - 1] == .period) continue; + if (index +| 2 < tags.len and tags[index + 1] == .colon) switch (tags[index + 2]) { + .l_brace, + .keyword_inline, + .keyword_while, + .keyword_for, + .keyword_switch, + => continue, + else => {}, + }; + if (index -| 2 > 0 and tags[index - 1] == .colon) switch (tags[index - 2]) { + .keyword_break, + .keyword_continue, + => continue, + else => {}, + }; + identifier_count += 1; } if (identifier_count <= 1)