Nice to have style hint and bug fix

This commit is contained in:
Auguste Rame 2022-07-09 00:00:27 +02:00 committed by Auguste Rame
parent 5838a34101
commit bb89c3518b

View File

@ -271,40 +271,65 @@ fn publishDiagnostics(arena: *std.heap.ArenaAllocator, handle: DocumentStore.Han
.severity = .Error, .severity = .Error,
.code = "unused_variable", .code = "unused_variable",
.source = "zls", .source = "zls",
.message = "Unused variable! Either remove the variable or use '_ = ' on the variable to bypass this error.", .message = "Unused variable; either remove the variable or use '_ = ' on the variable to bypass this error",
}); });
} }
} }
} }
// TODO: style warnings for types, values and declarations below root scope if (config.warn_style) {
if (tree.errors.len == 0) { var node: u32 = 0;
for (tree.rootDecls()) |decl_idx| { while (node < tree.nodes.len) : (node += 1) {
const decl = tree.nodes.items(.tag)[decl_idx]; if (ast.isBuiltinCall(tree, node)) {
switch (decl) { const builtin_token = tree.nodes.items(.main_token)[node];
// .simple_var_decl => { const call_name = tree.tokenSlice(builtin_token);
// // var d = ast.varDecl(tree, decl_idx).?;
// const loc = tree.tokenLocation(0, decl_idx);
// try diagnostics.append(.{ if (!std.mem.eql(u8, call_name, "@import")) continue;
// .range = astLocationToRange(loc),
// .severity = .Information, const node_data = tree.nodes.items(.data)[node];
// .code = "unused_variable", const params = switch (tree.nodes.items(.tag)[node]) {
// .source = "zls", .builtin_call, .builtin_call_comma => tree.extra_data[node_data.lhs..node_data.rhs],
// .message = "Unused variable", .builtin_call_two, .builtin_call_two_comma => if (node_data.lhs == 0)
// }); &[_]Ast.Node.Index{}
// }, else if (node_data.rhs == 0)
.fn_proto, &[_]Ast.Node.Index{node_data.lhs}
.fn_proto_multi, else
.fn_proto_one, &[_]Ast.Node.Index{ node_data.lhs, node_data.rhs },
.fn_proto_simple, else => unreachable,
.fn_decl, };
=> blk: {
var buf: [1]Ast.Node.Index = undefined; if (params.len != 1) continue;
const func = ast.fnProto(tree, decl_idx, &buf).?;
if (func.extern_export_inline_token != null) break :blk; const import_str_token = tree.nodes.items(.main_token)[params[0]];
const import_str = tree.tokenSlice(import_str_token);
if (std.mem.startsWith(u8, import_str, "\".")) {
try diagnostics.append(.{
.range = astLocationToRange(tree.tokenLocation(0, import_str_token)),
.severity = .Hint,
.code = "useless_dot",
.source = "zls",
.message = "A . or ./ is not needed in imports",
});
}
}
}
// TODO: style warnings for types, values and declarations below root scope
if (tree.errors.len == 0) {
for (tree.rootDecls()) |decl_idx| {
const decl = tree.nodes.items(.tag)[decl_idx];
switch (decl) {
.fn_proto,
.fn_proto_multi,
.fn_proto_one,
.fn_proto_simple,
.fn_decl,
=> blk: {
var buf: [1]Ast.Node.Index = undefined;
const func = ast.fnProto(tree, decl_idx, &buf).?;
if (func.extern_export_inline_token != null) break :blk;
if (config.warn_style) {
if (func.name_token) |name_token| { if (func.name_token) |name_token| {
const loc = tree.tokenLocation(0, name_token); const loc = tree.tokenLocation(0, name_token);
@ -314,24 +339,24 @@ fn publishDiagnostics(arena: *std.heap.ArenaAllocator, handle: DocumentStore.Han
if (!is_type_function and !analysis.isCamelCase(func_name)) { if (!is_type_function and !analysis.isCamelCase(func_name)) {
try diagnostics.append(.{ try diagnostics.append(.{
.range = astLocationToRange(loc), .range = astLocationToRange(loc),
.severity = .Information, .severity = .Hint,
.code = "bad-style", .code = "bad_style",
.source = "zls", .source = "zls",
.message = "Functions should be camelCase", .message = "Functions should be camelCase",
}); });
} else if (is_type_function and !analysis.isPascalCase(func_name)) { } else if (is_type_function and !analysis.isPascalCase(func_name)) {
try diagnostics.append(.{ try diagnostics.append(.{
.range = astLocationToRange(loc), .range = astLocationToRange(loc),
.severity = .Information, .severity = .Hint,
.code = "bad-style", .code = "bad_style",
.source = "zls", .source = "zls",
.message = "Type functions should be PascalCase", .message = "Type functions should be PascalCase",
}); });
} }
} }
} },
}, else => {},
else => {}, }
} }
} }
} }
@ -769,7 +794,14 @@ fn hoverSymbol(id: types.RequestId, arena: *std.heap.ArenaAllocator, decl_handle
.fn_proto_simple, .fn_proto_simple,
.fn_decl, .fn_decl,
=> "fn", // TODO:(?) Add more info? => "fn", // TODO:(?) Add more info?
else => tree.getNodeSource(p), .array_type,
.array_type_sentinel,
.ptr_type,
.ptr_type_aligned,
.ptr_type_bit_range,
.ptr_type_sentinel,
=> tree.getNodeSource(p),
else => "unknown", // TODO: Implement more "other" type expressions; better safe than sorry
}, },
else => "unknown", else => "unknown",
} }