-
Notifications
You must be signed in to change notification settings - Fork 8
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
Rule Idea Board #3
Comments
I don't know what to call this, but @paperdave proposed it here |
"declaration can be accessed with a shorter path" / |
@paperdave |
fn addEntry(self: *Foo, id: u32, item: u32) !void {
// this creates a copy. It's dropped when the function returns
var foos = self.foo[id]; // should be &self.foo[id];
try foos.append(item); // has no lasting side effects b/c foos is dropped
} |
like var a: union(enum) { int: i32, float: f32 } = .X;
if (a == .float) {
std.debug.print("float: {}", .{a.float}); // legal
} else {
std.debug.print("int: {}", .{a.float}); // illegal!
}
switch (a) {
.int => std.debug.print("int: {}", .{a.float}), // illegal
} |
|
it's better to avoid this pattern entirely, if possible. these rules should encourage var a: union(enum) { int: i32, float: f32 } = .X;
if (a == .float) {
std.debug.print("float: {}", .{a.float}); // illegal
} else {
std.debug.print("int: {}", .{a.float}); // illegal
}
// rewrite into
switch (a) {
.float => |float| std.debug.print("float: {}", .{a.float}),
.int => |int| std.debug.print("int: {}", .{a.int}),
} |
I agree in general, but it's the same reason to have There is code when you're checking multiple conditions and only one union variant where a switch may be cumbersome: if (condition and a == .float) {
std.debug.print("{}", .{a.float});
}
// versus
if (condition) switch (a) {
.float => |v| {
std.debug.print("{}", .{v});
},
else => {},
} probably both |
Should we add a rule banning it entirely? |
Sure but I'd recommend allowing it with checks as the default. It comes up often enough imo. A better example that I should have shown earlier is try checking that two separate variables have specific tags. You'd need a nested double switch if you don't allow tags checking in a single if statement try rewriting this as a switch: if (a == .float and b == .int) {
} |
i think we should split these into separate issues. this thread is getting very long. one issue per lint rule, or for related ones they can share one. |
https://github.com/nektro/ziglint/issues has a bunch i havent implemented |
`homeless-try` looks for `try` used outside of error union-returning functions. Part of #3
Variant on unsafe-optional-unwrap: Ban Though reading your description of it, if you're talking about doing a typescript/c# style analysis where you're able to confirm a prior null check passed, then allowing |
I just opened a draft PR to implement a line length check rule (#232 ). I just realized I jumped the gun an didn't bring the idea here first (sorry). The idea is to just check if any line surpasses a given threshold. Currently the only thing left to implement is to let the user define this threshold through the config (for now 120 is hardcoded). Is this rule a good fit for the project? |
Would checking for empty |
Yes |
From time to time I stumble in some comments in the zig community regarding EDIT: to clarify the idea further, the rule would work like |
This is a mega-issue tracking the list of rules we want to implement. Rule requests are welcome.
no-undefined
- banundefined
in assignments and initializations// SAFETY:
comment is presentself.* = undefined
indeinit()
functionsunsafe-optional-unwrap
: bad optional unwrapping without a prior null checkuse-after-deinit
: ban references tofoo
afterfoo.deinit()
calls (or other free-like calls)no-const-reassign
: do not reassignconst
-declared variables. Yes the compiler checks this, no they don't check it for tree-shaken code.no-private-access
: ban external references to_private
container fields.The text was updated successfully, but these errors were encountered: