Blocks of code that can never be reached

I have a .NET project (C #) where in my code there is such a function:

public void myFunction(string myStr) { myStr = "This is not an empty string"; if(String.IsNullOrEmpty(myStr)) { DoSomething(); } } 

I want to analyze my application to see if there are blocks of code that will never be reached. How can i do this?

+4
source share
3 answers

Use ReSharper to make your code more efficient. Check for inaccessible areas of code in one of them.

+1
source

To do this, in the general case, you will need a symbolic analysis of values ​​across all control paths and a Boolean symbolic simplification to determine if the condition is true. For instance:

 void bar(...a) { ... x=2*a; if (...) x=17; foo(x) ... } void foo(int x) { if (x<a && !x>5) { // dead code if called from bar ... ... 

To know that a line of dead code is really dead, you need to find all the calls to foo and verify that each of them causes this condition. So you need a global call graph, for which you need a global analysis of function pointers, for which global and, therefore, local points are needed - analysis ...

I do not know any ready-made tools that do this.

One could build with some effort using a program conversion system. Our DMS Software Reengineering Toolkit contains all these machines for C. Although all this equipment is not yet available for C #, it is implemented in a largely agnostic way, so getting there for C # is a sweat, but it is not impractical.

+1
source

You can use tools like ReSharper that can perform code quality analysis during development. it gives you warnings such as "heuristically unreachable code"

-1
source

Source: https://habr.com/ru/post/1414725/


All Articles