sonar-dotnet icon indicating copy to clipboard operation
sonar-dotnet copied to clipboard

Fix S1854 FN: Support &&, ||, ?? and ??=

Open pavel-mikula-sonarsource opened this issue 3 years ago • 2 comments

The LVA based on Roslyn CFG doesn't support Flow Capturing Operations. We need to support them to properly detect issues in cases where expression is translated into several CFG blocks.

        private void ConditionalEvaluation(bool b1, bool b2, object coalesce, object coalesceAssignment)
        {
            var x = false;  // Compliant ignored value
            x = true;       // Roslyn CFG FN: Consequence of inaccurate LVA state below
            x = b1 && b2;   // Roslyn CFG FN: Branching with FlowCaptureOperation
            x = b1 || b2;   // Roslyn CFG FN: Branching with FlowCaptureOperation
            coalesce = coalesce ?? "Value";   // Roslyn CFG FN: Branching with FlowCaptureOperation
            coalesceAssignment ??= "Value";   // Roslyn CFG FN: Branching with FlowCaptureOperation
        }

This should also solve this FN:

            DeadStores lst;
            lst = new DeadStores  // FN
            {
                Property = 42
            };
            lst = new DeadStores
            {
                Property = 42
            };
            lst.ToString();

The validation has revealed a problem. I re-open this ticket. We either need to fix or revert the PR.

Tim-Pohlmann avatar Aug 02 '24 10:08 Tim-Pohlmann