﻿# V3522\. AUTOSAR\. Unreachable code should not be present in the project\.

This diagnostic rule is based on the software development guidelines developed by [AUTOSAR](https://www.autosar.org/) \(AUTomotive Open System ARchitecture\)\.

Unreachable code may be a sign of a programmer's error and complicates support of code\.

For purposes of optimization, the compiler may remove unreachable code\. Unreachable code, not removed by the compiler can waste resources\. For example, it can increase the size of the binary file or cause unnecessary instruction caching\.

Let's consider the first example:

```cpp
void Error()
{
  ....
  exit(1);
}

FILE* OpenFile(const char *filename)
{
  FILE *f = fopen(filename, "w");
  if (f == nullptr)
  {
    Error();
    printf("No such file: %s", filename);
  }
  return f;
}
```

The 'printf\(\.\.\.\.\)' function will never print an error message as the 'Error\(\)' function doesn't return control\.  A proper way of fixing code depends on the behavior logic initially intended by a programmer\. Perhaps, the function must return control\. It is also possible that the order of expressions is wrong and the correct code should be as follows:

```cpp
FILE* OpenFile(const char *filename)
{
  FILE *f = fopen(filename, "w");
  if (f == nullptr)
  {
    printf("No such file: %s", filename);
    Error();
  }
  return f;
}
```

Let's consider the second example:

```cpp
char ch = strText[i];
switch (ch)
{
case '<':
  ...
  break;
case '>':
  ...
  break;
case 0xB7:
case 0xBB:
  ...
  break;
...
}
```

Here the branch after "case 0xB7:" and "case 0xBB:" will never regain control\. The 'ch' variable is of the 'char' type and therefore the range of its values lies within \[\-128\.\.127\]\. The result of the "ch \=\= 0xB7" and "ch\=\=0xBB" expressions will always be false\. The 'ch' variable has to be of the 'unsigned char' type so that the code was correct\. Fixed code:

```cpp
unsigned char ch = strText[i];
switch (ch)
{
case '<':
  ...
  break;
case '>':
  ...
  break;
case 0xB7:
case 0xBB:
  ...
  break;
...
}
```

Let's consider the third example:

```cpp
if (n < 5) { AB(); }
else if (n < 10) { BC(); }
else if (n < 15) { CD(); }
else if (n < 25) { DE(); }
else if (n < 20) { EF(); } // This branch will never be executed.
else if (n < 30) { FG(); }
```

Due to improper intersection of ranges under conditions, one of the branches will never be executed\. Fixed code:

```cpp
if (n < 5) { AB(); }
else if (n < 10) { BC(); }
else if (n < 15) { CD(); } 
else if (n < 20) { EF(); } 
else if (n < 25) { DE(); } 
else if (n < 30) { FG(); }
```