Skip to content

UnreachableAfterAttribute and unreachable code detecting #59

@knat

Description

@knat

The normal code:

void M()
{
    throw new Exception();
    Console.WriteLine();//warning CS0162: Unreachable code detected
}
int M2()
{
    if (...) return 42;
    throw new Exception();
}

But if a method definitely throws exceptions:

void Throw() 
{
    throw new Exception();
}
void M()
{
    Throw();
    Console.WriteLine();//no CS0162 warning
}
int M2()
{
    if (...) return 42;
    Throw();
}//error CS0161: not all code paths return a value

I suggest adding an UnreachableAfterAttribute, and the compiler is aware of it:

[UnreachableAfter]
void Throw() 
{
    throw new Exception();
}
void M()
{
    Throw();
    Console.WriteLine();//warning CS0162: Unreachable code detected
}
int M2()
{
    if (...) return 42;
    Throw();
}//OK

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions