dotnet / dotnet/roslyn

Unreachable code detection does not respect local scopes and throw statements

Open
#75,605 2 comments 0 reactions 0 assignees View on GitHub
Area-Compilers help wanted
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

## Version Used
VS 2022 Version 17.11.5
Roslyn 4.11.0 (5649376e0e5f)

Project info
C# 12.0
.NET 8.0

## Steps to Reproduce

Use the following code:

```csharp
public static int Add(int a, int b)
{
{
return a + b;
}

throw new InvalidOperationException();

{
throw new InvalidOperationException();
}

{
const int x = 523;
throw new InvalidOperationException();
}

{
throw new InvalidOperationException();
}
}
```

## Expected Behavior

In VS, the unreachable code will dim out showing that the dimmed out region is unreachable. In the above example, we expect all the code from the throw statement onwards to be dimmed out.

## Actual Behavior

The resulting dimming is very different and limited to the scope that starts with non-throw statements.
![Image](https://github.com/user-attachments/assets/87e0accf-733f-4dd5-ae1a-f2784308e3ac)

Contributor guide

Open the contributing guide

Research direction

Reproduce the behavior in Visual Studio 2022 with Roslyn 4.11.0 using the C# code in the issue. Start by tracing Roslyn's unreachable-code detection for local scopes and throw statements. Done means the editor dims all code after the first unreachable point, including subsequent scoped blocks.

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
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.