SonarSource/sonar-dotnet

Fix S1905 FP: casting `IEnumerable<string?>` to `IEnumerable<string>`

Opened this issue · 2 comments

Description

False positive S1905: casting IEnumerable<string?> to IEnumerable<string>.

Repro steps

class SonarTest
{
    public IEnumerable<string> GetNonNullStrings(IEnumerable<string?> strings)
    {
        // S1905: Remove this unnecessary cast to 'IEnumerable<string>'.
        return (IEnumerable<string>)strings.Where(s => s != null);
    }
}

Expected behavior

I didn't expect any diagnostics to be emitted.

Actual behavior

False positive.

Known workarounds

class SonarTest
{
    public IEnumerable<string> GetNonNullStrings(IEnumerable<string?> strings)
    {
        return strings.OfType<string>();
    }
}

Related information

  • SonarLint for Visual Studio 2022, version 7.4.0.80741
  • Visual Studio Professional 2022 (64 bit), version 17.7.6
  • dotnet 7.0.403
  • MSBuild version 17.7.2+d6990bcfa for .NET Framework, version 17.7.2.37605
  • Windows 11 Pro 22H2 22621.2715

Hello @fiotti,

Thank you for reporting this issue!

I confirm this as an FP.
I will add a reproducer to our code base to tackle it in the future!

This might benefit from or require #7036