Skip to content

IDE cannot make up it’s mind: cast is necessary or unnecessary for nullable reference? #33285

@vsfeedback

Description

@vsfeedback

The IDE tells me that the cast is redundant in here:

private EntityIdentifier(EntityIdentifierFlags flags, ReadOnlyMemory<byte> identifier, ReadOnlyMemory<byte> identifierSuffix, ReadOnlyMemory<byte>? buffer = null)
    : base(buffer == null ? new Lazy<ReadOnlyMemory<byte>>(CreateMemoryFactory(flags, identifier, identifierSuffix)) : new Lazy<ReadOnlyMemory<byte>>((ReadOnlyMemory<byte>)buffer))
{
    this.Flags = flags;
    this.Identifier = identifier;
    this.IdentifierSuffix = identifierSuffix;
}

(ReadOnlyMemory<byte>)buffer in new Lazy<ReadOnlyMemory<byte>>((ReadOnlyMemory<byte>)buffer) could be simplified to just buffer, because buffer is ReadOnlyMemory<byte>?. But upon simplification, the IDE cannot find the correct constructor to use. It complains that ReadOnlyMemory<byte>? cannot be converted to type bool.

This issue has been moved from https://developercommunity.visualstudio.com/content/problem/445486/ide-cannot-make-up-its-mind-cast-is-necessary-or-u.html
VSTS ticketId: 786138

These are the original issue comments:

I_Keon_Un on 2/5/2019, 08:14 AM (5 days ago): I solved it by writing an extension method for ReadOnlyMemory>:

static class ReadOnlyMemoryExtensions
{
public static Lazy> ToLazyReadOnlyMemory(this ReadOnlyMemory buffer) => new Lazy>(buffer);
}

And I rewrote my original constructor as follows:

private EntityIdentifier(EntityIdentifierFlags flags, ReadOnlyMemory identifier, ReadOnlyMemory identifierSuffix, ReadOnlyMemory? buffer = null)
: base(buffer?.ToLazyReadOnlyMemory() ?? new Lazy>(CreateMemoryFactory(flags, identifier, identifierSuffix)))
{
this.Flags = flags;
this.Identifier = identifier;
this.IdentifierSuffix = identifierSuffix;
}

That gets rid of the errors. But ideally, the method overload resolution should be smarter than that. And at the very least, the IDE should not be giving misleading advice.
I_Keon_Un on 2/5/2019, 08:15 AM (5 days ago): Oh god… what happened to the markdown formatting in my comment above…?
These are the original issue solutions:
(no solutions)

Metadata

Metadata

Assignees

Labels

Area-IDEBugDeveloper CommunityThe issue was originally reported on https://developercommunity.visualstudio.comIDE-CodeStyleBuilt-in analyzers, fixes, and refactoringshelp 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