RemoveUnnecessaryCastTests.cs 84.5 KB
Newer Older
1
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.
2 3

using System;
4
using System.Threading.Tasks;
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.CodeFixes.RemoveUnnecessaryCast;
using Microsoft.CodeAnalysis.CSharp.Diagnostics.RemoveUnnecessaryCast;
using Microsoft.CodeAnalysis.Diagnostics;
using Roslyn.Test.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Diagnostics.RemoveUnnecessaryCast
{
    public partial class RemoveUnnecessaryCastTests : AbstractCSharpDiagnosticProviderBasedUserDiagnosticTest
    {
        internal override Tuple<DiagnosticAnalyzer, CodeFixProvider> CreateDiagnosticProviderAndFixer(Workspace workspace)
        {
            return new Tuple<DiagnosticAnalyzer, CodeFixProvider>(
                new CSharpRemoveUnnecessaryCastDiagnosticAnalyzer(), new RemoveUnnecessaryCastCodeFixProvider());
        }

J
Jared Parsons 已提交
22
        [WorkItem(545979, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545979")]
23
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
24
        public async Task DontRemoveCastToErrorType()
25
        {
26
            await TestMissingAsync(
27 28 29 30 31 32 33 34 35 36 37 38
            @"
class Program
{
    static void Main()
    {
        object s = "";
        foreach (object x in [|(ErrorType)s|]) { }
    }
}
");
        }

J
Jared Parsons 已提交
39
        [WorkItem(545137, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545137"), WorkItem(870550, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/870550")]
40
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
41
        public async Task ParenthesizeToKeepParseTheSame1()
42
        {
43
            await TestAsync(
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
            @"
class Program
{
    static void Main()
    {
        int x = 2;
        int i = 1;
        Foo(x < [|(int)i|], x > (2 + 3));
    }
 
    static void Foo(bool a, bool b) { }
}",

            @"
class Program
{
    static void Main()
    {
        int x = 2;
        int i = 1;
        Foo((x < i), x > (2 + 3));
    }
 
    static void Foo(bool a, bool b) { }
}",

            index: 0,
            compareTokens: false);
        }

J
Jared Parsons 已提交
74
        [WorkItem(545146, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545146")]
75
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
76
        public async Task ParenthesizeToKeepParseTheSame2()
77
        {
78
            await TestAsync(
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
            @"
using System;
 
class C
{
    static void Main()
    {
        Action a = Console.WriteLine;
        ([|(Action)a|])();
    }
}",

            @"
using System;
 
class C
{
    static void Main()
    {
        Action a = Console.WriteLine;
        a();
    }
}",

            index: 0,
            compareTokens: false);
        }

J
Jared Parsons 已提交
107
        [WorkItem(545160, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545160")]
108
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
109
        public async Task ParenthesizeToKeepParseTheSame3()
110
        {
111
            await TestAsync(
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
            @"
using System;
 
class Program
{
    static void Main()
    {
        var x = (Decimal)[|(int)-1|];
    }
}",

            @"
using System;
 
class Program
{
    static void Main()
    {
        var x = (Decimal)(-1);
    }
}",

            index: 0,
            compareTokens: false);
        }

J
Jared Parsons 已提交
138
        [WorkItem(545138, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545138")]
139
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
140
        public async Task DontRemoveTypeParameterCastToObject()
141
        {
142
            await TestMissingAsync(
143 144 145 146 147 148 149 150 151 152 153
            @"
class С
{
    void Foo<T>(T obj)
    {
        int x = (int)[|(object)obj|];
    }
}
");
        }

J
Jared Parsons 已提交
154
        [WorkItem(545139, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545139")]
155
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
156
        public async Task DontRemoveCastInIsTest()
157
        {
158
            await TestMissingAsync(
159 160 161 162 163 164 165 166 167 168 169 170 171 172
            @"
using System;

class С
{
    static void Main()
    {
        DayOfWeek[] a = { };
        Console.WriteLine([|(object)a|] is int[]);
    }
}
");
        }

J
Jared Parsons 已提交
173
        [WorkItem(545142, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545142")]
174
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
175
        public async Task DontRemoveCastNeedForUserDefinedOperator()
176
        {
177
            await TestMissingAsync(
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
            @"
class A
{
    public static implicit operator A(string x)
    {
        return new A();
    }
}
 
class Program
{
    static void Main()
    {
        A x = [|(string)null|];
    }
}
");
        }

J
Jared Parsons 已提交
197
        [WorkItem(545143, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545143")]
198
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
199
        public async Task DontRemovePointerCast1()
200
        {
201
            await TestMissingAsync(
202 203 204 205 206 207 208 209 210 211 212
            @"
unsafe class C
{
    static unsafe void Main()
    {
        var x = (int)[|(int*)null|];
    }
}
");
        }

J
Jared Parsons 已提交
213
        [WorkItem(545144, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545144")]
214
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
215
        public async Task DontRemoveCastToObjectFromDelegateComparison()
216 217 218 219
        {
            // The cast below can't be removed because it would result in the Delegate
            // op_Equality operator overload being used over reference equality.

220
            await TestMissingAsync(
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
            @"
using System;

class Program
{
    static void Main()
    {
        Action a = Console.WriteLine;
        Action b = Console.WriteLine;
        Console.WriteLine(a == [|(object)b|]);
    }
}
");
        }

J
Jared Parsons 已提交
236
        [WorkItem(545145, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545145")]
237
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
238
        public async Task DontRemoveCastToAnonymousMethodWhenOnLeftOfAsCast()
239
        {
240
            await TestMissingAsync(
241 242 243 244 245 246 247 248 249 250 251 252 253
            @"
using System;

class C
{
    static void Main()
    {
        var x = [|(Action)delegate { }|] as Action;
    }
}
");
        }

J
Jared Parsons 已提交
254
        [WorkItem(545147, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545147")]
255
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
256
        public async Task DontRemoveCastInFloatingPointOperation()
257
        {
258
            await TestMissingAsync(
259 260 261 262 263 264 265 266 267 268 269 270
            @"
class C
{
    static void Main()
    {
        int x = 1;
        double y = [|(double)x|] / 2;
    }
}
");
        }

J
Jared Parsons 已提交
271
        [WorkItem(545157, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545157")]
272
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
273
        public async Task DontRemoveIdentityCastWhichAffectsOverloadResolution1()
274
        {
275
            await TestMissingAsync(
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
            @"
using System;
 
class Program
{
    static void Main()
    {
        Foo(x => [|(int)x|]);
    }
 
    static void Foo(Func<int, object> x) { }
    static void Foo(Func<string, object> x) { }
}
");
        }

J
Jared Parsons 已提交
292
        [WorkItem(545158, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545158")]
293
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
294
        public async Task DontRemoveIdentityCastWhichAffectsOverloadResolution2()
295
        {
296
            await TestMissingAsync(
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
            @"
using System;
 
class Program
{
    static void Main()
    {
        var x = [|(IComparable<int>)1|];
        Foo(x);
    }
 
    static void Foo(IComparable<int> x) { }
    static void Foo(int x) {}
}
");
        }

J
Jared Parsons 已提交
314
        [WorkItem(545158, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545158")]
315
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
316
        public async Task DontRemoveIdentityCastWhichAffectsOverloadResolution3()
317
        {
318
            await TestMissingAsync(
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
            @"
using System;
 
class Program
{
    static void Main()
    {
        var x = [|(IComparable<int>)1|];
        var y = x;
        Foo(y);
    }
 
    static void Foo(IComparable<int> x) { }
    static void Foo(int x) {}
}
");
        }

J
Jared Parsons 已提交
337
        [WorkItem(545747, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545747")]
338
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
339
        public async Task DontRemoveCastWhichChangesTypeOfInferredLocal()
340
        {
341
            await TestMissingAsync(
342 343 344 345 346 347 348 349 350 351 352 353
            @"
class C
{
    static void Main()
    {
        var x = [|(long)1|];
        x = long.MaxValue;
    }
}
");
        }

J
Jared Parsons 已提交
354
        [WorkItem(545159, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545159")]
355
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
356
        public async Task DontRemoveNeededCastToIListOfObject()
357
        {
358
            await TestMissingAsync(
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
            @"
using System;
using System.Collections.Generic;
 
class C
{
    static void Main()
    {
        Action<object>[] x = { };
        Foo(x);
    }
 
    static void Foo<T>(Action<T>[] x)
    {
        var y = (IList<Action<object>>)[|(IList<object>)x|];
        Console.WriteLine(y.Count);
    }
}
");
        }

J
Jared Parsons 已提交
380
        [WorkItem(545287, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545287"), WorkItem(880752, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/880752")]
381
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
382
        public async Task RemoveUnneededCastInParameterDefaultValue()
383
        {
384
            await TestAsync(
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
            @"
class Program
{
    static void M1(int? i1 = [|(int?)null|])
    {
    }
}",

@"
class Program
{
    static void M1(int? i1 = null)
    {
    }
}",
    index: 0,
    compareTokens: false);
        }

J
Jared Parsons 已提交
404
        [WorkItem(545289, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545289")]
405
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
406
        public async Task RemoveUnneededCastInReturnStatement()
407
        {
408
            await TestAsync(
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
            @"
class Program
{
    static long M2()
    {
        return [|(long)5|];
    }
}",

@"
class Program
{
    static long M2()
    {
        return 5;
    }
}",
    index: 0,
    compareTokens: false);
        }

J
Jared Parsons 已提交
430
        [WorkItem(545288, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545288")]
431
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
432
        public async Task RemoveUnneededCastInLambda1()
433
        {
434
            await TestAsync(
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
            @"
using System;
class Program
{
    static void M1()
    {
        Func<long> f1 = () => [|(long)5|];
    }
}",

@"
using System;
class Program
{
    static void M1()
    {
        Func<long> f1 = () => 5;
    }
}",
    index: 0,
    compareTokens: false);
        }

J
Jared Parsons 已提交
458
        [WorkItem(545288, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545288")]
459
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
460
        public async Task RemoveUnneededCastInLambda2()
461
        {
462
            await TestAsync(
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
            @"
using System;
class Program
{
    static void M1()
    {
        Func<long> f1 = () => { return [|(long)5|]; };
    }
}",

@"
using System;
class Program
{
    static void M1()
    {
        Func<long> f1 = () => { return 5; };
    }
}",
    index: 0,
    compareTokens: false);
        }

J
Jared Parsons 已提交
486
        [WorkItem(545288, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545288")]
487
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
488
        public async Task RemoveUnneededCastInLambda3()
489
        {
490
            await TestAsync(
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
            @"
using System;
class Program
{
    static void M1()
    {
        Func<long> f1 = _ => { return [|(long)5|]; };
    }
}",

@"
using System;
class Program
{
    static void M1()
    {
        Func<long> f1 = _ => { return 5; };
    }
}",
    index: 0,
    compareTokens: false);
        }

J
Jared Parsons 已提交
514
        [WorkItem(545288, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545288")]
515
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
516
        public async Task RemoveUnneededCastInLambda4()
517
        {
518
            await TestAsync(
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
            @"
using System;
class Program
{
    static void M1()
    {
        Func<long> f1 = _ => [|(long)5|];
    }
}",

@"
using System;
class Program
{
    static void M1()
    {
        Func<long> f1 = _ => 5;
    }
}",
    index: 0,
    compareTokens: false);
        }

J
Jared Parsons 已提交
542
        [WorkItem(545291, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545291")]
543
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
544
        public async Task RemoveUnneededCastInConditionalExpression1()
545
        {
546
            await TestAsync(
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
            @"
class Test
{
    public static void Main()
    {
        int b = 5;

        long f1 = (b == 5) ? [|(long)4|] : (long)5;
    }
}",

@"
class Test
{
    public static void Main()
    {
        int b = 5;

        long f1 = (b == 5) ? 4 : (long)5;
    }
}",
    index: 0,
    compareTokens: false);
        }

J
Jared Parsons 已提交
572
        [WorkItem(545291, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545291")]
573
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
574
        public async Task RemoveUnneededCastInConditionalExpression2()
575
        {
576
            await TestAsync(
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
            @"
class Test
{
    public static void Main()
    {
        int b = 5;

        long f1 = (b == 5) ? (long)4 : [|(long)5|];
    }
}",

@"
class Test
{
    public static void Main()
    {
        int b = 5;

        long f1 = (b == 5) ? (long)4 : 5;
    }
}",
    index: 0,
    compareTokens: false);
        }

J
Jared Parsons 已提交
602
        [WorkItem(545291, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545291")]
603
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
604
        public async Task RemoveUnneededCastInConditionalExpression3()
605
        {
606
            await TestAsync(
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
            @"
class Test
{
    public static void Main()
    {
        int b = 5;

        long f1 = (b == 5) ? 4 : [|(long)5|];
    }
}",

@"
class Test
{
    public static void Main()
    {
        int b = 5;

        long f1 = (b == 5) ? 4 : 5;
    }
}",
    index: 0,
    compareTokens: false);
        }

J
Jared Parsons 已提交
632
        [WorkItem(545291, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545291")]
633
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
634
        public async Task DontRemoveNeededCastInConditionalExpression()
635
        {
636
            await TestMissingAsync(
637 638 639 640 641 642 643 644 645 646 647 648
            @"
class Test
{
    public static void Main()
    {
        int b = 5;

        var f1 = (b == 5) ? 4 : [|(long)5|];
    }
}");
        }

J
Jared Parsons 已提交
649
        [WorkItem(545291, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545291")]
650
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
651
        public async Task RemoveUnneededCastInConditionalExpression4()
652
        {
653
            await TestAsync(
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
            @"
class Test
{
    public static void Main()
    {
        int b = 5;

        var f1 = (b == 5) ? (long)4 : [|(long)5|];
    }
}",

@"
class Test
{
    public static void Main()
    {
        int b = 5;

        var f1 = (b == 5) ? (long)4 : 5;
    }
}",
    index: 0,
    compareTokens: false);
        }

J
Jared Parsons 已提交
679
        [WorkItem(545459, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545459")]
680
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
681
        public async Task RemoveUnneededCastInsideADelegateConstructor()
682
        {
683
            await TestAsync(
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
            @"
using System;
class Test
{
    delegate void D(int x);

    static void Main(string[] args)
    {
        var cd1 = new D([|(Action<int>)M1|]);
    }

    public static void M1(int i) { }
}",

@"
using System;
class Test
{
    delegate void D(int x);

    static void Main(string[] args)
    {
        var cd1 = new D(M1);
    }

    public static void M1(int i) { }
}",
    index: 0,
    compareTokens: false);
        }

J
Jared Parsons 已提交
715
        [WorkItem(545419, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545419")]
716
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
717
        public async Task DontRemoveTriviaWhenRemovingCast()
718
        {
719
            await TestAsync(
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
            @"
using System;
class Test
{
    public static void Main()
    {
        Func<Func<int>> f2 = () =>
        {
            return [|(Func<int>)(/*Lambda returning int const*/() => 5 /*Const returned is 5*/)|];
        };
    }
}",

@"
using System;
class Test
{
    public static void Main()
    {
        Func<Func<int>> f2 = () =>
        {
            return /*Lambda returning int const*/() => 5 /*Const returned is 5*/;
        };
    }
}",
    index: 0,
    compareTokens: false);
        }

J
Jared Parsons 已提交
749
        [WorkItem(545422, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545422")]
750
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
751
        public async Task RemoveUnneededCastInsideCaseLabel()
752
        {
753
            await TestAsync(
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
            @"
class Test
{
    static void Main()
    {
        switch (5L)
        {
            case [|(long)5|]:
                break;
        }
    }
}",

@"
class Test
{
    static void Main()
    {
        switch (5L)
        {
            case 5:
                break;
        }
    }
}",
    index: 0,
    compareTokens: false);
        }

J
Jared Parsons 已提交
783
        [WorkItem(545578, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545578")]
784
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
785
        public async Task RemoveUnneededCastInsideGotoCaseStatement()
786
        {
787
            await TestAsync(
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
            @"
class Test
{
    static void Main()
    {
        switch (5L)
        {
            case 5:
                goto case [|(long)5|];
                break;
        }
    }
}",

@"
class Test
{
    static void Main()
    {
        switch (5L)
        {
            case 5:
                goto case 5;
                break;
        }
    }
}",
    index: 0,
    compareTokens: false);
        }

J
Jared Parsons 已提交
819
        [WorkItem(545595, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545595")]
820
        [WpfFact(Skip = "529787"), Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
821
        public async Task RemoveUnneededCastInCollectionInitializer()
822
        {
823
            await TestAsync(
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
            @"
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        var z = new List<long> { [|(long)0|] };
    }
}",

@"
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        var z = new List<long> { 0 };
    }
}",
    index: 0,
    compareTokens: false);
        }

J
Jared Parsons 已提交
849
        [WorkItem(529787, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529787")]
850
        [WpfFact(Skip = "529787"), Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
851
        public async Task DontRemoveNecessaryCastWhichInCollectionInitializer1()
852
        {
853
            await TestMissingAsync(
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
            @"
using System;
using System.Collections.Generic;

class X : List<int>
{
    void Add(object x) { Console.WriteLine(1); }
    void Add(string x) { Console.WriteLine(2); }
 
    static void Main()
    {
        var z = new X { [|(object)""""|] };
    }
}
");
        }

J
Jared Parsons 已提交
871
        [WorkItem(529787, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529787")]
872
        [WpfFact(Skip = "529787"), Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
873
        public async Task DontRemoveNecessaryCastWhichInCollectionInitializer2()
874
        {
875
            await TestMissingAsync(
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
            @"
using System;
using System.Collections.Generic;

class X : List<int>
{
    void Add(object x) { Console.WriteLine(1); }
    void Add(string x) { Console.WriteLine(2); }

    static void Main()
    {
        X z = new X { [|(object)""""|] };
    }
}
");
        }

J
Jared Parsons 已提交
893
        [WorkItem(545607, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545607")]
894
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
895
        public async Task RemoveUnneededCastInArrayInitializer()
896
        {
897
            await TestAsync(
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
            @"
class X
{
    static void Foo()
    {
        string x = "";
        var s = new object[] { [|(object)x|] };
    }
}",

@"
class X
{
    static void Foo()
    {
        string x = "";
        var s = new object[] { x };
    }
}",
    index: 0,
    compareTokens: false);
        }

J
Jared Parsons 已提交
921
        [WorkItem(545616, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545616")]
922
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
923
        public async Task RemoveUnneededCastWithOverloadedBinaryOperator()
924
        {
925
            await TestAsync(
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
            @"
using System;
class MyAction
{
    static void Foo()
    {
        MyAction x = null;
        var y = x + [|(Action)delegate { }|];
    }
 
    public static MyAction operator +(MyAction x, Action y)
    {
        throw new NotImplementedException();
    }
}",

@"
using System;
class MyAction
{
    static void Foo()
    {
        MyAction x = null;
        var y = x + delegate { };
    }
 
    public static MyAction operator +(MyAction x, Action y)
    {
        throw new NotImplementedException();
    }
}",
    index: 0,
    compareTokens: false);
        }

J
Jared Parsons 已提交
961
        [WorkItem(545822, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545822")]
962
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
963
        public async Task RemoveUnnecessaryCastShouldInsertWhitespaceWhereNeededToKeepCorrectParsing()
964
        {
965
            await TestAsync(
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
            @"
using System;
 
class Program
{
    static void Foo<T>()
    {
        Action a = null;
        var x = [|(Action)(Foo<Guid>)|]==a;
    }
}",

@"
using System;
 
class Program
{
    static void Foo<T>()
    {
        Action a = null;
        var x = (Foo<Guid>) == a;
    }
}",
    index: 0,
    compareTokens: false);
        }

J
Jared Parsons 已提交
993
        [WorkItem(545560, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545560")]
994
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
995
        public async Task DontRemoveNecessaryCastWithExplicitUserDefinedConversion()
996
        {
997
            await TestMissingAsync(
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
            @"
using System;
class A
{
    public static explicit operator long (A x)
    {
        return 1;
    }

    public static implicit operator int (A x)
    {
        return 2;
    }

    static void Main()
    {
        var a = new A();

        long x = [|(long)a|];
        long y = a;

        Console.WriteLine(x); // 1
        Console.WriteLine(y); // 2
    }
}");
        }

J
Jared Parsons 已提交
1025
        [WorkItem(545608, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545608")]
1026
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1027
        public async Task DontRemoveNecessaryCastWithImplicitUserDefinedConversion()
1028
        {
1029
            await TestMissingAsync(
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
            @"
class X
{
    static void Foo()
    {
        X x = null;
        object y = [|(string)x|];
    }
 
    public static implicit operator string (X x)
    {
        return "";
    }
}");
        }

J
Jared Parsons 已提交
1046
        [WorkItem(545941, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545941")]
1047
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1048
        public async Task DontRemoveNecessaryCastWithImplicitConversionInThrow()
1049
        {
C
Charles Stoner 已提交
1050
            // The cast below can't be removed because the throw statement expects
1051 1052 1053
            // an expression of type Exception -- not an expression convertible to
            // Exception.

1054
            await TestMissingAsync(
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
            @"
using System;

class E
{
    public static implicit operator Exception(E e)
    {
        return new Exception();
    }

    static void Main()
    {
        throw [|(Exception)new E()|];
    }
}
");
        }

J
Jared Parsons 已提交
1073
        [WorkItem(545981, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545981")]
1074
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1075
        public async Task DontRemoveNecessaryCastInThrow()
1076
        {
C
Charles Stoner 已提交
1077
            // The cast below can't be removed because the throw statement expects
1078 1079 1080
            // an expression of type Exception -- not an expression convertible to
            // Exception.

1081
            await TestMissingAsync(
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
            @"
using System;

class C
{
    static void Main()
    {
        object ex = new Exception();
        throw [|(Exception)ex|];
    }
}
");
        }

J
Jared Parsons 已提交
1096
        [WorkItem(545941, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545941")]
1097
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1098
        public async Task RemoveUnnecessaryCastInThrow()
1099
        {
1100
            await TestAsync(
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
            @"
using System;

class E
{
    static void Main()
    {
        throw [|(Exception)new Exception()|];
    }
}
",

@"
using System;

class E
{
    static void Main()
    {
        throw new Exception();
    }
}
",
            index: 0,
            compareTokens: false);
        }

J
Jared Parsons 已提交
1128
        [WorkItem(545945, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545945")]
1129
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1130
        public async Task DontRemoveNecessaryDowncast()
1131
        {
1132
            await TestMissingAsync(
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
            @"
class C
{
    void Foo(object y)
    {
        int x = [|(int)y|];
    }
}
");
        }

J
Jared Parsons 已提交
1144
        [WorkItem(545591, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545591")]
1145
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1146
        public async Task DontRemoveNecessaryCastWithinLambda()
1147
        {
1148
            await TestMissingAsync(
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
            @"
using System;
class Program
{
    static void Main()
    {
        Boo(x => Foo(x, y => [|(int)x|]), null); 
    }
    static void Boo(Action<int> x, object y) { Console.WriteLine(1); }
    static void Boo(Action<string> x, string y) { Console.WriteLine(2); }
    static void Foo(int x, Func<int, int> y) { }
    static void Foo(string x, Func<string, string> y) { }
    static void Foo(string x, Func<int, int> y) { }
}
");
        }

J
Jared Parsons 已提交
1166
        [WorkItem(545606, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545606")]
1167
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1168
        public async Task DontRemoveNecessaryCastFromNullToTypeParameter()
1169
        {
1170
            await TestMissingAsync(
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
            @"
class X
{
    static void Foo<T, S>() where T : class, S
    {
        S y = [|(T) null|];
    }
}
");
        }

J
Jared Parsons 已提交
1182
        [WorkItem(545744, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545744")]
1183
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1184
        public async Task DontRemoveNecessaryCastInImplicitlyTypedArray()
1185
        {
1186
            await TestMissingAsync(
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
            @"
class X
{
    static void Foo()
    {
        string x = "";
        var s = new [] { [|(object)x|] };
        s[0] = 1;
    }
}
");
        }

J
Jared Parsons 已提交
1200
        [WorkItem(545750, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545750")]
1201
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1202
        public async Task RemoveUnnecessaryCastToBaseType()
1203
        {
1204
            await TestAsync(
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
            @"
class X
{
    static void Main()
    {
        var s = ([|(object)new X()|]).ToString();
    }

    public override string ToString()
    {
        return "";
    }
}",

@"
class X
{
    static void Main()
    {
        var s = new X().ToString();
    }

    public override string ToString()
    {
        return "";
    }
}",
    index: 0,
    compareTokens: false);
        }

J
Jared Parsons 已提交
1236
        [WorkItem(545855, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545855")]
1237
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1238
        public async Task RemoveUnnecessaryLambdaToDelegateCast()
1239
        {
1240
            await TestAsync(
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
            @"
using System;
using System.Collections.Generic;
using System.Reflection;

static class Program
{
    static void Main()
    {
        FieldInfo[] fields = typeof(Exception).GetFields();
        Console.WriteLine(fields.Any([|(Func<FieldInfo, bool>)(field => field.IsStatic)|]));
    }

    static bool Any<T>(this IEnumerable<T> s, Func<T, bool> predicate)
    {
        return false;
    }

    static bool Any<T>(this ICollection<T> s, Func<T, bool> predicate)
    {
        return true;
    }
}
",

@"
using System;
using System.Collections.Generic;
using System.Reflection;

static class Program
{
    static void Main()
    {
        FieldInfo[] fields = typeof(Exception).GetFields();
        Console.WriteLine(fields.Any(field => field.IsStatic));
    }

    static bool Any<T>(this IEnumerable<T> s, Func<T, bool> predicate)
    {
        return false;
    }

    static bool Any<T>(this ICollection<T> s, Func<T, bool> predicate)
    {
        return true;
    }
}
",
    parseOptions: null,
    index: 0,
    compareTokens: false);
        }

J
Jared Parsons 已提交
1295
        [WorkItem(529816, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529816")]
1296
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1297
        public async Task RemoveUnnecessaryCastInQueryExpression()
1298
        {
1299
            await TestAsync(
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
            @"
using System;

class A
{
    int Select(Func<int, long> x) { return 1; }

    static void Main()
    {
        Console.WriteLine(from y in new A() select [|(long)0|]);
    }
}",

@"
using System;

class A
{
    int Select(Func<int, long> x) { return 1; }

    static void Main()
    {
        Console.WriteLine(from y in new A() select 0);
    }
}",
    index: 0,
    compareTokens: false);
        }

J
Jared Parsons 已提交
1329
        [WorkItem(529816, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529816")]
1330
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1331
        public async Task DontRemoveNecessaryCastInQueryExpression()
1332
        {
1333
            await TestMissingAsync(
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
            @"
using System;

class A
{
    int Select(Func<int, long> x) { return 1; }
    int Select(Func<int, int> x) { return 2; }

    static void Main()
    {
        Console.WriteLine(from y in new A() select [|(long)0|]);
    }
}
");
        }

J
Jared Parsons 已提交
1350
        [WorkItem(545848, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545848")]
1351
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1352
        public async Task DontRemoveNecessaryCastInConstructorInitializer()
1353
        {
1354
            await TestMissingAsync(
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
            @"
using System;

class C
{
    static void Foo(int x, Func<int, int> y) { }
    static void Foo(string x, Func<string, string> y) { }

    C(Action<int> x, object y) { Console.WriteLine(1); }
    C(Action<string> x, string y) { Console.WriteLine(2); }

    C() : this(x => Foo(x, y => [|(int)x|]), null) { }

    static void Main() { new C(); }
}
");
        }

J
Jared Parsons 已提交
1373
        [WorkItem(529831, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529831")]
1374
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1375
        public async Task DontRemoveNecessaryCastFromTypeParameterToInterface()
1376
        {
1377
            await TestMissingAsync(
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
            @"
using System;

interface IIncrementable
{
    int Value { get; }
    void Increment();
}

struct S : IIncrementable
{
    public int Value { get; private set; }
    public void Increment() { Value++; }
}

class C: IIncrementable
{
    public int Value { get; private set; }
    public void Increment() { Value++; }
}

static class Program
{
    static void Main()
    {
        Foo(new S(), new C());
    }

    static void Foo<TAny, TClass>(TAny x, TClass y) 
        where TAny : IIncrementable
        where TClass : class, IIncrementable
    {
        ([|(IIncrementable)x|]).Increment(); // False Unnecessary Cast
        ((IIncrementable)y).Increment(); // Unnecessary Cast - OK

        Console.WriteLine(x.Value);
        Console.WriteLine(y.Value);
    }
}
");
        }

J
Jared Parsons 已提交
1420
        [WorkItem(529831, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529831")]
1421
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1422
        public async Task RemoveUnnecessaryCastFromTypeParameterToInterface()
1423
        {
1424
            await TestAsync(
1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508
            @"
using System;

interface IIncrementable
{
    int Value { get; }
    void Increment();
}

struct S : IIncrementable
{
    public int Value { get; private set; }
    public void Increment() { Value++; }
}

class C: IIncrementable
{
    public int Value { get; private set; }
    public void Increment() { Value++; }
}

static class Program
{
    static void Main()
    {
        Foo(new S(), new C());
    }

    static void Foo<TAny, TClass>(TAny x, TClass y) 
        where TAny : IIncrementable
        where TClass : class, IIncrementable
    {
        ((IIncrementable)x).Increment(); // False Unnecessary Cast
        ([|(IIncrementable)y|]).Increment(); // Unnecessary Cast - OK

        Console.WriteLine(x.Value);
        Console.WriteLine(y.Value);
    }
}
",
 @"
using System;

interface IIncrementable
{
    int Value { get; }
    void Increment();
}

struct S : IIncrementable
{
    public int Value { get; private set; }
    public void Increment() { Value++; }
}

class C: IIncrementable
{
    public int Value { get; private set; }
    public void Increment() { Value++; }
}

static class Program
{
    static void Main()
    {
        Foo(new S(), new C());
    }

    static void Foo<TAny, TClass>(TAny x, TClass y) 
        where TAny : IIncrementable
        where TClass : class, IIncrementable
    {
        ((IIncrementable)x).Increment(); // False Unnecessary Cast
        y.Increment(); // Unnecessary Cast - OK

        Console.WriteLine(x.Value);
        Console.WriteLine(y.Value);
    }
}
",
    index: 0,
    compareTokens: false);
        }

J
Jared Parsons 已提交
1509
        [WorkItem(545877, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545877")]
1510
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1511
        public async Task DontCrashOnIncompleteMethodDeclaration()
1512
        {
1513
            await TestMissingAsync(
1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
            @"
using System;

class A
{
    static void Main()
    {
        byte
        Foo(x => 1, [|(byte)1|]);
    }

    static void Foo<T, S>(T x, ) { }
}
");
        }

J
Jared Parsons 已提交
1530
        [WorkItem(545777, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545777")]
1531
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1532
        public async Task DontRemoveImportantTrailingTrivia()
1533
        {
1534
            await TestAsync(
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565
            @"
class Program
{
    static void Main()
    {
        long x =
#if true
            [|(long) // Remove Unnecessary Cast
#endif
            1|];
    }
}
",

            @"
class Program
{
    static void Main()
    {
        long x =
#if true
            // Remove Unnecessary Cast
#endif
            1;
    }
}
",
    index: 0,
    compareTokens: false);
        }

J
Jared Parsons 已提交
1566
        [WorkItem(529791, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529791")]
1567
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1568
        public async Task RemoveUnnecessaryCastToNullable1()
1569
        {
1570
            await TestAsync(
1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595
            @"
class X
{
    static void Foo()
    {
        object x = (string)null;
        object y = [|(int?)null|];
    }
}
",

            @"
class X
{
    static void Foo()
    {
        object x = (string)null;
        object y = null;
    }
}
",
    index: 0,
    compareTokens: false);
        }

J
Jared Parsons 已提交
1596
        [WorkItem(545842, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545842")]
1597
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1598
        public async Task RemoveUnnecessaryCastToNullable2()
1599
        {
1600
            await TestAsync(
1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627
            @"
static class C
{
    static void Main()
    {
        int? x = 1;
        long y = 2;
        long? z = x + [|(long?) y|];
    }
}
",

            @"
static class C
{
    static void Main()
    {
        int? x = 1;
        long y = 2;
        long? z = x + y;
    }
}
",
    index: 0,
    compareTokens: false);
        }

J
Jared Parsons 已提交
1628
        [WorkItem(545850, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545850")]
1629
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1630
        public async Task RemoveSurroundingParentheses()
1631
        {
1632
            await TestAsync(
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657
            @"
class Program
{
    static void Main()
    {
        int x = 1;
        ([|(int)x|]).ToString();
    }
}
",

            @"
class Program
{
    static void Main()
    {
        int x = 1;
        x.ToString();
    }
}
",
    index: 0,
    compareTokens: false);
        }

J
Jared Parsons 已提交
1658
        [WorkItem(529846, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529846")]
1659
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1660
        public async Task DontRemoveNecessaryCastFromTypeParameterToObject()
1661
        {
1662
            await TestMissingAsync(
1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673
            @"
class C
{
    static void Foo<T>(T x, object y)
    {
        if ([|(object)x|] == y) { }
    }
}
");
        }

J
Jared Parsons 已提交
1674
        [WorkItem(545858, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545858")]
1675
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1676
        public async Task DontRemoveNecessaryCastFromDelegateTypeToMulticastDelegate()
1677
        {
1678
            await TestMissingAsync(
1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693
            @"
using System;

class C
{
    static void Main()
    {
        Action x = Console.WriteLine;
        Action y = Console.WriteLine;
        Console.WriteLine([|(MulticastDelegate)x|] == y);
    }
}
");
        }

J
Jared Parsons 已提交
1694
        [WorkItem(545857, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545857")]
1695
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1696
        public async Task DontRemoveNecessaryCastInSizeOfArrayCreationExpression1()
1697 1698 1699 1700
        {
            // The cast below can't be removed because it would result in the implicit
            // conversion to int being called instead.

1701
            await TestMissingAsync(
1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724
            @"
using System;

class C
{
    static void Main()
    {
        Console.WriteLine(new int[[|(long)default(C)|]].Length);
    }

    public static implicit operator long (C x)
    {
        return 1;
    }

    public static implicit operator int (C x)
    {
        return 2;
    }
}
");
        }

J
Jared Parsons 已提交
1725
        [WorkItem(545980, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545980")]
1726
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1727
        public async Task DontRemoveNecessaryCastInSizeOfArrayCreationExpression2()
1728 1729 1730
        {
            // Array bounds must be an int, so the cast below can't be removed.

1731
            await TestMissingAsync(
1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742
            @"
class C
{
    static void Main()
    {
        var a = new int[[|(int)decimal.Zero|]];
    }
}
");
        }

J
Jared Parsons 已提交
1743
        [WorkItem(529842, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529842")]
1744
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1745
        public async Task DontRemoveNecessaryCastInTernaryExpression()
1746
        {
1747
            await TestMissingAsync(
1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767
            @"
using System;

class X
{
    public static implicit operator string (X x)
    {
        return x.ToString();
    }

    static void Main()
    {
        bool b = true;
        X x = new X();
        Console.WriteLine(b ? [|(string)null|] : x);
    }
}
");
        }

J
Jared Parsons 已提交
1768
        [WorkItem(545882, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545882"), WorkItem(880752, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/880752")]
1769
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1770
        public async Task RemoveCastInConstructorInitializer1()
1771
        {
1772
            await TestAsync(
1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791
@"
class C
{
    C(int x) { }
    C() : this([|(int)1|]) { }
}
",

@"
class C
{
    C(int x) { }
    C() : this(1) { }
}
",
            index: 0,
            compareTokens: false);
        }

J
Jared Parsons 已提交
1792
        [WorkItem(545958, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545958"), WorkItem(880752, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/880752")]
1793
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1794
        public async Task RemoveCastInConstructorInitializer2()
1795
        {
1796
            await TestAsync(
1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821
@"
using System.Collections;

class C
{
    C(int x) { }
    C(object x) { }
    C() : this([|(IEnumerable)""""|]) { }
}
",

@"
using System.Collections;

class C
{
    C(int x) { }
    C(object x) { }
    C() : this("""") { }
}
",
            index: 0,
            compareTokens: false);
        }

J
Jared Parsons 已提交
1822
        [WorkItem(545957, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545957")]
1823
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1824
        public async Task DontRemoveCastInConstructorInitializer3()
1825
        {
1826
            await TestMissingAsync(
1827 1828 1829 1830 1831 1832 1833 1834 1835
@"
class C
{
    C(int x) { }
    C() : this([|(long)1|]) { }
}
");
        }

J
Jared Parsons 已提交
1836
        [WorkItem(545842, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545842")]
1837
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1838
        public async Task RemoveCastToNullableInArithmeticExpression()
1839
        {
1840
            await TestAsync(
1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867
@"
static class C
{
    static void Main()
    {
        int? x = 1;
        long y = 2;
        long? z = x + [|(long?)y|];
    }
}
",

@"
static class C
{
    static void Main()
    {
        int? x = 1;
        long y = 2;
        long? z = x + y;
    }
}
",
            index: 0,
            compareTokens: false);
        }

J
Jared Parsons 已提交
1868
        [WorkItem(545942, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545942")]
1869
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1870
        public async Task DontRemoveCastFromValueTypeToObjectInReferenceEquality()
1871 1872 1873 1874
        {
            // Note: The cast below can't be removed because it would result in an
            // illegal reference equality test between object and a value type.

1875
            await TestMissingAsync(
1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889
            @"
using System;

class Program
{
    static void Main()
    {
        object x = 1;
        Console.WriteLine(x == [|(object)1|]);
    }
}
");
        }

J
Jared Parsons 已提交
1890
        [WorkItem(545962, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545962")]
1891
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1892
        public async Task DontRemoveCastWhenExpressionDoesntBind()
1893 1894 1895
        {
            // Note: The cast below can't be removed because its expression doesn't bind.

1896
            await TestMissingAsync(
1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910
            @"
using System;
 
class Program
{
    static void Main()
    {
        ([|(IDisposable)x|]).Dispose();
    }
}

");
        }

J
Jared Parsons 已提交
1911
        [WorkItem(545944, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545944")]
1912
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1913
        public async Task DontRemoveNecessaryCastBeforePointerDereference1()
1914 1915 1916 1917
        {
            // Note: The cast below can't be removed because it would result in *null,
            // which is illegal.

1918
            await TestMissingAsync(
1919 1920 1921 1922 1923 1924 1925 1926
            @"
unsafe class C
{
    int x = *[|(int*)null|];
}
");
        }

J
Jared Parsons 已提交
1927
        [WorkItem(545978, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545978")]
1928
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1929
        public async Task DontRemoveNecessaryCastBeforePointerDereference2()
1930 1931 1932 1933
        {
            // Note: The cast below can't be removed because it would result in dereferencing
            // void*, which is illegal.

1934
            await TestMissingAsync(
1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946
            @"
unsafe class C
{
    static void Main()
    {
        void* p = null;
        int x = *[|(int*)p|];
    }
}
");
        }

1947 1948
        [WorkItem(2691, "https://github.com/dotnet/roslyn/issues/2691")]
        [WorkItem(2987, "https://github.com/dotnet/roslyn/issues/2987")]
1949
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1950
        public async Task DontRemoveNecessaryCastBeforePointerDereference3()
1951 1952 1953 1954
        {
            // Conservatively disable cast simplifications for casts involving pointer conversions.
            // https://github.com/dotnet/roslyn/issues/2987 tracks improving cast simplification for this scenario.

1955
            await TestMissingAsync(
1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968
            @"
class C
{
    public unsafe float ReadSingle(byte* ptr)
    {
        return *[|(float*)ptr|];
    }
}
");
        }

        [WorkItem(2691, "https://github.com/dotnet/roslyn/issues/2691")]
        [WorkItem(2987, "https://github.com/dotnet/roslyn/issues/2987")]
1969
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1970
        public async Task DontRemoveNumericCastInUncheckedExpression()
1971 1972 1973 1974
        {
            // Conservatively disable cast simplifications within explicit checked/unchecked expressions.
            // https://github.com/dotnet/roslyn/issues/2987 tracks improving cast simplification for this scenario.

1975
            await TestMissingAsync(
1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993
            @"
class C
{
    private unsafe readonly byte* _endPointer;
    private unsafe byte* _currentPointer;

    private unsafe void CheckBounds(int byteCount)
    {
        if (unchecked([|(uint)byteCount)|] > (_endPointer - _currentPointer))
        {
        }
    }
}
");
        }

        [WorkItem(2691, "https://github.com/dotnet/roslyn/issues/2691")]
        [WorkItem(2987, "https://github.com/dotnet/roslyn/issues/2987")]
1994
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1995
        public async Task DontRemoveNumericCastInUncheckedStatement()
1996 1997 1998 1999
        {
            // Conservatively disable cast simplifications within explicit checked/unchecked statements.
            // https://github.com/dotnet/roslyn/issues/2987 tracks improving cast simplification for this scenario.

2000
            await TestMissingAsync(
2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021
            @"
class C
{
    private unsafe readonly byte* _endPointer;
    private unsafe byte* _currentPointer;

    private unsafe void CheckBounds(int byteCount)
    {
        unchecked
        {
            if (([|(uint)byteCount)|] > (_endPointer - _currentPointer))
            {
            }
        }
    }
}
");
        }

        [WorkItem(2691, "https://github.com/dotnet/roslyn/issues/2691")]
        [WorkItem(2987, "https://github.com/dotnet/roslyn/issues/2987")]
2022
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2023
        public async Task DontRemoveNumericCastInCheckedExpression()
2024 2025 2026 2027
        {
            // Conservatively disable cast simplifications within explicit checked/unchecked expressions.
            // https://github.com/dotnet/roslyn/issues/2987 tracks improving cast simplification for this scenario.

2028
            await TestMissingAsync(
2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046
            @"
class C
{
    private unsafe readonly byte* _endPointer;
    private unsafe byte* _currentPointer;

    private unsafe void CheckBounds(int byteCount)
    {
        if (checked([|(uint)byteCount)|] > (_endPointer - _currentPointer))
        {
        }
    }
}
");
        }

        [WorkItem(2691, "https://github.com/dotnet/roslyn/issues/2691")]
        [WorkItem(2987, "https://github.com/dotnet/roslyn/issues/2987")]
2047
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2048
        public async Task DontRemoveNumericCastInCheckedStatement()
2049 2050 2051 2052
        {
            // Conservatively disable cast simplifications within explicit checked/unchecked statements.
            // https://github.com/dotnet/roslyn/issues/2987 tracks improving cast simplification for this scenario.

2053
            await TestMissingAsync(
2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072
            @"
class C
{
    private unsafe readonly byte* _endPointer;
    private unsafe byte* _currentPointer;

    private unsafe void CheckBounds(int byteCount)
    {
        checked
        {
            if (([|(uint)byteCount)|] > (_endPointer - _currentPointer))
            {
            }
        }
    }
}
");
        }

J
Jared Parsons 已提交
2073
        [WorkItem(545894, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545894")]
2074
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2075
        public async Task DontRemoveNecessaryCastInAttribute()
2076
        {
2077
            await TestMissingAsync(
2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090
            @"
using System;

[A([|(byte)0)|]]
class A : Attribute
{
    public A(object x) {  }
}
");
        }

        #region Interface Casts

J
Jared Parsons 已提交
2091
        [WorkItem(545889, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545889")]
2092
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2093
        public async Task DontRemoveCastToInterfaceForUnsealedType()
2094 2095 2096
        {
            // Note: The cast below can't be removed because X is not sealed.

2097
            await TestMissingAsync(
2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123
            @"
using System;

class X : IDisposable
{
    static void Main()
    {
        X x = new Y();
        ([|(IDisposable)x|]).Dispose();
    }
    public void Dispose()
    {
        Console.WriteLine(""X.Dispose"");
    }
}

class Y : X, IDisposable
{
    void IDisposable.Dispose()
    {
        Console.WriteLine(""Y.Dispose"");
    }
}
");
        }

J
Jared Parsons 已提交
2124
        [WorkItem(545890, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545890")]
2125
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2126
        public async Task RemoveCastToInterfaceForSealedType1()
2127 2128 2129 2130 2131
        {
            // Note: The cast below can be removed because C is sealed and the
            // unspecified optional parameters of I.Foo() and C.Foo() have the
            // same default values.

2132
            await TestAsync(
2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179
@"
using System;

interface I
{
    void Foo(int x = 0);
}

sealed class C : I
{
    public void Foo(int x = 0)
    {
        Console.WriteLine(x);
    }

    static void Main()
    {
        ([|(I)new C()|]).Foo();
    }
}
",

@"
using System;

interface I
{
    void Foo(int x = 0);
}

sealed class C : I
{
    public void Foo(int x = 0)
    {
        Console.WriteLine(x);
    }

    static void Main()
    {
        new C().Foo();
    }
}
",
            index: 0,
            compareTokens: false);
        }

J
Jared Parsons 已提交
2180
        [WorkItem(545890, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545890")]
2181
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2182
        public async Task RemoveCastToInterfaceForSealedType2()
2183 2184 2185 2186
        {
            // Note: The cast below can be removed because C is sealed and the
            // interface member has no parameters.

2187
            await TestAsync(
2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240
@"
using System;

interface I
{
    string Foo { get; }
}

sealed class C : I
{
    public string Foo
    {
        get
        {
            return ""Nikov Rules"";
        }
    }

    static void Main()
    {
        Console.WriteLine(([|(I)new C()|]).Foo);
    }
}
",

@"
using System;

interface I
{
    string Foo { get; }
}

sealed class C : I
{
    public string Foo
    {
        get
        {
            return ""Nikov Rules"";
        }
    }

    static void Main()
    {
        Console.WriteLine(new C().Foo);
    }
}
",
            index: 0,
            compareTokens: false);
        }

J
Jared Parsons 已提交
2241
        [WorkItem(545890, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545890")]
2242
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2243
        public async Task RemoveCastToInterfaceForSealedType3()
2244 2245 2246 2247
        {
            // Note: The cast below can be removed because C is sealed and the
            // interface member has no parameters.

2248
            await TestAsync(
2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305
@"
using System;

interface I
{
    string Foo { get; }
}

sealed class C : I
{
    public C Instance { get { return new C(); } }

    public string Foo
    {
        get
        {
            return ""Nikov Rules"";
        }
    }

    static void Main()
    {
        Console.WriteLine(([|(I)Instance|]).Foo);
    }
}
",

@"
using System;

interface I
{
    string Foo { get; }
}

sealed class C : I
{
    public C Instance { get { return new C(); } }

    public string Foo
    {
        get
        {
            return ""Nikov Rules"";
        }
    }

    static void Main()
    {
        Console.WriteLine(Instance.Foo);
    }
}
",
            index: 0,
            compareTokens: false);
        }

J
Jared Parsons 已提交
2306
        [WorkItem(545890, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545890")]
2307
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2308
        public async Task DontRemoveCastToInterfaceForSealedType4()
2309 2310 2311 2312
        {
            // Note: The cast below can't be removed (even though C is sealed)
            // because the unspecified optional parameter default values differ.

2313
            await TestMissingAsync(
2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336
            @"
using System;

interface I
{
    void Foo(int x = 0);
}

sealed class C : I
{
    public void Foo(int x = 1)
    {
        Console.WriteLine(x);
    }

    static void Main()
    {
        ([|(I)new C()|]).Foo();
    }
}
");
        }

J
Jared Parsons 已提交
2337
        [WorkItem(545890, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545890")]
2338
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2339
        public async Task RemoveCastToInterfaceForSealedType5()
2340 2341 2342 2343 2344
        {
            // Note: The cast below can be removed (even though C is sealed)
            // because the optional parameters whose default values differ are
            // specified.

2345
            await TestAsync(
2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392
@"
using System;

interface I
{
    void Foo(int x = 0);
}

sealed class C : I
{
    public void Foo(int x = 1)
    {
        Console.WriteLine(x);
    }

    static void Main()
    {
        ([|(I)new C()|]).Foo(2);
    }
}
",

@"
using System;

interface I
{
    void Foo(int x = 0);
}

sealed class C : I
{
    public void Foo(int x = 1)
    {
        Console.WriteLine(x);
    }

    static void Main()
    {
        new C().Foo(2);
    }
}
",
            index: 0,
            compareTokens: false);
        }

J
Jared Parsons 已提交
2393
        [WorkItem(545888, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545888")]
2394
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2395
        public async Task DontRemoveCastToInterfaceForSealedType6()
2396 2397 2398 2399 2400
        {
            // Note: The cast below can't be removed (even though C is sealed)
            // because the specified named arguments refer to parameters that
            // appear at different positions in the member signatures.

2401
            await TestMissingAsync(
2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424
            @"
using System;

interface I
{
    void Foo(int x = 0, int y = 0);
}

sealed class C : I
{
    public void Foo(int y = 0, int x = 0)
    {
        Console.WriteLine(x);
    }

    static void Main()
    {
        ([|(I)new C()|]).Foo(x: 1);
    }
}
");
        }

J
Jared Parsons 已提交
2425
        [WorkItem(545888, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545888")]
2426
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2427
        public async Task RemoveCastToInterfaceForSealedType7()
2428
        {
2429
            await TestAsync(
2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482
@"
using System;

interface I
{
    int this[int x = 0, int y = 0] { get; }
}

sealed class C : I
{
    public int this[int x = 0, int y = 0]
    {
        get
        {
            return x * 2;
        }
    }

    static void Main()
    {
        Console.WriteLine(([|(I)new C()|])[x: 1]);
    }
}
",

@"
using System;

interface I
{
    int this[int x = 0, int y = 0] { get; }
}

sealed class C : I
{
    public int this[int x = 0, int y = 0]
    {
        get
        {
            return x * 2;
        }
    }

    static void Main()
    {
        Console.WriteLine(new C()[x: 1]);
    }
}
",
            index: 0,
            compareTokens: false);
        }

J
Jared Parsons 已提交
2483
        [WorkItem(545888, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545888")]
2484
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2485
        public async Task DontRemoveCastToInterfaceForSealedType8()
2486 2487 2488 2489 2490
        {
            // Note: The cast below can't be removed (even though C is sealed)
            // because the specified named arguments refer to parameters that
            // appear at different positions in the member signatures.

2491
            await TestMissingAsync(
2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517
            @"
using System;

interface I
{
    int this[int x = 0, int y = 0] { get; }
}

sealed class C : I
{
    public int this(int y = 0, int x = 0)
    {
        get
        {
            return x * 2;
        }
    }

    static void Main()
    {
        Console.WriteLine(([|(I)new C()|])[x: 1]);
    }
}
");
        }

J
Jared Parsons 已提交
2518
        [WorkItem(545883, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545883")]
2519
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2520
        public async Task DontRemoveCastToInterfaceForSealedType9()
2521 2522 2523 2524 2525
        {
            // Note: The cast below can't be removed (even though C is sealed)
            // because it would result in binding to a Dispose method that doesn't
            // implement IDisposable.Dispose().

2526
            await TestMissingAsync(
2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546
            @"
using System;
using System.IO;

sealed class C : MemoryStream
{
    static void Main()
    {
        C s = new C();
        ([|(IDisposable)s|]).Dispose();
    }

    new public void Dispose()
    {
        Console.WriteLine(""new Dispose()"");
    }
}
");
        }

J
Jared Parsons 已提交
2547
        [WorkItem(545887, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545887")]
2548
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2549
        public async Task DontRemoveCastToInterfaceForStruct1()
2550 2551 2552 2553
        {
            // Note: The cast below can't be removed because the cast boxes 's' and
            // unboxing would change program behavior.

2554
            await TestMissingAsync(
2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578
            @"
using System;

interface IIncrementable
{
    int Value { get; }
    void Increment();
}

struct S : IIncrementable
{
    public int Value { get; private set; }
    public void Increment() { Value++; }

    static void Main()
    {
        var s = new S();
        ([|(IIncrementable)s|]).Increment();
        Console.WriteLine(s.Value);
    }
}
");
        }

J
Jared Parsons 已提交
2579
        [WorkItem(545834, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545834")]
2580
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2581
        public async Task RemoveCastToInterfaceForStruct2()
2582 2583 2584 2585
        {
            // Note: The cast below can be removed because we are sure to have
            // a fresh copy of the struct from the GetEnumerator() method.

2586
            await TestAsync(
2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627
            @"
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        ([|(IDisposable)GetEnumerator()|]).Dispose();
    }

    static List<int>.Enumerator GetEnumerator()
    {
        var x = new List<int> { 1, 2, 3 };
        return x.GetEnumerator();
    }
}
",

@"
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        GetEnumerator().Dispose();
    }

    static List<int>.Enumerator GetEnumerator()
    {
        var x = new List<int> { 1, 2, 3 };
        return x.GetEnumerator();
    }
}
",
    index: 0,
    compareTokens: false);
        }

J
Jared Parsons 已提交
2628
        [WorkItem(544655, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/544655")]
2629
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2630
        public async Task RemoveCastToICloneableForDelegate()
2631
        {
C
Charles Stoner 已提交
2632
            // Note: The cast below can be removed because delegates are implicitly
2633 2634
            // sealed.

2635
            await TestAsync(
2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664
            @"
using System;

class C
{
    static void Main()
    {
        Action a = () => { };
        var c = ([|(ICloneable)a|]).Clone();
    }
}
",

@"
using System;

class C
{
    static void Main()
    {
        Action a = () => { };
        var c = a.Clone();
    }
}
",
    index: 0,
    compareTokens: false);
        }

J
Jared Parsons 已提交
2665
        [WorkItem(545926, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545926")]
2666
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2667
        public async Task RemoveCastToICloneableForArray()
2668
        {
C
Charles Stoner 已提交
2669
            // Note: The cast below can be removed because arrays are implicitly
2670 2671
            // sealed.

2672
            await TestAsync(
2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701
            @"
using System;

class C
{
    static void Main()
    {
        var a = new[] { 1, 2, 3 };
        var c = ([|(ICloneable)a|]).Clone(); 
    }
}
",

@"
using System;

class C
{
    static void Main()
    {
        var a = new[] { 1, 2, 3 };
        var c = a.Clone(); 
    }
}
",
    index: 0,
    compareTokens: false);
        }

J
Jared Parsons 已提交
2702
        [WorkItem(529897, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529897")]
2703
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2704
        public async Task RemoveCastToIConvertibleForEnum()
2705
        {
C
Charles Stoner 已提交
2706
            // Note: The cast below can be removed because enums are implicitly
2707 2708
            // sealed.

2709
            await TestAsync(
2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742
            @"
using System;

class Program
{
    static void Main()
    {
        Enum e = DayOfWeek.Monday;
        var y = ([|(IConvertible)e|]).GetTypeCode();
    }
}
",

@"
using System;

class Program
{
    static void Main()
    {
        Enum e = DayOfWeek.Monday;
        var y = e.GetTypeCode();
    }
}
",
    index: 0,
    compareTokens: false);
        }

        #endregion

        #region ParamArray Parameter Casts

J
Jared Parsons 已提交
2743
        [WorkItem(545141, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545141")]
2744
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2745
        public async Task DontRemoveCastToObjectInParamArrayArg1()
2746
        {
2747
            await TestMissingAsync(
2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765
            @"
using System;

class C
{
    static void Main()
    {
        Foo([|(object)null|]);
    }

    static void Foo(params object[] x)
    {
        Console.WriteLine(x.Length);
    }
}
");
        }

J
Jared Parsons 已提交
2766
        [WorkItem(529911, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529911")]
2767
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2768
        public async Task DontRemoveCastToIntArrayInParamArrayArg2()
2769
        {
2770
            await TestMissingAsync(
2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788
            @"
using System;

class C
{
    static void Main()
    {
        Foo([|(int[])null|]);
    }

    static void Foo(params object[] x)
    {
        Console.WriteLine(x.Length);
    }
}
");
        }

J
Jared Parsons 已提交
2789
        [WorkItem(529911, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529911")]
2790
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2791
        public async Task DontRemoveCastToObjectArrayInParamArrayArg3()
2792
        {
2793
            await TestMissingAsync(
2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811
            @"
using System;

class C
{
    static void Main()
    {
        Foo([|(object[])null|]);
    }

    static void Foo(params object[][] x)
    {
        Console.WriteLine(x.Length);
    }
}
");
        }

J
Jared Parsons 已提交
2812
        [WorkItem(529911, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529911")]
2813
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2814
        public async Task RemoveCastToObjectArrayInParamArrayArg1()
2815
        {
2816
            await TestAsync(
2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843
            @"
class C
{
    static void Foo(params object[] x) { }

    static void Main()
    {
        Foo([|(object[])null|]);
    }
}
",

@"
class C
{
    static void Foo(params object[] x) { }

    static void Main()
    {
        Foo(null);
    }
}
",
            index: 0,
            compareTokens: false);
        }

J
Jared Parsons 已提交
2844
        [WorkItem(529911, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529911")]
2845
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2846
        public async Task RemoveCastToStringArrayInParamArrayArg2()
2847
        {
2848
            await TestAsync(
2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875
            @"
class C
{
    static void Foo(params object[] x) { }

    static void Main()
    {
        Foo([|(string[])null|]);
    }
}
",

@"
class C
{
    static void Foo(params object[] x) { }

    static void Main()
    {
        Foo(null);
    }
}
",
            index: 0,
            compareTokens: false);
        }

J
Jared Parsons 已提交
2876
        [WorkItem(529911, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529911")]
2877
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2878
        public async Task RemoveCastToIntArrayInParamArrayArg3()
2879
        {
2880
            await TestAsync(
2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907
            @"
class C
{
    static void Foo(params int[] x) { }

    static void Main()
    {
        Foo([|(int[])null|]);
    }
}
",

@"
class C
{
    static void Foo(params int[] x) { }

    static void Main()
    {
        Foo(null);
    }
}
",
            index: 0,
            compareTokens: false);
        }

J
Jared Parsons 已提交
2908
        [WorkItem(529911, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529911")]
2909
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2910
        public async Task RemoveCastToObjectArrayInParamArrayArg4()
2911
        {
2912
            await TestAsync(
2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939
            @"
class C
{
    static void Foo(params object[] x) { }

    static void Main()
    {
        Foo([|(object[])null|], null);
    }
}
",

@"
class C
{
    static void Foo(params object[] x) { }

    static void Main()
    {
        Foo(null, null);
    }
}
",
            index: 0,
            compareTokens: false);
        }

J
Jared Parsons 已提交
2940
        [WorkItem(529911, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529911")]
2941
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2942
        public async Task RemoveCastToObjectInParamArrayArg5()
2943
        {
2944
            await TestAsync(
2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971
            @"
class C
{
    static void Foo(params object[] x) { }

    static void Main()
    {
        Foo([|(object)null|], null);
    }
}
",

@"
class C
{
    static void Foo(params object[] x) { }

    static void Main()
    {
        Foo(null, null);
    }
}
",
            index: 0,
            compareTokens: false);
        }

2972
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2973
        public async Task RemoveCastToObjectArrayInParamArrayWithNamedArgument()
2974
        {
2975
            await TestAsync(
2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005
                @"
class C
{
    static void Main()
    {
        Foo(x: [|(object[])null|]);
    }

    static void Foo(params object[] x) { }
}
",
                @"
class C
{
    static void Main()
    {
        Foo(x: null);
    }

    static void Foo(params object[] x) { }
}
",
            index: 0,
            compareTokens: false);
        }

        #endregion

        #region ForEach Statements

J
Jared Parsons 已提交
3006
        [WorkItem(545961, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545961")]
3007
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3008
        public async Task DontRemoveNecessaryCastInForEach1()
3009 3010 3011 3012
        {
            // The cast below can't be removed because it would result an error
            // in the foreach statement.

3013
            await TestMissingAsync(
3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027
            @"
using System.Collections;

class Program
{
    static void Main()
    {
        object s = "";
        foreach (object x in [|(IEnumerable)s|]) { }
    }
}
");
        }

J
Jared Parsons 已提交
3028
        [WorkItem(545961, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545961")]
3029
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3030
        public async Task DontRemoveNecessaryCastInForEach2()
3031 3032 3033 3034
        {
            // The cast below can't be removed because it would result an error
            // in the foreach statement.

3035
            await TestMissingAsync(
3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049
            @"
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        object s = "";
        foreach (object x in [|(IEnumerable<char>)s|]) { }
    }
}
");
        }

J
Jared Parsons 已提交
3050
        [WorkItem(545961, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545961")]
3051
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3052
        public async Task DontRemoveNecessaryCastInForEach3()
3053 3054 3055 3056 3057
        {
            // The cast below can't be removed because it would result an error
            // in the foreach statement since C doesn't contain a GetEnumerator()
            // method.

3058
            await TestMissingAsync(
3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084
            @"
using System.Collections;

class D
{
    public IEnumerator GetEnumerator()
    {
        yield return 1;
    }
}

class C
{
    public static implicit operator D(C c)
    {
        return new D();
    }

    static void Main()
    {
        object s = "";
        foreach (object x in [|(D)new C()|]) { }
    }
}");
        }

J
Jared Parsons 已提交
3085
        [WorkItem(545961, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545961")]
3086
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3087
        public async Task DontRemoveNecessaryCastInForEach4()
3088 3089 3090 3091
        {
            // The cast below can't be removed because it would result in
            // C.GetEnumerator() being called rather than D.GetEnumerator().

3092
            await TestMissingAsync(
3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127
            @"
using System;
using System.Collections;

class D
{
    public IEnumerator GetEnumerator()
    {
        yield return 1;
    }
}

class C
{
    public IEnumerator GetEnumerator()
    {
        yield return 2;
    }

    public static implicit operator D(C c)
    {
        return new D();
    }

    static void Main()
    {
        object s = "";
        foreach (object x in [|(D)new C()|])
        {
            Console.WriteLine(x);
        }
    }
}");
        }

J
Jared Parsons 已提交
3128
        [WorkItem(545961, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545961")]
3129
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3130
        public async Task DontRemoveNecessaryCastInForEach5()
3131 3132 3133 3134
        {
            // The cast below can't be removed because it would change the
            // type of 'x'.

3135
            await TestMissingAsync(
3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155
            @"
using System;
 
class Program
{
    static void Main()
    {
        string[] s = { ""A"" };
        foreach (var x in [|(Array)s|])
        {
            var y = x;
            y = 1;
        }
    }
}
");
        }

        #endregion

J
Jared Parsons 已提交
3156
        [WorkItem(545925, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545925")]
3157
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3158
        public async Task DontRemoveCastIfOverriddenMethodHasIncompatibleParameterList()
3159 3160 3161 3162
        {
            // Note: The cast below can't be removed because the parameter list
            // of Foo and its override have different default values.

3163
            await TestMissingAsync(
3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186
            @"
using System;

abstract class Y
{
    public abstract void Foo(int x = 1);
}

class X : Y
{
    static void Main()
    {
        ([|(Y)new X()|]).Foo();
    }

    public override void Foo(int x = 2)
    {
        Console.WriteLine(x);
    }
}
");
        }

J
Jared Parsons 已提交
3187
        [WorkItem(545925, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545925")]
3188
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3189
        public async Task RemoveCastIfOverriddenMethodHaveCompatibleParameterList()
3190 3191 3192 3193
        {
            // Note: The cast below can be removed because the parameter list
            // of Foo and its override have the same default values.

3194
            await TestAsync(
3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241
@"
using System;

abstract class Y
{
    public abstract void Foo(int x = 1);
}

class X : Y
{
    static void Main()
    {
        ([|(Y)new X()|]).Foo();
    }

    public override void Foo(int x = 1)
    {
        Console.WriteLine(x);
    }
}
",

@"
using System;

abstract class Y
{
    public abstract void Foo(int x = 1);
}

class X : Y
{
    static void Main()
    {
        new X().Foo();
    }

    public override void Foo(int x = 1)
    {
        Console.WriteLine(x);
    }
}
",
            index: 0,
            compareTokens: false);
        }

J
Jared Parsons 已提交
3242
        [WorkItem(529916, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529916")]
3243
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3244
        public async Task RemoveCastInReceiverForMethodGroup()
3245 3246 3247 3248
        {
            // Note: The cast below can be removed because the it results in
            // the same method group.

3249
            await TestAsync(
3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280
@"
using System;

static class Program
{
    static void Main()
    {
        Action a = ([|(string)""""|]).Foo;
    }

    static void Foo(this string x) { }
}
",

@"
using System;

static class Program
{
    static void Main()
    {
        Action a = """".Foo;
    }

    static void Foo(this string x) { }
}
",
            index: 0,
            compareTokens: false);
        }

J
Jared Parsons 已提交
3281
        [WorkItem(609497, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/609497")]
3282
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3283
        public async Task Bugfix_609497()
3284
        {
3285
            await TestMissingAsync(
3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306
@"
using System;
using System.Threading.Tasks;
 
class Program
{
    static void Main()
    {
        Foo().Wait();
    }
 
    static async Task Foo()
    {
        Task task = Task.FromResult(0);
        Console.WriteLine(await [|(dynamic)task|]);
    }
}

");
        }

J
Jared Parsons 已提交
3307
        [WorkItem(545995, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545995")]
3308
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3309
        public async Task DontRemoveCastToDifferentTypeWithSameName()
3310 3311 3312 3313
        {
            // Note: The cast below cannot be removed because the it results in
            // a different overload being picked.

3314
            await TestMissingAsync(
3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341
@"
using System;
using MyInt = System.Int32;

namespace System
{
    public struct Int32
    {
        public static implicit operator Int32(int x)
        {
            return default(Int32);
        }
    }
}

class A
{
    static void Foo(int x) { Console.WriteLine(""int""); }
    static void Foo(MyInt x) { Console.WriteLine(""MyInt""); }
    static void Main()
    {
        Foo([|(MyInt)0|]);
    }
}
");
        }

J
Jared Parsons 已提交
3342
        [WorkItem(545921, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545921")]
3343
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3344
        public async Task DontRemoveCastWhichWouldChangeAttributeOverloadResolution1()
3345 3346 3347 3348
        {
            // Note: The cast below cannot be removed because it would result in
            // a different attribute constructor being picked

3349
            await TestMissingAsync(
3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376
@"
using System;

[Flags]
enum EEEnum
{
    Flag1 = 0x2,
    Flag2 = 0x1,
}

class MyAttributeAttribute : Attribute
{
    public MyAttributeAttribute(EEEnum e) { }
    public MyAttributeAttribute(short e) { }
 
    public void Foo(EEEnum e) { }
    public void Foo(short e) { }
 
    [MyAttribute([|(EEEnum)0x0|])]
    public void Bar()
    {
        Foo((EEEnum)0x0);
    }
}
");
        }

J
Jared Parsons 已提交
3377 3378
        [WorkItem(608180, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/608180")]
        [WorkItem(624252, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/624252")]
3379
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3380
        public async Task DontRemoveCastIfArgumentIsRestricted_TypedReference()
3381
        {
3382
            await TestMissingAsync(
3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406
@"
using System;

class Program
{
    static void Main(string[] args)
    {
        
    }

    static void v(dynamic x)
    {
        var y = default(TypedReference);
        dd([|(object)x|], y);
    }

    static void dd(object obj, TypedReference d)
    {

    }
}
");
        }

J
Jared Parsons 已提交
3407
        [WorkItem(627107, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/627107")]
3408
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3409
        public async Task DontRemoveCastOnArgumentsWithOtherDynamicArguments()
3410
        {
3411
            await TestMissingAsync(
3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436
@"
using System;
 
class Program
{
    static void Main()
    {
        C<string>.InvokeFoo(0);
    }
}
 
class C<T>
{
    public static void InvokeFoo(dynamic x)
    {
        Console.WriteLine(Foo(x, [|(object)""""|], """"));
    }
 
    static void Foo(int x, string y, T z) { }
    static bool Foo(int x, object y, object z) { return true; }
}

");
        }

J
Jared Parsons 已提交
3437
        [WorkItem(627107, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/627107")]
3438
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3439
        public async Task DontRemoveCastOnArgumentsWithOtherDynamicArguments_Bracketed()
3440
        {
3441
            await TestMissingAsync(
3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456
@"
class C<T>
{
    int this[int x, T s, string d = ""abc""] { get { return 0; } set { } }

    int this[int x, object s, object d] { get { return 0; } set { } }

    void Foo(dynamic xx)
    {
        var y = this[ x: xx, s: """", d: [|(object)""""|]];
    }
}
");
        }

J
Jared Parsons 已提交
3457
        [WorkItem(627107, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/627107")]
3458
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3459
        public async Task DontRemoveCastOnArgumentsWithDynamicReceiverOpt()
3460
        {
3461
            await TestMissingAsync(
3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473
@"
class C
{
    static bool Foo(dynamic d)
    {
        d([|(object)""""|]);
        return true;
    }
}
");
        }

J
Jared Parsons 已提交
3474
        [WorkItem(627107, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/627107")]
3475
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3476
        public async Task DontRemoveCastOnArgumentsWithDynamicReceiverOpt_1()
3477
        {
3478
            await TestMissingAsync(
3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490
@"
class C
{
    static bool Foo(dynamic d)
    {
        d.foo([|(object)""""|]);
        return true;
    }
}
");
        }

J
Jared Parsons 已提交
3491
        [WorkItem(627107, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/627107")]
3492
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3493
        public async Task DontRemoveCastOnArgumentsWithDynamicReceiverOpt_2()
3494
        {
3495
            await TestMissingAsync(
3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507
@"
class C
{
    static bool Foo(dynamic d)
    {
        d.foo.bar.foo([|(object)""""|]);
        return true;
    }
}
");
        }

J
Jared Parsons 已提交
3508
        [WorkItem(627107, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/627107")]
3509
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3510
        public async Task DontRemoveCastOnArgumentsWithDynamicReceiverOpt_3()
3511
        {
3512
            await TestMissingAsync(
3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524
@"
class C
{
    static bool Foo(dynamic d)
    {
        d.foo().bar().foo([|(object)""""|]);
        return true;
    }
}
");
        }

J
Jared Parsons 已提交
3525
        [WorkItem(627107, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/627107")]
3526
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3527
        public async Task DontRemoveCastOnArgumentsWithOtherDynamicArguments_1()
3528
        {
3529
            await TestMissingAsync(
3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554
@"
using System;
 
class Program
{
    static void Main()
    {
        C<string>.InvokeFoo(0);
    }
}

class C<T>
{
    public static void InvokeFoo(dynamic x)
    {
        Console.WriteLine(Foo([|(object)""""|], x, """"));
    }

    static void Foo(string y, int x,  T z) { }
    static bool Foo(object y, int x,  object z) { return true; }
}

");
        }

J
Jared Parsons 已提交
3555
        [WorkItem(545998, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545998")]
3556
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3557
        public async Task DontRemoveCastWhichWouldChangeAttributeOverloadResolution2()
3558 3559 3560 3561
        {
            // Note: The cast below cannot be removed because it would result in
            // a different attribute constructor being picked

3562
            await TestMissingAsync(
3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573
@"
using System;
 
[A(new[] { [|(long)0|] })]
class A : Attribute
{
    public A(long[] x) { }
}
");
        }

J
Jared Parsons 已提交
3574
        [WorkItem(529894, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529894")]
3575
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3576
        public async Task DontUnnecessaryCastFromEnumToUint()
3577
        {
3578
            await TestMissingAsync(
3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597
@"
using System;

enum E
{
    X = -1
}

class C
{
    static void Main()
    {
        E x = E.X;
        Console.WriteLine([|(uint)|]x > 0);
    }
}
");
        }

J
Jared Parsons 已提交
3598
        [WorkItem(529846, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529846")]
3599
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3600
        public async Task DontUnnecessaryCastFromTypeParameterToObject()
3601
        {
3602
            await TestMissingAsync(
3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614
@"
class C
{
    static void Foo<T>(T x, object y)
    {
        if ([|(object)|]x == y) { }
    }
}

");
        }

J
Jared Parsons 已提交
3615
        [WorkItem(640136, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/640136")]
3616
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3617
        public async Task RemoveUnnecessaryCastAndParseCorrect()
3618
        {
3619
            await TestAsync(
3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648
@"
using System;
using System.Threading.Tasks;
 
class C
{
    void Foo(Task<Action> x)
    {
        (([|(Task<Action>)x|]).Result)();
    }
}
",

@"
using System;
using System.Threading.Tasks;
 
class C
{
    void Foo(Task<Action> x)
    {
        x.Result();
    }
}
",
            index: 0,
            compareTokens: false);
        }

J
Jared Parsons 已提交
3649
        [WorkItem(626026, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/626026")]
3650
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3651
        public async Task DontRemoveCastIfUserDefinedExplicitCast()
3652
        {
3653
            await TestMissingAsync(
3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678
            @"
class Program
{
    static void Main(string[] args)
    {
        B bar = new B();
        A a = [|(A)bar|];
    }
}

public struct A
{
    public static explicit operator A(B b)
    {
        return new A();
    }
}

public struct B
{

}
");
        }

J
Jared Parsons 已提交
3679
        [WorkItem(768895, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/768895")]
3680
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3681
        public async Task DontRemoveNecessaryCastInTernary()
3682
        {
3683
            await TestMissingAsync(
3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695
            @"
class Program
{
    static void Main(string[] args)
    {
        object x = null;
        int y = [|(bool)x|] ? 1 : 0;
    }
}
");
        }

J
Jared Parsons 已提交
3696
        [WorkItem(770187, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/770187")]
3697
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3698
        public async Task DontRemoveNecessaryCastInSwitchExpression()
3699
        {
3700
            await TestMissingAsync(
3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726
            @"
namespace ConsoleApplication23
{
    class Program
    {
        static void Main(string[] args)
        {
            int foo = 0;
            switch ([|(E)foo|])
            {
                case E.A:
                case E.B:
                    return;
            }
        }
    }
    enum E
    {
        A,
        B,
        C
    }
}
");
        }

J
Jared Parsons 已提交
3727
        [WorkItem(844482, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/844482")]
3728
        [WorkItem(2761, "https://github.com/dotnet/roslyn/issues/2761")]
3729
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3730
        public async Task DontRemoveCastFromBaseToDerivedWithExplicitReference()
3731
        {
3732
            await TestMissingAsync(
3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750
@"class Program
{
    static void Main(string[] args)
    {
        C x = null;
        C y = null;
        y = [|(D)x|];
    }
}

class C
{

}

class D : C
{

3751 3752 3753 3754
}");
        }

        [WorkItem(3254, "https://github.com/dotnet/roslyn/issues/3254")]
3755
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3756
        public async Task DontRemoveCastToTypeParameterWithExceptionConstraint()
3757
        {
3758
            await TestMissingAsync(
3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774
@"using System;

class Program
{
    private static void RequiresCondition<TException>(bool condition, string messageOnFalseCondition)
            where TException : Exception
    {
        if (!condition)
        {
            throw [|(TException)Activator.CreateInstance(typeof(TException), messageOnFalseCondition)|];
        }
    }
}");
        }

        [WorkItem(3254, "https://github.com/dotnet/roslyn/issues/3254")]
3775
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3776
        public async Task DontRemoveCastToTypeParameterWithExceptionSubTypeConstraint()
3777
        {
3778
            await TestMissingAsync(
3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790
@"using System;

class Program
{
    private static void RequiresCondition<TException>(bool condition, string messageOnFalseCondition)
            where TException : ArgumentException
    {
        if (!condition)
        {
            throw [|(TException)Activator.CreateInstance(typeof(TException), messageOnFalseCondition)|];
        }
    }
3791
}");
3792
        }
3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809

        [WorkItem(8111, "https://github.com/dotnet/roslyn/issues/8111")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task DontRemoveCastThatChangesShapeOfAnonymousTypeObject()
        {
            await TestMissingAsync(
            @"
class Program
{
    static void Main(string[] args)
    {
        object thing = new { shouldBeAnInt = [|(int)Directions.South|] };
    }
    public enum Directions { North, East, South, West }
}
");
        }
3810 3811
    }
}