Quality gate failing: S2259

         var directoryContents = provider.Value?.GetDirectoryContents("/");

            if (directoryContents is { Exists: true })
                foreach (var resourceFileInfo in directoryContents)

Where provider.Value is an EmbeddedFileProvider from Microsoft.Extensions.FileProviders and directoryContents is an IDirectoryContents.

Error: ‘directoryContents’ is null on at least one execution path.

And the same if I check for null instead of the Exists pattern check.

Hi Will,

Thank you for opening this issue. Your feedback helps us improve our products.

Could you please provide me with the following information:

  • what is the type of the provider variable;
  • in which product you are reproducing the issue (SonarQube, SonarCloud, SonarLint);
  • which version of the C# Analyzer you are using;
  • are you using nullable reference types (C# 8+).

Those pieces of information would help me reproduce your problem.
Could you also confirm that the error is reported on the line containing the foreach statement?

Thank you and best regards,