tauri-apps/tauri · error

Not a CheckMenuItem

Error message

Not a CheckMenuItem

What it means

`as_check_menuitem_unchecked` downcasts a `MenuItemKind` to `CheckMenuItem<R>` and panics with 'Not a CheckMenuItem' if the variant is a plain item, submenu, predefined, or icon item. The checked `as_check_menuitem` returns Option and is the recommended alternative.

Solutions

  1. Use the safe `as_check_menuitem()` and handle the None case.
  2. Match on `MenuItemKind::Check(c)` explicitly before reading/writing check state.
  3. Retain the concrete `CheckMenuItem<R>` handle in application state to avoid casting by kind later.
  4. If the unchecked cast stays, pre-check with `matches!(kind, MenuItemKind::Check(_))`.

Example fix

// before
let chk: &CheckMenuItem<R> = kind.as_check_menuitem_unchecked();
chk.set_checked(true)?;
// after
if let Some(chk) = kind.as_check_menuitem() {
  chk.set_checked(true)?;
}
Defensive patterns

Strategy: type-guard

Validate before calling

if matches!(kind, tauri::menu::MenuItemKind::Check(_)) {
  let chk = kind.as_check_menuitem_unchecked();
}

Type guard

fn as_check<R: tauri::Runtime>(kind: &tauri::menu::MenuItemKind<R>) -> Option<&tauri::menu::CheckMenuItem<R>> {
  kind.as_check_menuitem()
}

Prevention

When it happens

Trigger: Calling `as_check_menuitem_unchecked()` on a kind that is not `MenuItemKind::Check` — commonly when handling a menu event by id and casting the stored kind assuming it was created with `check(...)`.

Common situations: Toggling checkbox state from a menu event handler where the item was later replaced by a plain MenuItem or IconMenuItem; iterating heterogeneous menu items to read `is_checked()`; version refactors changing item constructors.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of tauri-apps/tauri@460ec35447 (2026-09-18). Data as JSON: /api/errors/4aa45bd1f1a28f9a. Report an issue: GitHub.

Appendix: source

Thrown at crates/tauri/src/menu/mod.rs:657

    match self {
      MenuItemKind::Predefined(i) => i,
      _ => panic!("Not a PredefinedMenuItem"),
    }
  }

  /// Casts this item to a [`CheckMenuItem`], and returns `None` if it wasn't.
  pub fn as_check_menuitem(&self) -> Option<&CheckMenuItem<R>> {
    match self {
      MenuItemKind::Check(i) => Some(i),
      _ => None,
    }
  }

  /// Casts this item to a [`CheckMenuItem`], and panics if it wasn't.
  pub fn as_check_menuitem_unchecked(&self) -> &CheckMenuItem<R> {
    match self {
      MenuItemKind::Check(i) => i,
      _ => panic!("Not a CheckMenuItem"),
    }
  }

  /// Casts this item to a [`IconMenuItem`], and returns `None` if it wasn't.
  pub fn as_icon_menuitem(&self) -> Option<&IconMenuItem<R>> {
    match self {
      MenuItemKind::Icon(i) => Some(i),
      _ => None,
    }
  }

  /// Casts this item to a [`IconMenuItem`], and panics if it wasn't.
  pub fn as_icon_menuitem_unchecked(&self) -> &IconMenuItem<R> {
    match self {
      MenuItemKind::Icon(i) => i,
      _ => panic!("Not an IconMenuItem"),
    }
  }

View on GitHub (pinned to 460ec35447)