From 475463a7504c49e00cd579c24eb3e46ff9b01de0 Mon Sep 17 00:00:00 2001 From: Techatrix Date: Wed, 11 Dec 2024 19:40:47 +0100 Subject: [PATCH] ignore ast-check errors on invalid unused capture --- src/features/code_actions.zig | 14 +++++++++----- tests/lsp_features/code_actions.zig | 2 +- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/features/code_actions.zig b/src/features/code_actions.zig index 888695fa7..18c4431ca 100644 --- a/src/features/code_actions.zig +++ b/src/features/code_actions.zig @@ -436,6 +436,15 @@ fn handleUnusedCapture( const source = tree.source; + const identifier_token = offsets.sourceIndexToTokenIndex(tree, loc.start); + if (token_tags[identifier_token] != .identifier) return; + + const identifier_name = offsets.locToSlice(source, loc); + + // Zig can report incorrect "unused capture" errors + // https://github.com/ziglang/zig/pull/22209 + if (std.mem.eql(u8, identifier_name, "_")) return; + try actions.ensureUnusedCapacity(builder.arena, 3); if (builder.wantKind(.quickfix)) { @@ -464,11 +473,6 @@ fn handleUnusedCapture( if (!builder.wantKind(.@"source.fixAll")) return; - const identifier_token = offsets.sourceIndexToTokenIndex(tree, loc.start); - if (token_tags[identifier_token] != .identifier) return; - - const identifier_name = offsets.locToSlice(source, loc); - const capture_end: Ast.TokenIndex = @intCast(std.mem.indexOfScalarPos(Token.Tag, token_tags, identifier_token, .pipe) orelse return); var lbrace_token = capture_end + 1; diff --git a/tests/lsp_features/code_actions.zig b/tests/lsp_features/code_actions.zig index c4587101f..4e27ea28c 100644 --- a/tests/lsp_features/code_actions.zig +++ b/tests/lsp_features/code_actions.zig @@ -834,7 +834,7 @@ test "convert string literal to multiline - invalid" { } fn testAutofix(before: []const u8, after: []const u8) !void { - try testDiagnostic(before, after, .{ .filter_kind = .@"source.fixAll", .want_zir = true }); // diagnostics come from our AstGen fork + try testDiagnostic(before, after, .{ .filter_kind = .@"source.fixAll", .want_zir = true }); // diagnostics come from std.zig.AstGen try testDiagnostic(before, after, .{ .filter_kind = .@"source.fixAll", .want_zir = false }); // diagnostics come from calling zig ast-check }