S1854 False positive

SonarQube version: 7.9.1
Language: Csharp

In the below code example, SQ raises issue S1854 (Dead stores should be removed) for line:
int exitCode = Program.BatchRunFailed;

However, if an exception is raised in the try-block (before the exitCode is set to BatchRunSuccessful), then the initial exitCode will remain on BatchRunFailed and that will be passed to SaveBatchRun and returned. So I believe it is not a useless assignment.

public async Task<int> Start(IEndpointInstance endpointInstance, IBatchProcessor batchProcessor, BatchOptions options)
        {
            int exitCode = Program.BatchRunFailed;  // const int with value -1
            Exception exception = null;
            
            try
            {
                await batchProcessor
                    .Proces(endpointInstance)
                    .ConfigureAwait(false);

                Archive(options);

                exitCode = Program.BatchRunSuccessful;  // const int with value 0
            }
            catch (SystemException e)
            {
                exception = e;
            }
            
            SaveBatchrun(exitCode, exception);

            return exitCode;
        }

I confirm the FP, I opened #2600 to track it

thanks! and welcome to our community :slight_smile: !

1 Like