Skip to content

Commit

Permalink
add autofix for local variable is never mutated error
Browse files Browse the repository at this point in the history
  • Loading branch information
Techatrix authored and SuperAuguste committed Nov 21, 2023
1 parent 6b05cb6 commit 4462c16
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
26 changes: 26 additions & 0 deletions src/features/code_actions.zig
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub const Builder = struct {
// autofix: comment out code
// fix: remove code
},
.var_never_mutated => try handleVariableNeverMutated(builder, actions, loc),
}
}

Expand Down Expand Up @@ -289,6 +290,28 @@ fn handlePointlessDiscard(builder: *Builder, actions: *std.ArrayListUnmanaged(ty
});
}

fn handleVariableNeverMutated(builder: *Builder, actions: *std.ArrayListUnmanaged(types.CodeAction), loc: offsets.Loc) !void {
const source = builder.handle.tree.source;

const var_keyword_end = 1 + (std.mem.lastIndexOfNone(u8, source[0..loc.start], &std.ascii.whitespace) orelse return);

const var_keyword_loc: offsets.Loc = .{
.start = var_keyword_end -| "var".len,
.end = var_keyword_end,
};

if (!std.mem.eql(u8, offsets.locToSlice(source, var_keyword_loc), "var")) return;

try actions.append(builder.arena, .{
.title = "use 'const'",
.kind = .@"source.fixAll",
.isPreferred = true,
.edit = try builder.createWorkspaceEdit(&.{
builder.createTextEditLoc(var_keyword_loc, "const"),
}),
});
}

fn detectIndentation(source: []const u8) []const u8 {
// Essentially I'm looking for the first indentation in the file.
var i: usize = 0;
Expand Down Expand Up @@ -431,6 +454,7 @@ const DiagnosticKind = union(enum) {
non_camelcase_fn,
undeclared_identifier,
unreachable_code,
var_never_mutated,

const IdCat = enum {
@"function parameter",
Expand Down Expand Up @@ -464,6 +488,8 @@ const DiagnosticKind = union(enum) {
return .non_camelcase_fn;
} else if (std.mem.startsWith(u8, msg, "use of undeclared identifier")) {
return .undeclared_identifier;
} else if (std.mem.eql(u8, msg, "local variable is never mutated")) {
return .var_never_mutated;
}
return null;
}
Expand Down
20 changes: 18 additions & 2 deletions tests/lsp_features/code_actions.zig
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ test "code actions - remove pointless discard" {
try testAutofix(
\\fn foo(a: u32) u32 {
\\ _ = a;
\\ var b: ?u32 = a;
\\ const b: ?u32 = a;
\\ _ = b;
\\ const c = b;
\\ _ = c;
Expand All @@ -119,7 +119,7 @@ test "code actions - remove pointless discard" {
\\
,
\\fn foo(a: u32) u32 {
\\ var b: ?u32 = a;
\\ const b: ?u32 = a;
\\ const c = b;
\\ if (c) |d| {
\\ return d;
Expand All @@ -130,6 +130,22 @@ test "code actions - remove pointless discard" {
);
}

test "code actions - correct unnecessary uses of var" {
try testAutofix(
\\test {
\\ var foo = 5;
\\ _ = foo;
\\}
\\
,
\\test {
\\ const foo = 5;
\\ _ = foo;
\\}
\\
);
}

/// does not check for correct formatting
fn testAutofix(before: []const u8, after: []const u8) !void {
var ctx = try Context.init();
Expand Down

0 comments on commit 4462c16

Please sign in to comment.