dotnet / dotnet/roslyn

Unexpected "unreachable code" warning for `const` used by a local function

Open
#77,632 3 comments 2 reactions 0 assignees View on GitHub
Area-Compilers Bug
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

```cs
f();
return;

const string s = "a"; // warning CS0162: Unreachable code detected

void f()
{
System.Console.Write(s);
}
```

The program outputs "a", i.e., the `const` has been actually used, so the "unreachable code" warning is incorrect.

Contributor guide

Open the contributing guide

Research direction

Start by running the minimal C# reproduction from the issue and confirming warning CS0162 while the program outputs "a". Trace the compiler's unreachable-code analysis for the local function and const, and consider the issue done when this valid usage no longer produces the incorrect warning without regressing genuine unreachable-code diagnostics.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.