C# : null coalescing :"'x' is null on at least one execution path"

The following code fails inspection with 'bindingVariables' is null on at least one execution path issue. The issue is detected on the line of foreach loop. Clearly, variable bindingVariables cannot be null past the first if statement.

    public Dictionary<string, string> BindingVariables { get; set; } = new Dictionary<string, string>();

        public void AppendBindingVariables(Dictionary<string, string> bindingVariables)
        {
            if ((bindingVariables?.Count ?? 0) == 0)
            {
                return;
            }

            foreach (var bindingVariable in bindingVariables)
            {
                BindingVariables[bindingVariable.Key] = bindingVariable.Value;
            }
        }

Hi there.

Are you using SonarQube or SonarCloud?

If SonarQube – what version of SonarQube, and what version of the C# analyzer is installed on your instance?

Hi Colin

It’s SonarQube V 8.2.0.32929 sq / looking for lang version

C# analyzer V 8.6.1 (build 17183)installed

Thank you for the reproducer @tchelidze. This is a limitation of our symbolic execution engine. We are tracking this problem in https://github.com/SonarSource/sonar-dotnet/issues/2528.

Best,
Andrei

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.