Skip to content

Implement zlint rule 'useless-error-return' #17

@rockorager

Description

@rockorager

Rule: useless-error-return

Detects functions that declare an error union return type (!T) but never actually return an error.

This makes the code less clear and forces callers to handle errors that will never occur (unnecessary try or catch at every call site).

Example

// Bad: returns !u32 but never errors
fn compute() !u32 {
    return 42;
}

// Good: just return the value type
fn compute() u32 {
    return 42;
}

Implementation notes

Complexity: requires control flow analysis

This needs to analyze all return paths in a function to determine whether any of them can produce an error. This includes:

  • Direct return error.Foo statements
  • try expressions (which propagate errors)
  • Calls to other error-returning functions without catch

This is non-trivial and requires control flow analysis that ziglint doesn't currently have.

Reference

Suggested-By: @mattrobenolt

Metadata

Metadata

Assignees

No one assigned

    Labels

    rule-requestRequest for a new lint rule

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions