Skip to content

Unreachable code in Mono.Options GetArgumentName (always false do-while condition) #126538

@rustamque

Description

@rustamque

Hello! While running static analysis on the codebase, an unreachable code warning was flagged in Options.cs inside the GetArgumentName method.

In the following block, the variable j is initialized to 0 and is not modified before the while condition is evaluated:

int start, j = 0;
do {
    start = description.IndexOf (nameStart [i], j);
} while (start >= 0 && j != 0 ? description [j++ - 1] == '{' : false);

Because j is exactly 0 at the end of the first iteration, the ternary operator j != 0 ? ... : false always evaluates to false. This means the loop guarantees to exit after the first iteration, making the check description [j++ - 1] == '{' completely unreachable dead code.

Since this code is quite old, we are unsure if the do-while loop was originally intended to perform a deeper search (and a variable increment is missing), or if the loop is simply redundant and can be safely replaced with a single IndexOf call.

Could you please take a look and advise if this needs a logic fix or just a cleanup?

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Tools-ILLink.NET linker development as well as trimming analyzerstenet-performancePerformance related issueuntriagedNew issue has not been triaged by the area owner

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions