Skip to content

Allow .unwrap_or_else(|| { panic!(...) }) under limited circumstances #188

@leynos

Description

@leynos

Permit .unwrap_or_else(|| { panic!(...) }) only when both:

  1. it is being used in a test, and
  2. it is being used to interpolate a value into the panic message

Re-use the test detection from the no_expect_outside_of_tests lint.

Rationale

In tests, .unwrap_or_else(|| panic!(...)) can express something that expect(...) cannot: it allows the panic message to include the actual runtime value or error payload that caused the failure. That makes test failures markedly more diagnosable, especially when the value under inspection carries important structured context.

Outside tests, this pattern is still undesirable because it reintroduces effectively ad hoc expect-style panics through a closure, weakens consistency, and creates an easy escape hatch around the lint’s intent. Restricting it to tests preserves useful failure diagnostics where they matter most, while keeping production code held to the stricter rule.

The interpolation requirement is important because otherwise this exception would become a blanket allowance for expect(...) in disguise. The exemption should exist only for cases where the closure materially improves the panic output by incorporating the underlying value.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions