Skip to content
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

feat(linter/no-return-try): add auto fixer #196

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/linter/rules/no_return_try.zig
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,30 @@

const std = @import("std");
const util = @import("util");
const _source = @import("../../source.zig");

Check warning on line 42 in src/linter/rules/no_return_try.zig

View workflow job for this annotation

GitHub Actions / Lint Changed

unused-decls

variable '_source' is declared but never used.
const semantic = @import("../../semantic.zig");
const _rule = @import("../rule.zig");
const _span = @import("../../span.zig");

const Ast = std.zig.Ast;
const Node = Ast.Node;
const Symbol = semantic.Symbol;

Check warning on line 49 in src/linter/rules/no_return_try.zig

View workflow job for this annotation

GitHub Actions / Lint Changed

unused-decls

variable 'Symbol' is declared but never used.
const Loc = std.zig.Loc;

Check warning on line 50 in src/linter/rules/no_return_try.zig

View workflow job for this annotation

GitHub Actions / Lint Changed

unused-decls

variable 'Loc' is declared but never used.
const LinterContext = @import("../lint_context.zig");
const LabeledSpan = _span.LabeledSpan;
const Rule = _rule.Rule;
const NodeWrapper = _rule.NodeWrapper;
const Error = @import("../../Error.zig");
const Cow = util.Cow(false);
const Fix = @import("../fix.zig").Fix;

// Rule metadata
const NoReturnTry = @This();
pub const meta: Rule.Meta = .{
.name = "no-return-try",
.category = .pedantic,
.default = .warning,
.fix = Fix.Meta.fix(),
};

fn returnTryDiagnostic(ctx: *LinterContext, return_start: u32, try_start: u32) Error {
Expand All @@ -82,8 +84,18 @@

const starts = ast.tokens.items(.start);
const return_start = starts[node.main_token];
const try_start = starts[ast.nodes.items(.main_token)[returned_id]];
ctx.report(returnTryDiagnostic(ctx, return_start, try_start));
const try_token = ast.nodes.items(.main_token)[returned_id];
const try_start: Ast.TokenIndex = starts[try_token];
ctx.reportWithFix(
FixCtx{ .try_token = try_token },
returnTryDiagnostic(ctx, return_start, try_start),
&deleteTry,
);
}

const FixCtx = struct { try_token: Ast.TokenIndex };
fn deleteTry(ctx: FixCtx, builder: Fix.Builder) anyerror!Fix {
return builder.delete(builder.spanCovering(.token, ctx.try_token));

Check warning on line 98 in src/linter/rules/no_return_try.zig

View check run for this annotation

Codecov / codecov/patch

src/linter/rules/no_return_try.zig#L97-L98

Added lines #L97 - L98 were not covered by tests
}

pub fn rule(self: *NoReturnTry) Rule {
Expand Down
Loading