Skip to content

IDE0004: Cast is redundant not true when removing nullable for returning a value. #49690

@cafenroy

Description

@cafenroy

Analyzer

Diagnostic ID: https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0004:
Remove unnecessary cast

Analyzer source

SDK: Built-in CA analyzers in .NET 5 SDK or later

Version: SDK 5.0.100

Describe the bug

With the <Nullable>enable</Nullable> parameter ON, a cast to a non-nullable might be necessary.

The analyzer proposes to remove an unnecessary cast from this code.

static IEnumerable<string> DoThis(IEnumerable<string?> notreallynull)
{
    return notreallynull.Where(s => s is not null) as IEnumerable<string>;
}

Expected behavior

The analyzer should not say unnecessary in that situation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-IDEBughelp wantedThe issue is "up for grabs" - add a comment if you are interested in working on it

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions