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

rule: check for switch over *Enum or *TaggedUnion #160

Open
DonIsaac opened this issue Dec 16, 2024 · 0 comments
Open

rule: check for switch over *Enum or *TaggedUnion #160

DonIsaac opened this issue Dec 16, 2024 · 0 comments
Labels
A-linter Area - linter and lint rules C-rule Category - Lint rule suggestion

Comments

@DonIsaac
Copy link
Owner

Consider the following code:

const Foo = union(enum) {
  a: i32,
  b: []const u8,

  fn deinit(self: *Foo, alloc: Allocator) void {
    // WRONG! switching over pointer, not enum
    // should be `switch(self.*)`
    switch(self) {
      .a => {}, // noop
      .b => alloc.free(self.b),
    }
  }
};

The zig compiler provides this error message:

example.zig:8:5: error: else prong required when switching on type '*src.example.Foo'
        switch (self) {

This message isn't great. Ideally it would have a note saying "did you forget to dereference this pointer?"

We should add a rule that checks for this.

@DonIsaac DonIsaac added A-linter Area - linter and lint rules C-rule Category - Lint rule suggestion labels Dec 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-linter Area - linter and lint rules C-rule Category - Lint rule suggestion
Projects
None yet
Development

No branches or pull requests

1 participant