Bug 21805 - Inaccurate CS0165 "Use of unassigned local variable" when using switch statement with enum constant
Summary: Inaccurate CS0165 "Use of unassigned local variable" when using switch statem...
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: unspecified
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Marek Safar
Depends on:
Reported: 2014-08-04 20:02 UTC by Brendan Zagaeski (Xamarin Team, assistant)
Modified: 2014-09-29 22:36 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Brendan Zagaeski (Xamarin Team, assistant) 2014-08-04 20:02:21 UTC
Inaccurate CS0165 "Use of unassigned local variable" when using switch statement with enum constant.

This might be related to the fix for https://bugzilla.xamarin.com/show_bug.cgi?id=11526. The test case on that bug works correctly on the current `mcs`.

## Steps to reproduce

Compile the following program using `mcs`:

using System;

public enum Foo { One, Two };

class MainClass
    private const Foo foo = Foo.Two;

    public static void Main(string[] args)
        object obj;

        switch (foo)
            case Foo.One:
                obj = new object();
            case Foo.Two:
                obj = new object();


## Result with `mcs`
> $ mcs Program.cs
> Program.cs(15,13): warning CS0162: Unreachable code detected
> Program.cs(23,27): error CS0165: Use of unassigned local variable `obj'
> Compilation failed: 1 error(s), 1 warnings

## Result with `csc`

> C:\>csc Program.cs
> Microsoft (R) Visual C# Compiler version 12.0.30501.0
> for C# 5
> Copyright (C) Microsoft Corporation. All rights reserved.
> Program.cs(16,17): warning CS0162: Unreachable code detected

## Version information

Tested with Mono 3.6.0 (no/f540f8a) and Mono 3.6.1 (master/2111224)
Comment 1 Marek Safar 2014-08-06 03:53:29 UTC
Fixed in master