Skip to content

Commit 5852b88

Browse files
authored
fix(linter/case-convention): only fns returning 'type' are pascal case (#310)
1 parent 0c30426 commit 5852b88

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/linter/rules/case_convention.zig

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ fn functionNameDiagnostic(ctx: *LinterContext, fn_name: []const u8, case: CaseTy
109109
.{ctx.spanT(name)},
110110
);
111111
}
112+
112113
fn genericsArePascaleCase(ctx: *LinterContext, fn_name: []const u8, name: Ast.TokenIndex) Error {
113114
var d = ctx.diagnosticf(
114115
"Function '{s}' returns a type, but does not use PascalCase",
@@ -164,7 +165,8 @@ fn fnReturnsType(ctx: *LinterContext, fn_proto: *const Ast.full.FnProto) bool {
164165
@branchHint(.cold);
165166
return false;
166167
}
167-
return std.ascii.isUpper(return_type[0]) or std.mem.eql(u8, return_type, "type");
168+
169+
return std.mem.eql(u8, return_type, "type");
168170
}
169171

170172
// Used by the Linter to register the rule so it can be run.
@@ -180,9 +182,23 @@ test CaseConvention {
180182
var runner = RuleTester.init(t.allocator, case_convention.rule());
181183
defer runner.deinit();
182184

183-
const pass = &[_][:0]const u8{ "fn alllowercasefunctionsarealwaysgreen() void {}", "fn thisFunctionIsInCamelCase() void {}", "fn Generic(T: type) T { return T{}; }", "fn FooBar() type { return u32; }" };
185+
const pass = &[_][:0]const u8{
186+
"fn alllowercasefunctionsarealwaysgreen() void {}",
187+
"fn thisFunctionIsInCamelCase() void {}",
188+
"fn Generic(T: type) type { return *T; }",
189+
"fn FooBar() type { return u32; }",
190+
};
184191

185-
const fail = &[_][:0]const u8{ "fn ThisFunctionIsInPascalCase() void {}", "fn @\"this-one-is-in-kebab-case\"() void {}", "fn this_one_is_in_snake_case() void {}", "fn @\"This-is-both-Pascal-and-Kebab-kinda\"() void {}", "fn This_is_both_snake_case_and_pascal_kinda() void {}", "fn This_is_both_snake_case_and_pascal_kinda(a: u32, b: u32, c: u32, d: u32) void {}", "fn fooBar() type { return u32; }" };
192+
const fail = &[_][:0]const u8{
193+
"fn ThisFunctionIsInPascalCase() void {}",
194+
"fn @\"this-one-is-in-kebab-case\"() void {}",
195+
"fn this_one_is_in_snake_case() void {}",
196+
"fn @\"This-is-both-Pascal-and-Kebab-kinda\"() void {}",
197+
"fn This_is_both_snake_case_and_pascal_kinda() void {}",
198+
"fn This_is_both_snake_case_and_pascal_kinda(a: u32, b: u32, c: u32, d: u32) void {}",
199+
"fn fooBar() type { return u32; }",
200+
"fn NotGeneric(T: type) T { return T{}; }",
201+
};
186202

187203
try runner
188204
.withPass(pass)

src/linter/rules/snapshots/case-convention.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,9 @@
4141
╰────
4242
help: By convention, Zig uses PascalCase for structs, generics, and all other type variables.
4343

44+
𝙭 case-convention: Function NotGeneric name is in PascalCase. It should be camelCase
45+
╭─[case-convention.zig:1:4]
46+
1fn NotGeneric(T: type) T { return T{}; }
47+
· ──────────
48+
╰────
49+

0 commit comments

Comments
 (0)