Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ControlFlowAnalysis.EndPointIsReachable appears to be thrown off by a local function #42447

Open
louis-z opened this issue Mar 15, 2020 · 3 comments

Comments

@louis-z
Copy link
Contributor

louis-z commented Mar 15, 2020

Version Used:
Microsoft.CodeAnalysis.CSharp.Workspaces 2.10.0
(I know it's an old version)

Steps to Reproduce:
Analyze control flow on the following if block:

if (value < 0)
{
    Increment(ref value);
    return;
    void Increment(ref int val) => val++;
    value++;
}

Expected Behavior:
EndPointIsReachable == false

According to the EndPointIsReachable documentation:

Return true if and only if the end of the last statement in a region is reachable or the region contains no statements.

In this case, the last statement (value++;) is unreachable.

Actual Behavior:
EndPointIsReachable == true

Here's the complete code I used to test this behavior:

using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Xunit;

namespace MyCodeAnalysisTests
{
    public sealed class EndPointIsReachableTests
    {
        [Fact]
        public void Test_IfReturns_EndPointIsUnreachable()
        {
            var code = @"
class TestClass
{
    void TestMethod()
    {
        var value = -1;
        if (value < 0)
        {
            Increment(ref value);
            return;
            void Increment(ref int val) => val++;
            value++;
        }
        else
        {
            value--;
        }
    }
}";
            var tree = CSharpSyntaxTree.ParseText(code);

            var compilation = CSharpCompilation.Create("MyCompilation", syntaxTrees: new[] { tree });
            var model = compilation.GetSemanticModel(tree);

            var ifStatement = tree.GetRoot().DescendantNodes().OfType<IfStatementSyntax>().Single();
            var result = model.AnalyzeControlFlow(ifStatement.Statement);

            Assert.True(result.Succeeded);
            Assert.False(result.EndPointIsReachable);
        }
    }
}
@jinujoseph jinujoseph added this to the Backlog milestone Mar 16, 2020
@mavasani mavasani removed their assignment Mar 17, 2020
@mavasani mavasani removed this from the Backlog milestone Mar 17, 2020
@mavasani
Copy link
Contributor

Moving to the compiler team - this is a compiler API

@Rekkonnect
Copy link
Contributor

In VS 17.11.5, using the sample code above I'm getting it dimmed and the compiler emits a warning about the code being unreachable at that location, so this must be fixed already I assume

@CyrusNajmabadi
Copy link
Member

@Rekkonnect What does hte api produce though?

louis-z added a commit to louis-z/Meziantou.Analyzer that referenced this issue Oct 23, 2024
Several years ago, an issue was reported (dotnet/roslyn#42447) and bypassed in the AvoidUsingRedundantElseAnalyzer code. It appears that issue has been resolved (dotnet/roslyn#42447 (comment)), which now allows us to simplify the code a bit.
meziantou pushed a commit to meziantou/Meziantou.Analyzer that referenced this issue Oct 24, 2024
* Simplify AvoidUsingRedundantElseAnalyzer

Several years ago, an issue was reported (dotnet/roslyn#42447) and bypassed in the AvoidUsingRedundantElseAnalyzer code. It appears that issue has been resolved (dotnet/roslyn#42447 (comment)), which now allows us to simplify the code a bit.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants