Skip to content

unwrap considered harmful #12660

@james7132

Description

@james7132

What problem does this solve or what need does it fill?

Unwrap should generally be avoided. We already encourage contributors to avoid the use of Option::unwrap, yet crashes from panics are still one of the most common categories of issues we're seeing now.

What solution would you like?

  • Enable the clippy unwrap_used lint at a workspace level, and add allow-unwrap-in-tests = true to Clippy.toml.
  • Replace actual expected crashes with expect or unwrap_or_else with a panic where possible, otherwise properly gracefully handle the error.
  • Preferably avoid panicking in the renderer entirely, instead failing gracefully where possible by logging a warning and not rendering anything or using blatantly obvious errors (i.e. Valve's missing texture shader).

What alternative(s) have you considered?

Leaving it as is. Keep crashing from unwraps.

Status

  • bevy_animation
  • bevy_app
  • bevy_asset
  • bevy_core_pipeline
  • bevy_core_widgets
  • bevy_derive
  • bevy_diagnostic
  • bevy_ecs
  • bevy_ecs_macros
  • bevy_gilrs
  • bevy_gltf
  • bevy_gizmos
  • bevy_image
  • bevy_input_focus
  • bevy_light
  • bevy_log
  • bevy_macro_utils
  • bevy_math
  • bevy_mesh
  • bevy_pbr
  • bevy_post_process
  • bevy_reflect
  • bevy_reflect_derive
  • bevy_render_macros
  • bevy_scene
  • bevy_shader
  • bevy_sprite_render
  • bevy_text
  • bevy_transform
  • bevy_ui
  • bevy_ui_render
  • bevy_window
  • bevy_winit

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-Cross-CuttingImpacts the entire engineC-Code-QualityA section of code that is hard to understand or changeP-CrashA sudden unexpected crash

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions