Skip to content

False positive "Cast is redundant" #37041

@vsfeedback

Description

@vsfeedback

VSF_TYPE_MARKDOWNVisual Studio says that cast in list. Add((Func<B>) Foo); is redundant in the following code (but it is not redundant, removing the cast changes the output of the program).

using System;
using System.Collections.Generic;

namespace RedundantCast
{
    class Program
    {
        class A { }
        class B : A { }

        B Foo() { return null; }

        static void Main()
        {
            (new Program()). Run();
        }

        void Run()
        {
            var list = new List<Func<A>>();
            list. Add((Func<B>) Foo);
            switch (list[0])
            {
                case Func<B> value: Console.WriteLine("B"); break;
                case Func<A> value: Console.WriteLine("A"); break;
            }
        }
    }
}

This issue has been moved from https://developercommunity.visualstudio.com/content/problem/635779/false-positive-cast-is-redundant.html
VSTS ticketId: 943413

These are the original issue comments:

Visual Studio Feedback System on 7/8/2019, 02:11 AM (8 hours ago):

We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.



These are the original issue solutions:
(no solutions)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-IDEBugDeveloper CommunityThe issue was originally reported on https://developercommunity.visualstudio.comhelp 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