Skip to content

rule: always-set-diagnostic-before-error #332

@MichaelBelousov

Description

@MichaelBelousov

Description

Make sure that code following (a slightly modified version of) the diagnostic pattern set their diagnostic to a necessary data right before returning any errors.

I think that the diagnostic pattern is too flexible considering the diagnostic could be an argument, a field of an options argument, might not be a tagged union, may require separate initialization for some fields, etc. But I wanted to discuss, since zlint might be a good engine to prevent these bugs for me. I am also not against contributing the necessary stuff myself once I have some time for it.

One (possibly problematic) way to handle it would be a magic comment to indicate a binding that should always be assigned to before returning an error.

Examples

Examples of incorrect code for this rule

fn parse(src: []const u8, diag: ?*Diag) !Data {
  if (startsWith(u8, "hello", src)) {
    return error.NeverSayHello;
  }
  //...
  return Data{};
}

Examples of correct code for this rule

fn parse(src: []const u8, in_diag: ?*Diag) !Data {
  var dummy_diag: Diag = undefined;
  // zlint:diagnostic diag
  const diag = in_diag orelse dummy_diag;

  if (startsWith(u8, "hello", src)) {
    diag.* = .{ .NeverSayHello = relevant_data };
    return error.NeverSayHello;
  }
  //...
  return Data{};
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-linterArea - linter and lint rulesC-ruleCategory - Lint rule suggestion

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions