From 5a393820c6bea13a59133a140dfcaf0ab43bf6bf Mon Sep 17 00:00:00 2001 From: Swastik Baranwal Date: Mon, 2 Feb 2026 02:32:01 +0530 Subject: [PATCH] checker: preserve mut in other non-none if branch (fix #26491) (#26495) --- vlib/v/checker/checker.v | 2 +- vlib/v/tests/options/option_2_test.v | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index e3116b665..adef2c2f8 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -4913,7 +4913,7 @@ fn (mut c Checker) smartcast(mut expr ast.Expr, cur_type ast.Type, to_type_ ast. typ: cur_type pos: expr.pos is_used: true - is_mut: expr.is_mut + is_mut: expr.is_mut || is_mut is_inherited: is_inherited is_unwrapped: is_option_unwrap smartcasts: smartcasts diff --git a/vlib/v/tests/options/option_2_test.v b/vlib/v/tests/options/option_2_test.v index 16982cd9f..d94069d07 100644 --- a/vlib/v/tests/options/option_2_test.v +++ b/vlib/v/tests/options/option_2_test.v @@ -64,3 +64,14 @@ fn foo() ?string { fn test_opt_subexp_field() { assert foo()?.len == 2 } + +fn test_mut_opt_none_if_branch() { + mut x := ?int(none) + + if x != none { + x = 10 + } else { + x = 5 + } + assert x? == 5 +} -- 2.39.5