Skip to content

Overzealous unconditional_panic (denied) warning #78803

Closed
@Nemo157

Description

@Nemo157

To try and workaround the lack of const Result::unwrap on 1.47 I implemented a basic variant of it manually, but this fails to compile without suppressing the unconditional_panic warning:

#[derive(Copy, Clone)]
pub struct Foo;

impl Foo {
    pub const fn new_unwrap(success: bool) -> Self {
        let result = if success { Ok(Foo) } else { Err(()) };
        [][match result {
            Ok(foo) => return foo,
            Err(_) => 0,
        }]
    }
}

fn main() {}
error: this operation will panic at runtime
  --> uwu.rs:7:9
   |
7  | /         [][match result {
8  | |             Ok(foo) => return foo,
9  | |             Err(_) => 0,
10 | |         }]
   | |__________^ index out of bounds: the length is 0 but the index is 0
   |
   = note: `#[deny(unconditional_panic)]` on by default

error: aborting due to previous error

This code panicking is conditional on the input success value.

@rustbot modify labels: +A-const-fn, +A-lint

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-control-flowArea: Control flowA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions