Skip to content

Add a lint to suggest ok_or_else() for "expensive" values of ok_or() argument #15677

@djc

Description

@djc

What it does

It would be nice to have the inverse of unnecessary_lazy_evaluations where clippy would suggest these translations:

  • unwrap_or to unwrap_or_else
  • and to and_then
  • or to or_else
  • get_or_insert to get_or_insert_with
  • ok_or to ok_or_else
  • then_some to then

if the argument to the original is "expensive" (non-const, function call other than enum tuple variant, etc).

Advantage

In rustls I recently wrote a PR that changed ok_or(Error::Foo) to ok_or(cx.send_fatal_alert(Error::Foo)), which broke a lot of stuff in somewhat non-obvious ways because it sent alerts eagerly rather than only in the actual error case.

Example

On seeing ok_or(cx.send_alert(Error::Foo)), suggest using ok_or_else(..) instead.

Comparison with existing lints

Similar to unnecessary_lazy_evaluations, but the other way around.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions