tauri-apps/tauri · error

Not a MenuItem

Error message

Not a MenuItem

What it means

MenuItemKind is an enum over menu item kinds (MenuItem, Submenu, Predefined, Check, Icon). `as_menuitem_unchecked` performs an unchecked downcast to `MenuItem<R>` and panics with 'Not a MenuItem' when the variant is any other kind; the checked `as_menuitem` returns Option instead.

Solutions

  1. Use the safe `as_menuitem()` which returns `Option<&MenuItem<R>>` and handle the None case.
  2. Match on the `MenuItemKind` enum explicitly and handle each variant.
  3. Track item kinds at creation time (store the concrete `MenuItem<R>` handle) instead of casting later from the generic kind.
  4. If unchecked is intentional, guard with `matches!(kind, MenuItemKind::MenuItem(_))` before casting.

Example fix

// before
let item: &MenuItem<R> = kind.as_menuitem_unchecked(); // panics for other kinds
// after
if let Some(item) = kind.as_menuitem() {
  // use item
}
Defensive patterns

Strategy: type-guard

Validate before calling

if matches!(kind, tauri::menu::MenuItemKind::MenuItem(_)) {
  let item = kind.as_menuitem_unchecked();
}

Type guard

fn as_menu_item<R: tauri::Runtime>(kind: &tauri::menu::MenuItemKind<R>) -> Option<&tauri::menu::MenuItem<R>> {
  kind.as_menuitem()
}

Prevention

When it happens

Trigger: Calling `as_menuitem_unchecked()` on a `MenuItemKind` obtained from a submenu, predefined item, check item, or icon item — e.g. iterating `menu.items()` and blindly casting each entry.

Common situations: Code that walks a menu's items and assumes all are regular MenuItems; event handlers storing items as MenuItemKind and later casting without remembering which variant was created; refactors that changed an item's kind without updating the cast.

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/b75bc43eefba8aed. Report an issue: GitHub.

Appendix: source

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

      muda::MenuItemKind::Icon(i) => Self::Icon(IconMenuItem(Arc::new(IconMenuItemInner::new(
        app_handle, i,
      )))),
    }
  }

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

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

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

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

View on GitHub (pinned to 460ec35447)