RemoveUnnecessaryCastTests.cs 107.0 KB
Newer Older
S
Sam Harwell 已提交
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.Collections.Generic;
4
using System.Threading.Tasks;
5
using Microsoft.CodeAnalysis.CodeFixes;
6
using Microsoft.CodeAnalysis.CSharp;
7 8 9
using Microsoft.CodeAnalysis.CSharp.CodeFixes.RemoveUnnecessaryCast;
using Microsoft.CodeAnalysis.CSharp.Diagnostics.RemoveUnnecessaryCast;
using Microsoft.CodeAnalysis.Diagnostics;
10
using Microsoft.CodeAnalysis.Test.Utilities;
11 12 13 14 15 16 17
using Roslyn.Test.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Diagnostics.RemoveUnnecessaryCast
{
    public partial class RemoveUnnecessaryCastTests : AbstractCSharpDiagnosticProviderBasedUserDiagnosticTest
    {
18 19
        internal override (DiagnosticAnalyzer, CodeFixProvider) CreateDiagnosticProviderAndFixer(Workspace workspace)
            => (new CSharpRemoveUnnecessaryCastDiagnosticAnalyzer(), new RemoveUnnecessaryCastCodeFixProvider());
20

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

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

            @"
class Program
{
    static void Main()
    {
        int x = 2;
        int i = 1;
63
        Goo(x < (i), x > (2 + 3));
64 65
    }
 
66
    static void Goo(bool a, bool b) { }
67
}");
68 69
        }

J
Jared Parsons 已提交
70
        [WorkItem(545146, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545146")]
71
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
72
        public async Task ParenthesizeToKeepParseTheSame2()
73
        {
C
CyrusNajmabadi 已提交
74
            await TestInRegularAndScriptAsync(
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
            @"
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();
    }
97
}");
98 99
        }

J
Jared Parsons 已提交
100
        [WorkItem(545160, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545160")]
101
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
102
        public async Task ParenthesizeToKeepParseTheSame3()
103
        {
C
CyrusNajmabadi 已提交
104
            await TestInRegularAndScriptAsync(
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
            @"
using System;
 
class Program
{
    static void Main()
    {
        var x = (Decimal)[|(int)-1|];
    }
}",

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

J
Jared Parsons 已提交
128
        [WorkItem(545138, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545138")]
129
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
130
        public async Task DontRemoveTypeParameterCastToObject()
131
        {
C
CyrusNajmabadi 已提交
132
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
133
@"class С
134
{
135
    void Goo<T>(T obj)
C
CyrusNajmabadi 已提交
136 137
{
    int x = (int)[|(object)obj|];
138
}
C
CyrusNajmabadi 已提交
139
}");
140 141
        }

J
Jared Parsons 已提交
142
        [WorkItem(545139, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545139")]
143
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
144
        public async Task DontRemoveCastInIsTest()
145
        {
C
CyrusNajmabadi 已提交
146
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
147
@"using System;
148 149 150 151

class С
{
    static void Main()
C
CyrusNajmabadi 已提交
152 153 154 155
{
    DayOfWeek[] a = {
    };
    Console.WriteLine([|(object)a|] is int[]);
156
}
C
CyrusNajmabadi 已提交
157
}");
158 159
        }

J
Jared Parsons 已提交
160
        [WorkItem(545142, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545142")]
161
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
162
        public async Task DontRemoveCastNeedForUserDefinedOperator()
163
        {
C
CyrusNajmabadi 已提交
164
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
165
@"class A
166 167 168 169 170 171
{
    public static implicit operator A(string x)
    {
        return new A();
    }
}
C
CyrusNajmabadi 已提交
172

173 174 175 176 177 178
class Program
{
    static void Main()
    {
        A x = [|(string)null|];
    }
C
CyrusNajmabadi 已提交
179
}");
180 181
        }

J
Jared Parsons 已提交
182
        [WorkItem(545143, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545143")]
183
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
184
        public async Task DontRemovePointerCast1()
185
        {
C
CyrusNajmabadi 已提交
186
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
187
@"unsafe class C
188 189 190 191 192
{
    static unsafe void Main()
    {
        var x = (int)[|(int*)null|];
    }
C
CyrusNajmabadi 已提交
193
}");
194 195
        }

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

C
CyrusNajmabadi 已提交
203
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
204
@"using System;
205 206 207 208 209 210 211 212 213

class Program
{
    static void Main()
    {
        Action a = Console.WriteLine;
        Action b = Console.WriteLine;
        Console.WriteLine(a == [|(object)b|]);
    }
C
CyrusNajmabadi 已提交
214
}");
215 216
        }

J
Jared Parsons 已提交
217
        [WorkItem(545145, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545145")]
218
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
219
        public async Task DontRemoveCastToAnonymousMethodWhenOnLeftOfAsCast()
220
        {
C
CyrusNajmabadi 已提交
221
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
222
@"using System;
223 224 225 226 227

class C
{
    static void Main()
    {
228
        var x = [|(Action)delegate {
C
CyrusNajmabadi 已提交
229 230 231
        }|]

        as Action;
232
    }
C
CyrusNajmabadi 已提交
233
}");
234 235
        }

J
Jared Parsons 已提交
236
        [WorkItem(545147, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545147")]
237
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
238
        public async Task DontRemoveCastInFloatingPointOperation()
239
        {
C
CyrusNajmabadi 已提交
240
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
241
@"class C
242 243 244 245 246 247
{
    static void Main()
    {
        int x = 1;
        double y = [|(double)x|] / 2;
    }
C
CyrusNajmabadi 已提交
248
}");
249 250
        }

J
Jared Parsons 已提交
251
        [WorkItem(545157, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545157")]
252
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
253
        public async Task DontRemoveIdentityCastWhichAffectsOverloadResolution1()
254
        {
C
CyrusNajmabadi 已提交
255
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
256 257
@"using System;

258 259 260 261
class Program
{
    static void Main()
    {
262
        Goo(x => [|(int)x|]);
263
    }
C
CyrusNajmabadi 已提交
264

265
    static void Goo(Func<int, object> x)
C
CyrusNajmabadi 已提交
266 267 268
    {
    }

269
    static void Goo(Func<string, object> x)
C
CyrusNajmabadi 已提交
270 271 272
    {
    }
}");
273 274
        }

J
Jared Parsons 已提交
275
        [WorkItem(545158, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545158")]
276
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
277
        public async Task DontRemoveIdentityCastWhichAffectsOverloadResolution2()
278
        {
C
CyrusNajmabadi 已提交
279
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
280 281
@"using System;

282 283 284 285 286
class Program
{
    static void Main()
    {
        var x = [|(IComparable<int>)1|];
287
        Goo(x);
288
    }
C
CyrusNajmabadi 已提交
289

290
    static void Goo(IComparable<int> x)
C
CyrusNajmabadi 已提交
291 292 293
    {
    }

294
    static void Goo(int x)
C
CyrusNajmabadi 已提交
295 296 297
    {
    }
}");
298 299
        }

J
Jared Parsons 已提交
300
        [WorkItem(545158, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545158")]
301
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
302
        public async Task DontRemoveIdentityCastWhichAffectsOverloadResolution3()
303
        {
C
CyrusNajmabadi 已提交
304
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
305 306
@"using System;

307 308 309 310 311 312
class Program
{
    static void Main()
    {
        var x = [|(IComparable<int>)1|];
        var y = x;
313
        Goo(y);
314
    }
C
CyrusNajmabadi 已提交
315

316
    static void Goo(IComparable<int> x)
C
CyrusNajmabadi 已提交
317 318 319
    {
    }

320
    static void Goo(int x)
C
CyrusNajmabadi 已提交
321 322 323
    {
    }
}");
324 325
        }

J
Jared Parsons 已提交
326
        [WorkItem(545747, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545747")]
327
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
328
        public async Task DontRemoveCastWhichChangesTypeOfInferredLocal()
329
        {
C
CyrusNajmabadi 已提交
330
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
331
@"class C
332 333 334 335 336 337
{
    static void Main()
    {
        var x = [|(long)1|];
        x = long.MaxValue;
    }
C
CyrusNajmabadi 已提交
338
}");
339 340
        }

J
Jared Parsons 已提交
341
        [WorkItem(545159, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545159")]
342
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
343
        public async Task DontRemoveNeededCastToIListOfObject()
344
        {
C
CyrusNajmabadi 已提交
345
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
346
@"using System;
347
using System.Collections.Generic;
C
CyrusNajmabadi 已提交
348

349 350 351 352
class C
{
    static void Main()
    {
C
CyrusNajmabadi 已提交
353 354
        Action<object>[] x = {
        };
355
        Goo(x);
356
    }
C
CyrusNajmabadi 已提交
357

358
    static void Goo<T>(Action<T>[] x)
359 360 361 362
    {
        var y = (IList<Action<object>>)[|(IList<object>)x|];
        Console.WriteLine(y.Count);
    }
C
CyrusNajmabadi 已提交
363
}");
364 365
        }

J
Jared Parsons 已提交
366
        [WorkItem(545287, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545287"), WorkItem(880752, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/880752")]
367
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
368
        public async Task RemoveUnneededCastInParameterDefaultValue()
369
        {
C
CyrusNajmabadi 已提交
370
            await TestInRegularAndScriptAsync(
371 372 373 374 375 376 377 378 379 380 381 382 383 384
            @"
class Program
{
    static void M1(int? i1 = [|(int?)null|])
    {
    }
}",

@"
class Program
{
    static void M1(int? i1 = null)
    {
    }
385
}");
386 387
        }

J
Jared Parsons 已提交
388
        [WorkItem(545289, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545289")]
389
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
390
        public async Task RemoveUnneededCastInReturnStatement()
391
        {
C
CyrusNajmabadi 已提交
392
            await TestInRegularAndScriptAsync(
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
            @"
class Program
{
    static long M2()
    {
        return [|(long)5|];
    }
}",

@"
class Program
{
    static long M2()
    {
        return 5;
    }
409
}");
410 411
        }

J
Jared Parsons 已提交
412
        [WorkItem(545288, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545288")]
413
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
414
        public async Task RemoveUnneededCastInLambda1()
415
        {
C
CyrusNajmabadi 已提交
416
            await TestInRegularAndScriptAsync(
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
            @"
using System;
class Program
{
    static void M1()
    {
        Func<long> f1 = () => [|(long)5|];
    }
}",

@"
using System;
class Program
{
    static void M1()
    {
        Func<long> f1 = () => 5;
    }
435
}");
436 437
        }

J
Jared Parsons 已提交
438
        [WorkItem(545288, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545288")]
439
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
440
        public async Task RemoveUnneededCastInLambda2()
441
        {
C
CyrusNajmabadi 已提交
442
            await TestInRegularAndScriptAsync(
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
            @"
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; };
    }
461
}");
462 463
        }

J
Jared Parsons 已提交
464
        [WorkItem(545288, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545288")]
465
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
466
        public async Task RemoveUnneededCastInLambda3()
467
        {
C
CyrusNajmabadi 已提交
468
            await TestInRegularAndScriptAsync(
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
            @"
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; };
    }
487
}");
488 489
        }

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

@"
using System;
class Program
{
    static void M1()
    {
        Func<long> f1 = _ => 5;
    }
513
}");
514 515
        }

J
Jared Parsons 已提交
516
        [WorkItem(545291, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545291")]
517
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
518
        public async Task RemoveUnneededCastInConditionalExpression1()
519
        {
C
CyrusNajmabadi 已提交
520
            await TestInRegularAndScriptAsync(
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
            @"
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;
    }
541
}");
542 543
        }

J
Jared Parsons 已提交
544
        [WorkItem(545291, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545291")]
545
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
546
        public async Task RemoveUnneededCastInConditionalExpression2()
547
        {
C
CyrusNajmabadi 已提交
548
            await TestInRegularAndScriptAsync(
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
            @"
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;
    }
569
}");
570 571
        }

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 RemoveUnneededCastInConditionalExpression3()
575
        {
C
CyrusNajmabadi 已提交
576
            await TestInRegularAndScriptAsync(
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
            @"
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;
    }
597
}");
598 599
        }

J
Jared Parsons 已提交
600
        [WorkItem(545291, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545291")]
601
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
602
        public async Task DontRemoveNeededCastInConditionalExpression()
603
        {
C
CyrusNajmabadi 已提交
604
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
605
@"class Test
606 607 608 609 610 611 612 613 614
{
    public static void Main()
    {
        int b = 5;
        var f1 = (b == 5) ? 4 : [|(long)5|];
    }
}");
        }

J
Jared Parsons 已提交
615
        [WorkItem(545291, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545291")]
616
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
617
        public async Task RemoveUnneededCastInConditionalExpression4()
618
        {
C
CyrusNajmabadi 已提交
619
            await TestInRegularAndScriptAsync(
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
            @"
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;
    }
640
}");
641 642
        }

J
Jared Parsons 已提交
643
        [WorkItem(545459, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545459")]
644
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
645
        public async Task RemoveUnneededCastInsideADelegateConstructor()
646
        {
C
CyrusNajmabadi 已提交
647
            await TestInRegularAndScriptAsync(
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
            @"
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) { }
674
}");
675 676
        }

J
Jared Parsons 已提交
677
        [WorkItem(545419, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545419")]
678
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
679
        public async Task DontRemoveTriviaWhenRemovingCast()
680
        {
C
CyrusNajmabadi 已提交
681
            await TestInRegularAndScriptAsync(
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
            @"
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*/;
        };
    }
706
}");
707 708
        }

J
Jared Parsons 已提交
709
        [WorkItem(545422, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545422")]
710
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
711
        public async Task RemoveUnneededCastInsideCaseLabel()
712
        {
C
CyrusNajmabadi 已提交
713
            await TestInRegularAndScriptAsync(
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
            @"
class Test
{
    static void Main()
    {
        switch (5L)
        {
            case [|(long)5|]:
                break;
        }
    }
}",

@"
class Test
{
    static void Main()
    {
        switch (5L)
        {
            case 5:
                break;
        }
    }
738
}");
739 740
        }

J
Jared Parsons 已提交
741
        [WorkItem(545578, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545578")]
742
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
743
        public async Task RemoveUnneededCastInsideGotoCaseStatement()
744
        {
C
CyrusNajmabadi 已提交
745
            await TestInRegularAndScriptAsync(
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
            @"
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;
        }
    }
772
}");
773 774
        }

J
Jared Parsons 已提交
775
        [WorkItem(545595, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545595")]
776
        [WpfFact(Skip = "529787"), Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
777
        public async Task RemoveUnneededCastInCollectionInitializer()
778
        {
C
CyrusNajmabadi 已提交
779
            await TestInRegularAndScriptAsync(
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
            @"
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 };
    }
800
}");
801 802
        }

J
Jared Parsons 已提交
803
        [WorkItem(529787, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529787")]
804
        [WpfFact(Skip = "529787"), Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
805
        public async Task DontRemoveNecessaryCastWhichInCollectionInitializer1()
806
        {
C
CyrusNajmabadi 已提交
807
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
808
@"using System;
809 810 811 812
using System.Collections.Generic;

class X : List<int>
{
C
CyrusNajmabadi 已提交
813 814 815 816 817 818 819 820 821 822
    void Add(object x)
    {
        Console.WriteLine(1);
    }

    void Add(string x)
    {
        Console.WriteLine(2);
    }

823 824 825 826
    static void Main()
    {
        var z = new X { [|(object)""""|] };
    }
C
CyrusNajmabadi 已提交
827
}");
828 829
        }

J
Jared Parsons 已提交
830
        [WorkItem(529787, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529787")]
831
        [WpfFact(Skip = "529787"), Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
832
        public async Task DontRemoveNecessaryCastWhichInCollectionInitializer2()
833
        {
C
CyrusNajmabadi 已提交
834
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
835
@"using System;
836 837 838 839
using System.Collections.Generic;

class X : List<int>
{
C
CyrusNajmabadi 已提交
840 841 842 843 844 845 846 847 848
    void Add(object x)
    {
        Console.WriteLine(1);
    }

    void Add(string x)
    {
        Console.WriteLine(2);
    }
849 850 851 852 853

    static void Main()
    {
        X z = new X { [|(object)""""|] };
    }
C
CyrusNajmabadi 已提交
854
}");
855 856
        }

J
Jared Parsons 已提交
857
        [WorkItem(545607, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545607")]
858
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
859
        public async Task RemoveUnneededCastInArrayInitializer()
860
        {
C
CyrusNajmabadi 已提交
861
            await TestInRegularAndScriptAsync(
862 863 864
            @"
class X
{
865
    static void Goo()
866 867 868 869 870 871 872 873 874
    {
        string x = "";
        var s = new object[] { [|(object)x|] };
    }
}",

@"
class X
{
875
    static void Goo()
876 877 878 879
    {
        string x = "";
        var s = new object[] { x };
    }
880
}");
881 882
        }

J
Jared Parsons 已提交
883
        [WorkItem(545616, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545616")]
884
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
885
        public async Task RemoveUnneededCastWithOverloadedBinaryOperator()
886
        {
C
CyrusNajmabadi 已提交
887
            await TestInRegularAndScriptAsync(
888 889 890 891
            @"
using System;
class MyAction
{
892
    static void Goo()
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
    {
        MyAction x = null;
        var y = x + [|(Action)delegate { }|];
    }
 
    public static MyAction operator +(MyAction x, Action y)
    {
        throw new NotImplementedException();
    }
}",

@"
using System;
class MyAction
{
908
    static void Goo()
909 910 911 912 913 914 915 916 917
    {
        MyAction x = null;
        var y = x + delegate { };
    }
 
    public static MyAction operator +(MyAction x, Action y)
    {
        throw new NotImplementedException();
    }
918
}");
919 920
        }

J
Jared Parsons 已提交
921
        [WorkItem(545822, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545822")]
922
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
923
        public async Task RemoveUnnecessaryCastShouldInsertWhitespaceWhereNeededToKeepCorrectParsing()
924
        {
C
CyrusNajmabadi 已提交
925
            await TestInRegularAndScriptAsync(
926 927 928 929 930
            @"
using System;
 
class Program
{
931
    static void Goo<T>()
932 933
    {
        Action a = null;
934
        var x = [|(Action)(Goo<Guid>)|]==a;
935 936 937 938 939 940 941 942
    }
}",

@"
using System;
 
class Program
{
943
    static void Goo<T>()
944 945
    {
        Action a = null;
946
        var x = (Goo<Guid>) == a;
947
    }
948
}");
949 950
        }

J
Jared Parsons 已提交
951
        [WorkItem(545560, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545560")]
952
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
953
        public async Task DontRemoveNecessaryCastWithExplicitUserDefinedConversion()
954
        {
C
CyrusNajmabadi 已提交
955
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
956 957
@"using System;

958 959
class A
{
C
CyrusNajmabadi 已提交
960
    public static explicit operator long(A x)
961 962 963 964
    {
        return 1;
    }

C
CyrusNajmabadi 已提交
965
    public static implicit operator int(A x)
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
    {
        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 已提交
981
        [WorkItem(545608, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545608")]
982
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
983
        public async Task DontRemoveNecessaryCastWithImplicitUserDefinedConversion()
984
        {
C
CyrusNajmabadi 已提交
985
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
986
@"class X
987
{
988
    static void Goo()
989 990 991 992
    {
        X x = null;
        object y = [|(string)x|];
    }
C
CyrusNajmabadi 已提交
993 994

    public static implicit operator string(X x)
995 996 997 998 999 1000
    {
        return "";
    }
}");
        }

J
Jared Parsons 已提交
1001
        [WorkItem(545941, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545941")]
1002
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1003
        public async Task DontRemoveNecessaryCastWithImplicitConversionInThrow()
1004
        {
C
Charles Stoner 已提交
1005
            // The cast below can't be removed because the throw statement expects
1006 1007 1008
            // an expression of type Exception -- not an expression convertible to
            // Exception.

C
CyrusNajmabadi 已提交
1009
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
1010
@"using System;
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022

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

    static void Main()
    {
        throw [|(Exception)new E()|];
    }
C
CyrusNajmabadi 已提交
1023
}");
1024 1025
        }

J
Jared Parsons 已提交
1026
        [WorkItem(545981, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545981")]
1027
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1028
        public async Task DontRemoveNecessaryCastInThrow()
1029
        {
C
Charles Stoner 已提交
1030
            // The cast below can't be removed because the throw statement expects
1031 1032 1033
            // an expression of type Exception -- not an expression convertible to
            // Exception.

C
CyrusNajmabadi 已提交
1034
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
1035
@"using System;
1036 1037 1038 1039 1040 1041 1042 1043

class C
{
    static void Main()
    {
        object ex = new Exception();
        throw [|(Exception)ex|];
    }
C
CyrusNajmabadi 已提交
1044
}");
1045 1046
        }

J
Jared Parsons 已提交
1047
        [WorkItem(545941, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545941")]
1048
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1049
        public async Task RemoveUnnecessaryCastInThrow()
1050
        {
C
CyrusNajmabadi 已提交
1051
            await TestInRegularAndScriptAsync(
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
            @"
using System;

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

@"
using System;

class E
{
    static void Main()
    {
        throw new Exception();
    }
}
1074
");
1075 1076
        }

J
Jared Parsons 已提交
1077
        [WorkItem(545945, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545945")]
1078
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1079
        public async Task DontRemoveNecessaryDowncast()
1080
        {
C
CyrusNajmabadi 已提交
1081
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
1082
@"class C
1083
{
1084
    void Goo(object y)
1085 1086 1087
    {
        int x = [|(int)y|];
    }
C
CyrusNajmabadi 已提交
1088
}");
1089 1090
        }

J
Jared Parsons 已提交
1091
        [WorkItem(545591, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545591")]
1092
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1093
        public async Task DontRemoveNecessaryCastWithinLambda()
1094
        {
C
CyrusNajmabadi 已提交
1095
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
1096 1097
@"using System;

1098 1099 1100 1101
class Program
{
    static void Main()
    {
1102
        Boo(x => Goo(x, y => [|(int)x|]), null);
1103
    }
C
CyrusNajmabadi 已提交
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114

    static void Boo(Action<int> x, object y)
    {
        Console.WriteLine(1);
    }

    static void Boo(Action<string> x, string y)
    {
        Console.WriteLine(2);
    }

1115
    static void Goo(int x, Func<int, int> y)
C
CyrusNajmabadi 已提交
1116 1117 1118
    {
    }

1119
    static void Goo(string x, Func<string, string> y)
C
CyrusNajmabadi 已提交
1120 1121 1122
    {
    }

1123
    static void Goo(string x, Func<int, int> y)
C
CyrusNajmabadi 已提交
1124 1125 1126
    {
    }
}");
1127 1128
        }

J
Jared Parsons 已提交
1129
        [WorkItem(545606, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545606")]
1130
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1131
        public async Task DontRemoveNecessaryCastFromNullToTypeParameter()
1132
        {
C
CyrusNajmabadi 已提交
1133
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
1134
@"class X
1135
{
1136
    static void Goo<T, S>() where T : class, S
1137
    {
C
CyrusNajmabadi 已提交
1138
        S y = [|(T)null|];
1139
    }
C
CyrusNajmabadi 已提交
1140
}");
1141 1142
        }

J
Jared Parsons 已提交
1143
        [WorkItem(545744, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545744")]
1144
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1145
        public async Task DontRemoveNecessaryCastInImplicitlyTypedArray()
1146
        {
C
CyrusNajmabadi 已提交
1147
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
1148
@"class X
1149
{
1150
    static void Goo()
1151 1152
    {
        string x = "";
C
CyrusNajmabadi 已提交
1153
        var s = new[] { [|(object)x|] };
1154 1155
        s[0] = 1;
    }
C
CyrusNajmabadi 已提交
1156
}");
1157 1158
        }

J
Jared Parsons 已提交
1159
        [WorkItem(545750, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545750")]
1160
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1161
        public async Task RemoveUnnecessaryCastToBaseType()
1162
        {
C
CyrusNajmabadi 已提交
1163
            await TestInRegularAndScriptAsync(
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
            @"
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 "";
    }
1190
}");
1191 1192
        }

J
Jared Parsons 已提交
1193
        [WorkItem(545855, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545855")]
1194
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1195
        public async Task RemoveUnnecessaryLambdaToDelegateCast()
1196
        {
1197
            await TestAsync(
1198 1199 1200 1201 1202 1203 1204 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 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
            @"
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;
    }
}
",
1247
    parseOptions: null);
1248 1249
        }

J
Jared Parsons 已提交
1250
        [WorkItem(529816, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529816")]
1251
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1252
        public async Task RemoveUnnecessaryCastInQueryExpression()
1253
        {
C
CyrusNajmabadi 已提交
1254
            await TestInRegularAndScriptAsync(
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
            @"
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);
    }
1279
}");
1280 1281
        }

J
Jared Parsons 已提交
1282
        [WorkItem(529816, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529816")]
1283
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1284
        public async Task DontRemoveNecessaryCastInQueryExpression()
1285
        {
C
CyrusNajmabadi 已提交
1286
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
1287
@"using System;
1288 1289 1290

class A
{
C
CyrusNajmabadi 已提交
1291 1292 1293 1294 1295 1296 1297 1298 1299
    int Select(Func<int, long> x)
    {
        return 1;
    }

    int Select(Func<int, int> x)
    {
        return 2;
    }
1300 1301 1302

    static void Main()
    {
C
CyrusNajmabadi 已提交
1303 1304
        Console.WriteLine(from y in new A()
                          select [|(long)0|]);
1305
    }
C
CyrusNajmabadi 已提交
1306
}");
1307 1308
        }

J
Jared Parsons 已提交
1309
        [WorkItem(545848, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545848")]
1310
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1311
        public async Task DontRemoveNecessaryCastInConstructorInitializer()
1312
        {
C
CyrusNajmabadi 已提交
1313
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
1314
@"using System;
1315 1316 1317

class C
{
1318
    static void Goo(int x, Func<int, int> y)
C
CyrusNajmabadi 已提交
1319 1320 1321
    {
    }

1322
    static void Goo(string x, Func<string, string> y)
C
CyrusNajmabadi 已提交
1323 1324
    {
    }
1325

C
CyrusNajmabadi 已提交
1326 1327 1328 1329
    C(Action<int> x, object y)
    {
        Console.WriteLine(1);
    }
1330

C
CyrusNajmabadi 已提交
1331 1332 1333 1334
    C(Action<string> x, string y)
    {
        Console.WriteLine(2);
    }
1335

1336
    C() : this(x => Goo(x, y => [|(int)x|]), null)
C
CyrusNajmabadi 已提交
1337 1338 1339 1340 1341 1342 1343 1344
    {
    }

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

J
Jared Parsons 已提交
1347
        [WorkItem(529831, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529831")]
1348
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1349
        public async Task DontRemoveNecessaryCastFromTypeParameterToInterface()
1350
        {
C
CyrusNajmabadi 已提交
1351
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
1352
@"using System;
1353 1354 1355 1356

interface IIncrementable
{
    int Value { get; }
C
CyrusNajmabadi 已提交
1357

1358 1359 1360 1361 1362 1363
    void Increment();
}

struct S : IIncrementable
{
    public int Value { get; private set; }
C
CyrusNajmabadi 已提交
1364 1365 1366 1367 1368

    public void Increment()
    {
        Value++;
    }
1369 1370
}

C
CyrusNajmabadi 已提交
1371
class C : IIncrementable
1372 1373
{
    public int Value { get; private set; }
C
CyrusNajmabadi 已提交
1374 1375 1376 1377 1378

    public void Increment()
    {
        Value++;
    }
1379 1380 1381 1382 1383 1384
}

static class Program
{
    static void Main()
    {
1385
        Goo(new S(), new C());
1386 1387
    }

1388
    static void Goo<TAny, TClass>(TAny x, TClass y)
1389 1390 1391 1392 1393 1394 1395 1396 1397
        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);
    }
C
CyrusNajmabadi 已提交
1398
}");
1399 1400
        }

J
Jared Parsons 已提交
1401
        [WorkItem(529831, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529831")]
1402
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1403
        public async Task RemoveUnnecessaryCastFromTypeParameterToInterface()
1404
        {
C
CyrusNajmabadi 已提交
1405
            await TestInRegularAndScriptAsync(
1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
            @"
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()
    {
1431
        Goo(new S(), new C());
1432 1433
    }

1434
    static void Goo<TAny, TClass>(TAny x, TClass y) 
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
        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()
    {
1471
        Goo(new S(), new C());
1472 1473
    }

1474
    static void Goo<TAny, TClass>(TAny x, TClass y) 
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484
        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);
    }
}
1485
");
1486 1487
        }

J
Jared Parsons 已提交
1488
        [WorkItem(545877, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545877")]
1489
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1490
        public async Task DontCrashOnIncompleteMethodDeclaration()
1491
        {
C
CyrusNajmabadi 已提交
1492
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
1493
@"using System;
1494 1495 1496 1497 1498 1499

class A
{
    static void Main()
    {
        byte
1500
        Goo(x => 1, [|(byte)1|]);
1501 1502
    }

1503
    static void Goo<T, S>(T x, )
C
CyrusNajmabadi 已提交
1504 1505 1506
    {
    }
}");
1507 1508
        }

J
Jared Parsons 已提交
1509
        [WorkItem(545777, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545777")]
1510
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1511
        public async Task DontRemoveImportantTrailingTrivia()
1512
        {
C
CyrusNajmabadi 已提交
1513
            await TestInRegularAndScriptAsync(
1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
            @"
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;
    }
}
1540
");
1541 1542
        }

J
Jared Parsons 已提交
1543
        [WorkItem(529791, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529791")]
1544
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1545
        public async Task RemoveUnnecessaryCastToNullable1()
1546
        {
C
CyrusNajmabadi 已提交
1547
            await TestInRegularAndScriptAsync(
1548 1549 1550
            @"
class X
{
1551
    static void Goo()
1552 1553 1554 1555 1556 1557 1558 1559 1560 1561
    {
        object x = (string)null;
        object y = [|(int?)null|];
    }
}
",

            @"
class X
{
1562
    static void Goo()
1563 1564 1565 1566 1567
    {
        object x = (string)null;
        object y = null;
    }
}
1568
");
1569 1570
        }

J
Jared Parsons 已提交
1571
        [WorkItem(545842, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545842")]
1572
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1573
        public async Task RemoveUnnecessaryCastToNullable2()
1574
        {
C
CyrusNajmabadi 已提交
1575
            await TestInRegularAndScriptAsync(
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597
            @"
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;
    }
}
1598
");
1599 1600
        }

J
Jared Parsons 已提交
1601
        [WorkItem(545850, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545850")]
1602
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1603
        public async Task RemoveSurroundingParentheses()
1604
        {
C
CyrusNajmabadi 已提交
1605
            await TestInRegularAndScriptAsync(
1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625
            @"
class Program
{
    static void Main()
    {
        int x = 1;
        ([|(int)x|]).ToString();
    }
}
",

            @"
class Program
{
    static void Main()
    {
        int x = 1;
        x.ToString();
    }
}
1626
");
1627 1628
        }

J
Jared Parsons 已提交
1629
        [WorkItem(529846, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529846")]
1630
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1631
        public async Task DontRemoveNecessaryCastFromTypeParameterToObject()
1632
        {
C
CyrusNajmabadi 已提交
1633
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
1634
@"class C
1635
{
1636
    static void Goo<T>(T x, object y)
1637
    {
C
CyrusNajmabadi 已提交
1638 1639 1640
        if ([|(object)x|] == y)
        {
        }
1641
    }
C
CyrusNajmabadi 已提交
1642
}");
1643 1644
        }

J
Jared Parsons 已提交
1645
        [WorkItem(545858, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545858")]
1646
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1647
        public async Task DontRemoveNecessaryCastFromDelegateTypeToMulticastDelegate()
1648
        {
C
CyrusNajmabadi 已提交
1649
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
1650
@"using System;
1651 1652 1653 1654 1655 1656 1657 1658 1659

class C
{
    static void Main()
    {
        Action x = Console.WriteLine;
        Action y = Console.WriteLine;
        Console.WriteLine([|(MulticastDelegate)x|] == y);
    }
C
CyrusNajmabadi 已提交
1660
}");
1661 1662
        }

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

C
CyrusNajmabadi 已提交
1670
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
1671
@"using System;
1672 1673 1674 1675 1676 1677 1678 1679

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

C
CyrusNajmabadi 已提交
1680
    public static implicit operator long(C x)
1681 1682 1683 1684
    {
        return 1;
    }

C
CyrusNajmabadi 已提交
1685
    public static implicit operator int(C x)
1686 1687 1688
    {
        return 2;
    }
C
CyrusNajmabadi 已提交
1689
}");
1690 1691
        }

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

C
CyrusNajmabadi 已提交
1698
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
1699
@"class C
1700 1701 1702 1703 1704
{
    static void Main()
    {
        var a = new int[[|(int)decimal.Zero|]];
    }
C
CyrusNajmabadi 已提交
1705
}");
1706 1707
        }

J
Jared Parsons 已提交
1708
        [WorkItem(529842, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529842")]
1709
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1710
        public async Task DontRemoveNecessaryCastInTernaryExpression()
1711
        {
C
CyrusNajmabadi 已提交
1712
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
1713
@"using System;
1714 1715 1716

class X
{
C
CyrusNajmabadi 已提交
1717
    public static implicit operator string(X x)
1718 1719 1720 1721 1722 1723 1724 1725 1726 1727
    {
        return x.ToString();
    }

    static void Main()
    {
        bool b = true;
        X x = new X();
        Console.WriteLine(b ? [|(string)null|] : x);
    }
C
CyrusNajmabadi 已提交
1728
}");
1729 1730
        }

J
Jared Parsons 已提交
1731
        [WorkItem(545882, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545882"), WorkItem(880752, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/880752")]
1732
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1733
        public async Task RemoveCastInConstructorInitializer1()
1734
        {
C
CyrusNajmabadi 已提交
1735
            await TestInRegularAndScriptAsync(
1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749
@"
class C
{
    C(int x) { }
    C() : this([|(int)1|]) { }
}
",

@"
class C
{
    C(int x) { }
    C() : this(1) { }
}
1750
");
1751 1752
        }

J
Jared Parsons 已提交
1753
        [WorkItem(545958, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545958"), WorkItem(880752, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/880752")]
1754
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1755
        public async Task RemoveCastInConstructorInitializer2()
1756
        {
C
CyrusNajmabadi 已提交
1757
            await TestInRegularAndScriptAsync(
1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777
@"
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("""") { }
}
1778
");
1779 1780
        }

J
Jared Parsons 已提交
1781
        [WorkItem(545957, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545957")]
1782
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1783
        public async Task DontRemoveCastInConstructorInitializer3()
1784
        {
C
CyrusNajmabadi 已提交
1785
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
1786
@"class C
1787
{
C
CyrusNajmabadi 已提交
1788 1789 1790 1791 1792 1793 1794 1795
    C(int x)
    {
    }

    C() : this([|(long)1|])
    {
    }
}");
1796 1797
        }

J
Jared Parsons 已提交
1798
        [WorkItem(545842, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545842")]
1799
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1800
        public async Task RemoveCastToNullableInArithmeticExpression()
1801
        {
C
CyrusNajmabadi 已提交
1802
            await TestInRegularAndScriptAsync(
1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824
@"
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;
    }
}
1825
");
1826 1827
        }

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

C
CyrusNajmabadi 已提交
1835
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
1836
@"using System;
1837 1838 1839 1840 1841 1842 1843 1844

class Program
{
    static void Main()
    {
        object x = 1;
        Console.WriteLine(x == [|(object)1|]);
    }
C
CyrusNajmabadi 已提交
1845
}");
1846 1847
        }

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

C
CyrusNajmabadi 已提交
1854
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
1855 1856
@"using System;

1857 1858 1859 1860 1861 1862
class Program
{
    static void Main()
    {
        ([|(IDisposable)x|]).Dispose();
    }
C
CyrusNajmabadi 已提交
1863
}");
1864 1865
        }

J
Jared Parsons 已提交
1866
        [WorkItem(545944, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545944")]
1867
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1868
        public async Task DontRemoveNecessaryCastBeforePointerDereference1()
1869 1870 1871 1872
        {
            // Note: The cast below can't be removed because it would result in *null,
            // which is illegal.

C
CyrusNajmabadi 已提交
1873
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
1874
@"unsafe class C
1875 1876
{
    int x = *[|(int*)null|];
C
CyrusNajmabadi 已提交
1877
}");
1878 1879
        }

J
Jared Parsons 已提交
1880
        [WorkItem(545978, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545978")]
1881
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1882
        public async Task DontRemoveNecessaryCastBeforePointerDereference2()
1883 1884 1885 1886
        {
            // Note: The cast below can't be removed because it would result in dereferencing
            // void*, which is illegal.

C
CyrusNajmabadi 已提交
1887
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
1888
@"unsafe class C
1889 1890 1891 1892 1893 1894
{
    static void Main()
    {
        void* p = null;
        int x = *[|(int*)p|];
    }
C
CyrusNajmabadi 已提交
1895
}");
1896 1897
        }

1898 1899
        [WorkItem(2691, "https://github.com/dotnet/roslyn/issues/2691")]
        [WorkItem(2987, "https://github.com/dotnet/roslyn/issues/2987")]
1900
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1901
        public async Task DontRemoveNecessaryCastBeforePointerDereference3()
1902 1903 1904 1905
        {
            // Conservatively disable cast simplifications for casts involving pointer conversions.
            // https://github.com/dotnet/roslyn/issues/2987 tracks improving cast simplification for this scenario.

C
CyrusNajmabadi 已提交
1906
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
1907
@"class C
1908 1909 1910 1911 1912
{
    public unsafe float ReadSingle(byte* ptr)
    {
        return *[|(float*)ptr|];
    }
C
CyrusNajmabadi 已提交
1913
}");
1914 1915 1916 1917
        }

        [WorkItem(2691, "https://github.com/dotnet/roslyn/issues/2691")]
        [WorkItem(2987, "https://github.com/dotnet/roslyn/issues/2987")]
1918
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1919
        public async Task DontRemoveNumericCastInUncheckedExpression()
1920 1921 1922 1923
        {
            // Conservatively disable cast simplifications within explicit checked/unchecked expressions.
            // https://github.com/dotnet/roslyn/issues/2987 tracks improving cast simplification for this scenario.

C
CyrusNajmabadi 已提交
1924
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
1925
@"class C
1926 1927 1928 1929 1930 1931 1932 1933 1934 1935
{
    private unsafe readonly byte* _endPointer;
    private unsafe byte* _currentPointer;

    private unsafe void CheckBounds(int byteCount)
    {
        if (unchecked([|(uint)byteCount)|] > (_endPointer - _currentPointer))
        {
        }
    }
C
CyrusNajmabadi 已提交
1936
}");
1937 1938 1939 1940
        }

        [WorkItem(2691, "https://github.com/dotnet/roslyn/issues/2691")]
        [WorkItem(2987, "https://github.com/dotnet/roslyn/issues/2987")]
1941
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1942
        public async Task DontRemoveNumericCastInUncheckedStatement()
1943 1944 1945 1946
        {
            // Conservatively disable cast simplifications within explicit checked/unchecked statements.
            // https://github.com/dotnet/roslyn/issues/2987 tracks improving cast simplification for this scenario.

C
CyrusNajmabadi 已提交
1947
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
1948
@"class C
1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961
{
    private unsafe readonly byte* _endPointer;
    private unsafe byte* _currentPointer;

    private unsafe void CheckBounds(int byteCount)
    {
        unchecked
        {
            if (([|(uint)byteCount)|] > (_endPointer - _currentPointer))
            {
            }
        }
    }
C
CyrusNajmabadi 已提交
1962
}");
1963 1964 1965 1966
        }

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

C
CyrusNajmabadi 已提交
1973
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
1974
@"class C
1975 1976 1977 1978 1979 1980 1981 1982 1983 1984
{
    private unsafe readonly byte* _endPointer;
    private unsafe byte* _currentPointer;

    private unsafe void CheckBounds(int byteCount)
    {
        if (checked([|(uint)byteCount)|] > (_endPointer - _currentPointer))
        {
        }
    }
C
CyrusNajmabadi 已提交
1985
}");
1986 1987 1988 1989
        }

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

C
CyrusNajmabadi 已提交
1996
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
1997
@"class C
1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010
{
    private unsafe readonly byte* _endPointer;
    private unsafe byte* _currentPointer;

    private unsafe void CheckBounds(int byteCount)
    {
        checked
        {
            if (([|(uint)byteCount)|] > (_endPointer - _currentPointer))
            {
            }
        }
    }
C
CyrusNajmabadi 已提交
2011
}");
2012 2013
        }

J
Jared Parsons 已提交
2014
        [WorkItem(545894, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545894")]
2015
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2016
        public async Task DontRemoveNecessaryCastInAttribute()
2017
        {
C
CyrusNajmabadi 已提交
2018
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
2019
@"using System;
2020 2021 2022 2023

[A([|(byte)0)|]]
class A : Attribute
{
C
CyrusNajmabadi 已提交
2024 2025 2026 2027
    public A(object x)
    {
    }
}");
2028 2029 2030 2031
        }

        #region Interface Casts

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

C
CyrusNajmabadi 已提交
2038
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
2039
@"using System;
2040 2041 2042 2043 2044 2045 2046 2047

class X : IDisposable
{
    static void Main()
    {
        X x = new Y();
        ([|(IDisposable)x|]).Dispose();
    }
C
CyrusNajmabadi 已提交
2048

2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060
    public void Dispose()
    {
        Console.WriteLine(""X.Dispose"");
    }
}

class Y : X, IDisposable
{
    void IDisposable.Dispose()
    {
        Console.WriteLine(""Y.Dispose"");
    }
C
CyrusNajmabadi 已提交
2061
}");
2062 2063
        }

J
Jared Parsons 已提交
2064
        [WorkItem(545890, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545890")]
2065
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2066
        public async Task RemoveCastToInterfaceForSealedType1()
2067 2068
        {
            // Note: The cast below can be removed because C is sealed and the
2069
            // unspecified optional parameters of I.Goo() and C.Goo() have the
2070 2071
            // same default values.

C
CyrusNajmabadi 已提交
2072
            await TestInRegularAndScriptAsync(
2073 2074 2075 2076 2077
@"
using System;

interface I
{
2078
    void Goo(int x = 0);
2079 2080 2081 2082
}

sealed class C : I
{
2083
    public void Goo(int x = 0)
2084 2085 2086 2087 2088 2089
    {
        Console.WriteLine(x);
    }

    static void Main()
    {
2090
        ([|(I)new C()|]).Goo();
2091 2092 2093 2094 2095 2096 2097 2098 2099
    }
}
",

@"
using System;

interface I
{
2100
    void Goo(int x = 0);
2101 2102 2103 2104
}

sealed class C : I
{
2105
    public void Goo(int x = 0)
2106 2107 2108 2109 2110 2111
    {
        Console.WriteLine(x);
    }

    static void Main()
    {
2112
        new C().Goo();
2113 2114
    }
}
2115
");
2116 2117
        }

J
Jared Parsons 已提交
2118
        [WorkItem(545890, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545890")]
2119
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2120
        public async Task RemoveCastToInterfaceForSealedType2()
2121 2122 2123 2124
        {
            // Note: The cast below can be removed because C is sealed and the
            // interface member has no parameters.

C
CyrusNajmabadi 已提交
2125
            await TestInRegularAndScriptAsync(
2126 2127 2128 2129 2130
@"
using System;

interface I
{
2131
    string Goo { get; }
2132 2133 2134 2135
}

sealed class C : I
{
2136
    public string Goo
2137 2138 2139 2140 2141 2142 2143 2144 2145
    {
        get
        {
            return ""Nikov Rules"";
        }
    }

    static void Main()
    {
2146
        Console.WriteLine(([|(I)new C()|]).Goo);
2147 2148 2149 2150 2151 2152 2153 2154 2155
    }
}
",

@"
using System;

interface I
{
2156
    string Goo { get; }
2157 2158 2159 2160
}

sealed class C : I
{
2161
    public string Goo
2162 2163 2164 2165 2166 2167 2168 2169 2170
    {
        get
        {
            return ""Nikov Rules"";
        }
    }

    static void Main()
    {
2171
        Console.WriteLine(new C().Goo);
2172 2173
    }
}
2174
");
2175 2176
        }

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

C
CyrusNajmabadi 已提交
2184
            await TestInRegularAndScriptAsync(
2185 2186 2187 2188 2189
@"
using System;

interface I
{
2190
    string Goo { get; }
2191 2192 2193 2194 2195 2196
}

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

2197
    public string Goo
2198 2199 2200 2201 2202 2203 2204 2205 2206
    {
        get
        {
            return ""Nikov Rules"";
        }
    }

    static void Main()
    {
2207
        Console.WriteLine(([|(I)Instance|]).Goo);
2208 2209 2210 2211 2212 2213 2214 2215 2216
    }
}
",

@"
using System;

interface I
{
2217
    string Goo { get; }
2218 2219 2220 2221 2222 2223
}

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

2224
    public string Goo
2225 2226 2227 2228 2229 2230 2231 2232 2233
    {
        get
        {
            return ""Nikov Rules"";
        }
    }

    static void Main()
    {
2234
        Console.WriteLine(Instance.Goo);
2235 2236
    }
}
2237
");
2238 2239
        }

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

C
CyrusNajmabadi 已提交
2247
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
2248
@"using System;
2249 2250 2251

interface I
{
2252
    void Goo(int x = 0);
2253 2254 2255 2256
}

sealed class C : I
{
2257
    public void Goo(int x = 1)
2258 2259 2260 2261 2262 2263
    {
        Console.WriteLine(x);
    }

    static void Main()
    {
2264
        ([|(I)new C()|]).Goo();
2265
    }
C
CyrusNajmabadi 已提交
2266
}");
2267 2268
        }

J
Jared Parsons 已提交
2269
        [WorkItem(545890, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545890")]
2270
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2271
        public async Task RemoveCastToInterfaceForSealedType5()
2272 2273 2274 2275 2276
        {
            // Note: The cast below can be removed (even though C is sealed)
            // because the optional parameters whose default values differ are
            // specified.

C
CyrusNajmabadi 已提交
2277
            await TestInRegularAndScriptAsync(
2278 2279 2280 2281 2282
@"
using System;

interface I
{
2283
    void Goo(int x = 0);
2284 2285 2286 2287
}

sealed class C : I
{
2288
    public void Goo(int x = 1)
2289 2290 2291 2292 2293 2294
    {
        Console.WriteLine(x);
    }

    static void Main()
    {
2295
        ([|(I)new C()|]).Goo(2);
2296 2297 2298 2299 2300 2301 2302 2303 2304
    }
}
",

@"
using System;

interface I
{
2305
    void Goo(int x = 0);
2306 2307 2308 2309
}

sealed class C : I
{
2310
    public void Goo(int x = 1)
2311 2312 2313 2314 2315 2316
    {
        Console.WriteLine(x);
    }

    static void Main()
    {
2317
        new C().Goo(2);
2318 2319
    }
}
2320
");
2321 2322
        }

J
Jared Parsons 已提交
2323
        [WorkItem(545888, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545888")]
2324
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2325
        public async Task DontRemoveCastToInterfaceForSealedType6()
2326 2327 2328 2329 2330
        {
            // 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.

C
CyrusNajmabadi 已提交
2331
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
2332
@"using System;
2333 2334 2335

interface I
{
2336
    void Goo(int x = 0, int y = 0);
2337 2338 2339 2340
}

sealed class C : I
{
2341
    public void Goo(int y = 0, int x = 0)
2342 2343 2344 2345 2346 2347
    {
        Console.WriteLine(x);
    }

    static void Main()
    {
2348
        ([|(I)new C()|]).Goo(x: 1);
2349
    }
C
CyrusNajmabadi 已提交
2350
}");
2351 2352
        }

J
Jared Parsons 已提交
2353
        [WorkItem(545888, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545888")]
2354
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2355
        public async Task RemoveCastToInterfaceForSealedType7()
2356
        {
C
CyrusNajmabadi 已提交
2357
            await TestInRegularAndScriptAsync(
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 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405
@"
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]);
    }
}
2406
");
2407 2408
        }

J
Jared Parsons 已提交
2409
        [WorkItem(545888, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545888")]
2410
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2411
        public async Task DontRemoveCastToInterfaceForSealedType8()
2412 2413 2414 2415 2416
        {
            // 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.

C
CyrusNajmabadi 已提交
2417
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
2418
@"using System;
2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438

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]);
    }
C
CyrusNajmabadi 已提交
2439
}");
2440 2441
        }

J
Jared Parsons 已提交
2442
        [WorkItem(545883, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545883")]
2443
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2444
        public async Task DontRemoveCastToInterfaceForSealedType9()
2445 2446 2447 2448 2449
        {
            // 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().

C
CyrusNajmabadi 已提交
2450
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
2451
@"using System;
2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465
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()"");
    }
C
CyrusNajmabadi 已提交
2466
}");
2467 2468
        }

J
Jared Parsons 已提交
2469
        [WorkItem(545887, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545887")]
2470
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2471
        public async Task DontRemoveCastToInterfaceForStruct1()
2472 2473 2474 2475
        {
            // Note: The cast below can't be removed because the cast boxes 's' and
            // unboxing would change program behavior.

C
CyrusNajmabadi 已提交
2476
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
2477
@"using System;
2478 2479 2480 2481

interface IIncrementable
{
    int Value { get; }
C
CyrusNajmabadi 已提交
2482

2483 2484 2485 2486 2487 2488
    void Increment();
}

struct S : IIncrementable
{
    public int Value { get; private set; }
C
CyrusNajmabadi 已提交
2489 2490 2491 2492 2493

    public void Increment()
    {
        Value++;
    }
2494 2495 2496 2497 2498 2499 2500

    static void Main()
    {
        var s = new S();
        ([|(IIncrementable)s|]).Increment();
        Console.WriteLine(s.Value);
    }
C
CyrusNajmabadi 已提交
2501
}");
2502 2503
        }

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

C
CyrusNajmabadi 已提交
2511
            await TestInRegularAndScriptAsync(
2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547
            @"
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();
    }
}
2548
");
2549 2550
        }

J
Jared Parsons 已提交
2551
        [WorkItem(544655, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/544655")]
2552
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2553
        public async Task RemoveCastToICloneableForDelegate()
2554
        {
C
Charles Stoner 已提交
2555
            // Note: The cast below can be removed because delegates are implicitly
2556 2557
            // sealed.

C
CyrusNajmabadi 已提交
2558
            await TestInRegularAndScriptAsync(
2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582
            @"
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();
    }
}
2583
");
2584 2585
        }

J
Jared Parsons 已提交
2586
        [WorkItem(545926, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545926")]
2587
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2588
        public async Task RemoveCastToICloneableForArray()
2589
        {
C
Charles Stoner 已提交
2590
            // Note: The cast below can be removed because arrays are implicitly
2591 2592
            // sealed.

C
CyrusNajmabadi 已提交
2593
            await TestInRegularAndScriptAsync(
2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617
            @"
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(); 
    }
}
2618
");
2619 2620
        }

J
Jared Parsons 已提交
2621
        [WorkItem(529897, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529897")]
2622
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2623
        public async Task RemoveCastToIConvertibleForEnum()
2624
        {
C
Charles Stoner 已提交
2625
            // Note: The cast below can be removed because enums are implicitly
2626 2627
            // sealed.

C
CyrusNajmabadi 已提交
2628
            await TestInRegularAndScriptAsync(
2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652
            @"
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();
    }
}
2653
");
2654 2655 2656 2657 2658 2659
        }

        #endregion

        #region ParamArray Parameter Casts

J
Jared Parsons 已提交
2660
        [WorkItem(545141, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545141")]
2661
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2662
        public async Task DontRemoveCastToObjectInParamArrayArg1()
2663
        {
C
CyrusNajmabadi 已提交
2664
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
2665
@"using System;
2666 2667 2668 2669 2670

class C
{
    static void Main()
    {
2671
        Goo([|(object)null|]);
2672 2673
    }

2674
    static void Goo(params object[] x)
2675 2676 2677
    {
        Console.WriteLine(x.Length);
    }
C
CyrusNajmabadi 已提交
2678
}");
2679 2680
        }

J
Jared Parsons 已提交
2681
        [WorkItem(529911, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529911")]
2682
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2683
        public async Task DontRemoveCastToIntArrayInParamArrayArg2()
2684
        {
C
CyrusNajmabadi 已提交
2685
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
2686
@"using System;
2687 2688 2689 2690 2691

class C
{
    static void Main()
    {
2692
        Goo([|(int[])null|]);
2693 2694
    }

2695
    static void Goo(params object[] x)
2696 2697 2698
    {
        Console.WriteLine(x.Length);
    }
C
CyrusNajmabadi 已提交
2699
}");
2700 2701
        }

J
Jared Parsons 已提交
2702
        [WorkItem(529911, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529911")]
2703
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2704
        public async Task DontRemoveCastToObjectArrayInParamArrayArg3()
2705
        {
C
CyrusNajmabadi 已提交
2706
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
2707
@"using System;
2708 2709 2710 2711 2712

class C
{
    static void Main()
    {
2713
        Goo([|(object[])null|]);
2714 2715
    }

2716
    static void Goo(params object[][] x)
2717 2718 2719
    {
        Console.WriteLine(x.Length);
    }
C
CyrusNajmabadi 已提交
2720
}");
2721 2722
        }

J
Jared Parsons 已提交
2723
        [WorkItem(529911, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529911")]
2724
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2725
        public async Task RemoveCastToObjectArrayInParamArrayArg1()
2726
        {
C
CyrusNajmabadi 已提交
2727
            await TestInRegularAndScriptAsync(
2728 2729 2730
            @"
class C
{
2731
    static void Goo(params object[] x) { }
2732 2733 2734

    static void Main()
    {
2735
        Goo([|(object[])null|]);
2736 2737 2738 2739 2740 2741 2742
    }
}
",

@"
class C
{
2743
    static void Goo(params object[] x) { }
2744 2745 2746

    static void Main()
    {
2747
        Goo(null);
2748 2749
    }
}
2750
");
2751 2752
        }

J
Jared Parsons 已提交
2753
        [WorkItem(529911, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529911")]
2754
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2755
        public async Task RemoveCastToStringArrayInParamArrayArg2()
2756
        {
C
CyrusNajmabadi 已提交
2757
            await TestInRegularAndScriptAsync(
2758 2759 2760
            @"
class C
{
2761
    static void Goo(params object[] x) { }
2762 2763 2764

    static void Main()
    {
2765
        Goo([|(string[])null|]);
2766 2767 2768 2769 2770 2771 2772
    }
}
",

@"
class C
{
2773
    static void Goo(params object[] x) { }
2774 2775 2776

    static void Main()
    {
2777
        Goo(null);
2778 2779
    }
}
2780
");
2781 2782
        }

J
Jared Parsons 已提交
2783
        [WorkItem(529911, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529911")]
2784
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2785
        public async Task RemoveCastToIntArrayInParamArrayArg3()
2786
        {
C
CyrusNajmabadi 已提交
2787
            await TestInRegularAndScriptAsync(
2788 2789 2790
            @"
class C
{
2791
    static void Goo(params int[] x) { }
2792 2793 2794

    static void Main()
    {
2795
        Goo([|(int[])null|]);
2796 2797 2798 2799 2800 2801 2802
    }
}
",

@"
class C
{
2803
    static void Goo(params int[] x) { }
2804 2805 2806

    static void Main()
    {
2807
        Goo(null);
2808 2809
    }
}
2810
");
2811 2812
        }

J
Jared Parsons 已提交
2813
        [WorkItem(529911, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529911")]
2814
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2815
        public async Task RemoveCastToObjectArrayInParamArrayArg4()
2816
        {
C
CyrusNajmabadi 已提交
2817
            await TestInRegularAndScriptAsync(
2818 2819 2820
            @"
class C
{
2821
    static void Goo(params object[] x) { }
2822 2823 2824

    static void Main()
    {
2825
        Goo([|(object[])null|], null);
2826 2827 2828 2829 2830 2831 2832
    }
}
",

@"
class C
{
2833
    static void Goo(params object[] x) { }
2834 2835 2836

    static void Main()
    {
2837
        Goo(null, null);
2838 2839
    }
}
2840
");
2841 2842
        }

J
Jared Parsons 已提交
2843
        [WorkItem(529911, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529911")]
2844
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2845
        public async Task RemoveCastToObjectInParamArrayArg5()
2846
        {
C
CyrusNajmabadi 已提交
2847
            await TestInRegularAndScriptAsync(
2848 2849 2850
            @"
class C
{
2851
    static void Goo(params object[] x) { }
2852 2853 2854

    static void Main()
    {
2855
        Goo([|(object)null|], null);
2856 2857 2858 2859 2860 2861 2862
    }
}
",

@"
class C
{
2863
    static void Goo(params object[] x) { }
2864 2865 2866

    static void Main()
    {
2867
        Goo(null, null);
2868 2869
    }
}
2870
");
2871 2872
        }

2873
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2874
        public async Task RemoveCastToObjectArrayInParamArrayWithNamedArgument()
2875
        {
C
CyrusNajmabadi 已提交
2876
            await TestInRegularAndScriptAsync(
2877 2878 2879 2880 2881
                @"
class C
{
    static void Main()
    {
2882
        Goo(x: [|(object[])null|]);
2883 2884
    }

2885
    static void Goo(params object[] x) { }
2886 2887 2888 2889 2890 2891 2892
}
",
                @"
class C
{
    static void Main()
    {
2893
        Goo(x: null);
2894 2895
    }

2896
    static void Goo(params object[] x) { }
2897
}
2898
");
2899 2900 2901 2902 2903 2904
        }

        #endregion

        #region ForEach Statements

J
Jared Parsons 已提交
2905
        [WorkItem(545961, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545961")]
2906
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2907
        public async Task DontRemoveNecessaryCastInForEach1()
2908 2909 2910 2911
        {
            // The cast below can't be removed because it would result an error
            // in the foreach statement.

C
CyrusNajmabadi 已提交
2912
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
2913
@"using System.Collections;
2914 2915 2916 2917 2918 2919

class Program
{
    static void Main()
    {
        object s = "";
C
CyrusNajmabadi 已提交
2920 2921 2922
        foreach (object x in [|(IEnumerable)s|])
        {
        }
2923
    }
C
CyrusNajmabadi 已提交
2924
}");
2925 2926
        }

J
Jared Parsons 已提交
2927
        [WorkItem(545961, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545961")]
2928
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2929
        public async Task DontRemoveNecessaryCastInForEach2()
2930 2931 2932 2933
        {
            // The cast below can't be removed because it would result an error
            // in the foreach statement.

C
CyrusNajmabadi 已提交
2934
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
2935
@"using System.Collections.Generic;
2936 2937 2938 2939 2940 2941

class Program
{
    static void Main()
    {
        object s = "";
C
CyrusNajmabadi 已提交
2942 2943 2944
        foreach (object x in [|(IEnumerable<char>)s|])
        {
        }
2945
    }
C
CyrusNajmabadi 已提交
2946
}");
2947 2948
        }

J
Jared Parsons 已提交
2949
        [WorkItem(545961, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545961")]
2950
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2951
        public async Task DontRemoveNecessaryCastInForEach3()
2952 2953 2954 2955 2956
        {
            // 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.

C
CyrusNajmabadi 已提交
2957
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
2958
@"using System.Collections;
2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977

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 = "";
C
CyrusNajmabadi 已提交
2978 2979 2980
        foreach (object x in [|(D)new C()|])
        {
        }
2981 2982 2983 2984
    }
}");
        }

J
Jared Parsons 已提交
2985
        [WorkItem(545961, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545961")]
2986
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2987
        public async Task DontRemoveNecessaryCastInForEach4()
2988 2989 2990 2991
        {
            // The cast below can't be removed because it would result in
            // C.GetEnumerator() being called rather than D.GetEnumerator().

C
CyrusNajmabadi 已提交
2992
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
2993
@"using System;
2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026
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 已提交
3027
        [WorkItem(545961, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545961")]
3028
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3029
        public async Task DontRemoveNecessaryCastInForEach5()
3030 3031 3032 3033
        {
            // The cast below can't be removed because it would change the
            // type of 'x'.

C
CyrusNajmabadi 已提交
3034
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3035 3036
@"using System;

3037 3038 3039 3040
class Program
{
    static void Main()
    {
C
CyrusNajmabadi 已提交
3041 3042 3043
        string[] s = {
            ""A""
        };
3044 3045 3046 3047 3048 3049
        foreach (var x in [|(Array)s|])
        {
            var y = x;
            y = 1;
        }
    }
C
CyrusNajmabadi 已提交
3050
}");
3051 3052 3053 3054
        }

        #endregion

J
Jared Parsons 已提交
3055
        [WorkItem(545925, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545925")]
3056
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3057
        public async Task DontRemoveCastIfOverriddenMethodHasIncompatibleParameterList()
3058 3059
        {
            // Note: The cast below can't be removed because the parameter list
3060
            // of Goo and its override have different default values.
3061

C
CyrusNajmabadi 已提交
3062
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3063
@"using System;
3064 3065 3066

abstract class Y
{
3067
    public abstract void Goo(int x = 1);
3068 3069 3070 3071 3072 3073
}

class X : Y
{
    static void Main()
    {
3074
        ([|(Y)new X()|]).Goo();
3075 3076
    }

3077
    public override void Goo(int x = 2)
3078 3079 3080
    {
        Console.WriteLine(x);
    }
C
CyrusNajmabadi 已提交
3081
}");
3082 3083
        }

J
Jared Parsons 已提交
3084
        [WorkItem(545925, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545925")]
3085
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3086
        public async Task RemoveCastIfOverriddenMethodHaveCompatibleParameterList()
3087 3088
        {
            // Note: The cast below can be removed because the parameter list
3089
            // of Goo and its override have the same default values.
3090

C
CyrusNajmabadi 已提交
3091
            await TestInRegularAndScriptAsync(
3092 3093 3094 3095 3096
@"
using System;

abstract class Y
{
3097
    public abstract void Goo(int x = 1);
3098 3099 3100 3101 3102 3103
}

class X : Y
{
    static void Main()
    {
3104
        ([|(Y)new X()|]).Goo();
3105 3106
    }

3107
    public override void Goo(int x = 1)
3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118
    {
        Console.WriteLine(x);
    }
}
",

@"
using System;

abstract class Y
{
3119
    public abstract void Goo(int x = 1);
3120 3121 3122 3123 3124 3125
}

class X : Y
{
    static void Main()
    {
3126
        new X().Goo();
3127 3128
    }

3129
    public override void Goo(int x = 1)
3130 3131 3132 3133
    {
        Console.WriteLine(x);
    }
}
3134
");
3135 3136
        }

J
Jared Parsons 已提交
3137
        [WorkItem(529916, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529916")]
3138
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3139
        public async Task RemoveCastInReceiverForMethodGroup()
3140 3141 3142 3143
        {
            // Note: The cast below can be removed because the it results in
            // the same method group.

C
CyrusNajmabadi 已提交
3144
            await TestInRegularAndScriptAsync(
3145 3146 3147 3148 3149 3150 3151
@"
using System;

static class Program
{
    static void Main()
    {
3152
        Action a = ([|(string)""""|]).Goo;
3153 3154
    }

3155
    static void Goo(this string x) { }
3156 3157 3158 3159 3160 3161 3162 3163 3164 3165
}
",

@"
using System;

static class Program
{
    static void Main()
    {
3166
        Action a = """".Goo;
3167 3168
    }

3169
    static void Goo(this string x) { }
3170
}
3171
");
3172 3173
        }

J
Jared Parsons 已提交
3174
        [WorkItem(609497, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/609497")]
3175
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3176
        public async Task Bugfix_609497()
3177
        {
C
CyrusNajmabadi 已提交
3178
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3179
@"using System;
3180
using System.Threading.Tasks;
C
CyrusNajmabadi 已提交
3181

3182 3183 3184 3185
class Program
{
    static void Main()
    {
3186
        Goo().Wait();
3187
    }
C
CyrusNajmabadi 已提交
3188

3189
    static async Task Goo()
3190 3191 3192 3193
    {
        Task task = Task.FromResult(0);
        Console.WriteLine(await [|(dynamic)task|]);
    }
C
CyrusNajmabadi 已提交
3194
}");
3195 3196
        }

J
Jared Parsons 已提交
3197
        [WorkItem(545995, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545995")]
3198
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3199
        public async Task DontRemoveCastToDifferentTypeWithSameName()
3200 3201 3202 3203
        {
            // Note: The cast below cannot be removed because the it results in
            // a different overload being picked.

C
CyrusNajmabadi 已提交
3204
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3205
@"using System;
3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220
using MyInt = System.Int32;

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

class A
{
3221
    static void Goo(int x)
C
CyrusNajmabadi 已提交
3222 3223 3224 3225
    {
        Console.WriteLine(""int"");
    }

3226
    static void Goo(MyInt x)
C
CyrusNajmabadi 已提交
3227 3228 3229 3230
    {
        Console.WriteLine(""MyInt"");
    }

3231 3232
    static void Main()
    {
3233
        Goo([|(MyInt)0|]);
3234
    }
C
CyrusNajmabadi 已提交
3235
}");
3236 3237
        }

J
Jared Parsons 已提交
3238
        [WorkItem(545921, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545921")]
3239
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3240
        public async Task DontRemoveCastWhichWouldChangeAttributeOverloadResolution1()
3241 3242 3243 3244
        {
            // Note: The cast below cannot be removed because it would result in
            // a different attribute constructor being picked

C
CyrusNajmabadi 已提交
3245
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3246
@"using System;
3247 3248 3249 3250 3251 3252 3253 3254 3255 3256

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

class MyAttributeAttribute : Attribute
{
C
CyrusNajmabadi 已提交
3257 3258 3259 3260 3261 3262 3263 3264
    public MyAttributeAttribute(EEEnum e)
    {
    }

    public MyAttributeAttribute(short e)
    {
    }

3265
    public void Goo(EEEnum e)
C
CyrusNajmabadi 已提交
3266 3267 3268
    {
    }

3269
    public void Goo(short e)
C
CyrusNajmabadi 已提交
3270 3271 3272
    {
    }

3273 3274 3275
    [MyAttribute([|(EEEnum)0x0|])]
    public void Bar()
    {
3276
        Goo((EEEnum)0x0);
3277
    }
C
CyrusNajmabadi 已提交
3278
}");
3279 3280
        }

J
Jared Parsons 已提交
3281 3282
        [WorkItem(608180, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/608180")]
        [WorkItem(624252, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/624252")]
3283
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3284
        public async Task DontRemoveCastIfArgumentIsRestricted_TypedReference()
3285
        {
C
CyrusNajmabadi 已提交
3286
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3287
@"using System;
3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303

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)
    {
    }
C
CyrusNajmabadi 已提交
3304
}");
3305 3306
        }

J
Jared Parsons 已提交
3307
        [WorkItem(627107, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/627107")]
3308
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3309
        public async Task DontRemoveCastOnArgumentsWithOtherDynamicArguments()
3310
        {
C
CyrusNajmabadi 已提交
3311
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3312 3313
@"using System;

3314 3315 3316 3317
class Program
{
    static void Main()
    {
3318
        C<string>.InvokeGoo(0);
3319 3320
    }
}
C
CyrusNajmabadi 已提交
3321

3322 3323
class C<T>
{
3324
    public static void InvokeGoo(dynamic x)
3325
    {
3326
        Console.WriteLine(Goo(x, [|(object)""""|], """"));
3327 3328
    }

3329
    static void Goo(int x, string y, T z)
C
CyrusNajmabadi 已提交
3330 3331 3332
    {
    }

3333
    static bool Goo(int x, object y, object z)
C
CyrusNajmabadi 已提交
3334 3335 3336 3337
    {
        return true;
    }
}");
3338 3339
        }

J
Jared Parsons 已提交
3340
        [WorkItem(627107, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/627107")]
3341
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3342
        public async Task DontRemoveCastOnArgumentsWithOtherDynamicArguments_Bracketed()
3343
        {
C
CyrusNajmabadi 已提交
3344
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3345
@"class C<T>
3346
{
C
CyrusNajmabadi 已提交
3347 3348 3349 3350 3351 3352
    int this[int x, T s, string d = ""abc""]
    {
        get
        {
            return 0;
        }
3353

C
CyrusNajmabadi 已提交
3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369
        set
        {
        }
    }

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

        set
        {
        }
    }
3370

3371
    void Goo(dynamic xx)
3372
    {
C
CyrusNajmabadi 已提交
3373
        var y = this[x: xx, s: """", d: [|(object)""""|]];
3374
    }
C
CyrusNajmabadi 已提交
3375
}");
3376 3377
        }

J
Jared Parsons 已提交
3378
        [WorkItem(627107, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/627107")]
3379
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3380
        public async Task DontRemoveCastOnArgumentsWithDynamicReceiverOpt()
3381
        {
C
CyrusNajmabadi 已提交
3382
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3383
@"class C
3384
{
3385
    static bool Goo(dynamic d)
3386 3387 3388 3389
    {
        d([|(object)""""|]);
        return true;
    }
C
CyrusNajmabadi 已提交
3390
}");
3391 3392
        }

J
Jared Parsons 已提交
3393
        [WorkItem(627107, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/627107")]
3394
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3395
        public async Task DontRemoveCastOnArgumentsWithDynamicReceiverOpt_1()
3396
        {
C
CyrusNajmabadi 已提交
3397
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3398
@"class C
3399
{
3400
    static bool Goo(dynamic d)
3401
    {
3402
        d.goo([|(object)""""|]);
3403 3404
        return true;
    }
C
CyrusNajmabadi 已提交
3405
}");
3406 3407
        }

J
Jared Parsons 已提交
3408
        [WorkItem(627107, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/627107")]
3409
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3410
        public async Task DontRemoveCastOnArgumentsWithDynamicReceiverOpt_2()
3411
        {
C
CyrusNajmabadi 已提交
3412
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3413
@"class C
3414
{
3415
    static bool Goo(dynamic d)
3416
    {
3417
        d.goo.bar.goo([|(object)""""|]);
3418 3419
        return true;
    }
C
CyrusNajmabadi 已提交
3420
}");
3421 3422
        }

J
Jared Parsons 已提交
3423
        [WorkItem(627107, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/627107")]
3424
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3425
        public async Task DontRemoveCastOnArgumentsWithDynamicReceiverOpt_3()
3426
        {
C
CyrusNajmabadi 已提交
3427
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3428
@"class C
3429
{
3430
    static bool Goo(dynamic d)
3431
    {
3432
        d.goo().bar().goo([|(object)""""|]);
3433 3434
        return true;
    }
C
CyrusNajmabadi 已提交
3435
}");
3436 3437
        }

J
Jared Parsons 已提交
3438
        [WorkItem(627107, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/627107")]
3439
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3440
        public async Task DontRemoveCastOnArgumentsWithOtherDynamicArguments_1()
3441
        {
C
CyrusNajmabadi 已提交
3442
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3443 3444
@"using System;

3445 3446 3447 3448
class Program
{
    static void Main()
    {
3449
        C<string>.InvokeGoo(0);
3450 3451 3452 3453 3454
    }
}

class C<T>
{
3455
    public static void InvokeGoo(dynamic x)
3456
    {
3457
        Console.WriteLine(Goo([|(object)""""|], x, """"));
3458 3459
    }

3460
    static void Goo(string y, int x, T z)
C
CyrusNajmabadi 已提交
3461 3462
    {
    }
3463

3464
    static bool Goo(object y, int x, object z)
C
CyrusNajmabadi 已提交
3465 3466 3467 3468
    {
        return true;
    }
}");
3469 3470
        }

J
Jared Parsons 已提交
3471
        [WorkItem(545998, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545998")]
3472
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3473
        public async Task DontRemoveCastWhichWouldChangeAttributeOverloadResolution2()
3474 3475 3476 3477
        {
            // Note: The cast below cannot be removed because it would result in
            // a different attribute constructor being picked

C
CyrusNajmabadi 已提交
3478
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3479 3480
@"using System;

3481 3482 3483
[A(new[] { [|(long)0|] })]
class A : Attribute
{
C
CyrusNajmabadi 已提交
3484 3485 3486 3487
    public A(long[] x)
    {
    }
}");
3488 3489
        }

J
Jared Parsons 已提交
3490
        [WorkItem(529894, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529894")]
3491
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3492
        public async Task DontUnnecessaryCastFromEnumToUint()
3493
        {
C
CyrusNajmabadi 已提交
3494
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3495
@"using System;
3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508

enum E
{
    X = -1
}

class C
{
    static void Main()
    {
        E x = E.X;
        Console.WriteLine([|(uint)|]x > 0);
    }
C
CyrusNajmabadi 已提交
3509
}");
3510 3511
        }

J
Jared Parsons 已提交
3512
        [WorkItem(529846, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529846")]
3513
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3514
        public async Task DontUnnecessaryCastFromTypeParameterToObject()
3515
        {
C
CyrusNajmabadi 已提交
3516
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3517
@"class C
3518
{
3519
    static void Goo<T>(T x, object y)
3520
    {
C
CyrusNajmabadi 已提交
3521 3522 3523
        if ([|(object)|]x == y)
        {
        }
3524
    }
C
CyrusNajmabadi 已提交
3525
}");
3526 3527
        }

J
Jared Parsons 已提交
3528
        [WorkItem(640136, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/640136")]
3529
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3530
        public async Task RemoveUnnecessaryCastAndParseCorrect()
3531
        {
C
CyrusNajmabadi 已提交
3532
            await TestInRegularAndScriptAsync(
3533 3534 3535 3536 3537 3538
@"
using System;
using System.Threading.Tasks;
 
class C
{
3539
    void Goo(Task<Action> x)
3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551
    {
        (([|(Task<Action>)x|]).Result)();
    }
}
",

@"
using System;
using System.Threading.Tasks;
 
class C
{
3552
    void Goo(Task<Action> x)
3553
    {
3554
        (x.Result)();
3555 3556
    }
}
3557
");
3558 3559
        }

J
Jared Parsons 已提交
3560
        [WorkItem(626026, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/626026")]
3561
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3562
        public async Task DontRemoveCastIfUserDefinedExplicitCast()
3563
        {
C
CyrusNajmabadi 已提交
3564
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3565
@"class Program
3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583
{
    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
{
C
CyrusNajmabadi 已提交
3584
}");
3585 3586
        }

J
Jared Parsons 已提交
3587
        [WorkItem(768895, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/768895")]
3588
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3589
        public async Task DontRemoveNecessaryCastInTernary()
3590
        {
C
CyrusNajmabadi 已提交
3591
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3592
@"class Program
3593 3594 3595 3596 3597 3598
{
    static void Main(string[] args)
    {
        object x = null;
        int y = [|(bool)x|] ? 1 : 0;
    }
C
CyrusNajmabadi 已提交
3599
}");
3600 3601
        }

J
Jared Parsons 已提交
3602
        [WorkItem(770187, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/770187")]
3603
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3604
        public async Task DontRemoveNecessaryCastInSwitchExpression()
3605
        {
C
CyrusNajmabadi 已提交
3606
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3607
@"namespace ConsoleApplication23
3608 3609 3610 3611 3612
{
    class Program
    {
        static void Main(string[] args)
        {
3613 3614
            int goo = 0;
            switch ([|(E)goo|])
3615 3616 3617 3618 3619 3620 3621
            {
                case E.A:
                case E.B:
                    return;
            }
        }
    }
C
CyrusNajmabadi 已提交
3622

3623 3624 3625 3626 3627 3628
    enum E
    {
        A,
        B,
        C
    }
C
CyrusNajmabadi 已提交
3629
}");
3630 3631
        }

J
Jared Parsons 已提交
3632
        [WorkItem(844482, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/844482")]
3633
        [WorkItem(2761, "https://github.com/dotnet/roslyn/issues/2761")]
3634
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3635
        public async Task DontRemoveCastFromBaseToDerivedWithExplicitReference()
3636
        {
C
CyrusNajmabadi 已提交
3637
            await TestMissingInRegularAndScriptAsync(
3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653
@"class Program
{
    static void Main(string[] args)
    {
        C x = null;
        C y = null;
        y = [|(D)x|];
    }
}

class C
{
}

class D : C
{
3654 3655 3656 3657
}");
        }

        [WorkItem(3254, "https://github.com/dotnet/roslyn/issues/3254")]
3658
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3659
        public async Task DontRemoveCastToTypeParameterWithExceptionConstraint()
3660
        {
C
CyrusNajmabadi 已提交
3661
            await TestMissingInRegularAndScriptAsync(
3662 3663 3664 3665
@"using System;

class Program
{
C
CyrusNajmabadi 已提交
3666
    private static void RequiresCondition<TException>(bool condition, string messageOnFalseCondition) where TException : Exception
3667 3668 3669 3670 3671 3672 3673 3674 3675 3676
    {
        if (!condition)
        {
            throw [|(TException)Activator.CreateInstance(typeof(TException), messageOnFalseCondition)|];
        }
    }
}");
        }

        [WorkItem(3254, "https://github.com/dotnet/roslyn/issues/3254")]
3677
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3678
        public async Task DontRemoveCastToTypeParameterWithExceptionSubTypeConstraint()
3679
        {
C
CyrusNajmabadi 已提交
3680
            await TestMissingInRegularAndScriptAsync(
3681 3682 3683 3684
@"using System;

class Program
{
C
CyrusNajmabadi 已提交
3685
    private static void RequiresCondition<TException>(bool condition, string messageOnFalseCondition) where TException : ArgumentException
3686 3687 3688 3689 3690 3691
    {
        if (!condition)
        {
            throw [|(TException)Activator.CreateInstance(typeof(TException), messageOnFalseCondition)|];
        }
    }
3692
}");
3693
        }
3694 3695 3696 3697 3698

        [WorkItem(8111, "https://github.com/dotnet/roslyn/issues/8111")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task DontRemoveCastThatChangesShapeOfAnonymousTypeObject()
        {
C
CyrusNajmabadi 已提交
3699
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3700
@"class Program
3701 3702 3703 3704 3705
{
    static void Main(string[] args)
    {
        object thing = new { shouldBeAnInt = [|(int)Directions.South|] };
    }
C
CyrusNajmabadi 已提交
3706 3707 3708 3709 3710 3711 3712 3713 3714

    public enum Directions
    {
        North,
        East,
        South,
        West
    }
}");
3715 3716 3717 3718 3719 3720
        }

        [WorkItem(8111, "https://github.com/dotnet/roslyn/issues/8111")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task RemoveCastThatDoesntChangeShapeOfAnonymousTypeObject()
        {
C
CyrusNajmabadi 已提交
3721
            await TestInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3722
@"class Program
3723 3724 3725 3726 3727 3728
{
    static void Main(string[] args)
    {
        object thing = new { shouldBeAnInt = [|(Directions)Directions.South|] };
    }

C
CyrusNajmabadi 已提交
3729 3730 3731 3732 3733 3734 3735 3736 3737 3738
    public enum Directions
    {
        North,
        East,
        South,
        West
    }
}",

@"class Program
3739 3740 3741 3742 3743
{
    static void Main(string[] args)
    {
        object thing = new { shouldBeAnInt = Directions.South };
    }
C
CyrusNajmabadi 已提交
3744 3745 3746 3747 3748 3749 3750 3751 3752

    public enum Directions
    {
        North,
        East,
        South,
        West
    }
}");
3753
        }
3754 3755 3756 3757

        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task Tuple()
        {
C
CyrusNajmabadi 已提交
3758
            await TestInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771
@"class C
{
    void Main()
    {
        (int, string) tuple = [|((int, string))(1, ""hello"")|];
    }
}",
@"class C
{
    void Main()
    {
        (int, string) tuple = (1, ""hello"");
    }
C
CyrusNajmabadi 已提交
3772
}");
3773 3774 3775 3776 3777
        }

        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task TupleWithDifferentNames()
        {
C
CyrusNajmabadi 已提交
3778
            await TestInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791
@"class C
{
    void Main()
    {
        (int a, string) tuple = [|((int, string d))(1, f: ""hello"")|];
    }
}",
@"class C
{
    void Main()
    {
        (int a, string) tuple = (1, f: ""hello"");
    }
C
CyrusNajmabadi 已提交
3792
}");
3793
        }
3794

3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        [WorkItem(24791, "https://github.com/dotnet/roslyn/issues/24791")]
        public async Task SimpleBoolCast()
        {
            await TestInRegularAndScriptAsync(
@"class C
{
    bool M()
    {
        if (![|(bool)|]M()) throw null;
        throw null;
    }
}",
@"class C
{
    bool M()
    {
        if (!M()) throw null;
        throw null;
    }
}");
        }

3818 3819 3820 3821 3822
        [WorkItem(12572, "https://github.com/dotnet/roslyn/issues/12572")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task DontRemoveCastThatUnboxes()
        {
            // The cast below can't be removed because it could throw a null ref exception.
C
CyrusNajmabadi 已提交
3823
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3824
@"using System;
3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843

class Program
{
    static void Main()
    {
        object i = null;
        switch ([|(int)i|])
        {
            case 0:
                Console.WriteLine(0);
                break;
            case 1:
                Console.WriteLine(1);
                break;
            case 2:
                Console.WriteLine(2);
                break;
        }
    }
3844 3845 3846 3847 3848 3849 3850
}");
        }

        [WorkItem(17029, "https://github.com/dotnet/roslyn/issues/17029")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task DontRemoveCastOnEnumComparison1()
        {
C
CyrusNajmabadi 已提交
3851
            await TestMissingInRegularAndScriptAsync(
3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872
@"
enum TransferTypeKey
{
    Transfer,
    TransferToBeneficiary
}

class Program
{
    static void Main(dynamic p)
    {
        if (p.TYP != [|(int)TransferTypeKey.TransferToBeneficiary|])
          throw new InvalidOperationException();
    }
}");
        }

        [WorkItem(17029, "https://github.com/dotnet/roslyn/issues/17029")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task DontRemoveCastOnEnumComparison2()
        {
C
CyrusNajmabadi 已提交
3873
            await TestMissingInRegularAndScriptAsync(
3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887
@"
enum TransferTypeKey
{
    Transfer,
    TransferToBeneficiary
}

class Program
{
    static void Main(dynamic p)
    {
        if ([|(int)TransferTypeKey.TransferToBeneficiary|] != p.TYP)
          throw new InvalidOperationException();
    }
C
CyrusNajmabadi 已提交
3888
}");
3889
        }
3890 3891 3892

        [WorkItem(18978, "https://github.com/dotnet/roslyn/issues/18978")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3893
        public async Task DontRemoveCastOnCallToMethodWithParamsArgs()
3894 3895 3896 3897 3898 3899 3900 3901
        {
            await TestMissingInRegularAndScriptAsync(
@"
class Program
{
    public static void Main(string[] args)
    {
        var takesArgs = new[] { ""Hello"", ""World"" };
3902
        TakesParams([|(object)|]takesArgs);
3903 3904
    }

3905
    private static void TakesParams(params object[] goo)
3906
    {
3907
        Console.WriteLine(goo.Length);
3908 3909 3910
    }
}");
        }
3911

3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task DontRemoveCastOnCallToMethodWithParamsArgsWithIncorrectMethodDefintion()
        {
            await TestMissingInRegularAndScriptAsync(
@"
class Program
{
    public static void Main(string[] args)
    {
        TakesParams([|(string)|]null);
    }

    private static void TakesParams(params string wrongDefined)
    {
        Console.WriteLine(wrongDefined.Length);
    }
}");
        }

3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944
        [WorkItem(18978, "https://github.com/dotnet/roslyn/issues/18978")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task RemoveCastOnCallToMethodWithParamsArgsIfImplicitConversionExists()
        {
            await TestInRegularAndScriptAsync(
@"
class Program
{
    public static void Main(string[] args)
    {
        var takesArgs = new[] { ""Hello"", ""World"" };
        TakesParams([|(System.IComparable[])|]takesArgs);
    }

3945
    private static void TakesParams(params object[] goo)
3946
    {
3947
        System.Console.WriteLine(goo.Length);
3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958
    }
}",
@"
class Program
{
    public static void Main(string[] args)
    {
        var takesArgs = new[] { ""Hello"", ""World"" };
        TakesParams(takesArgs);
    }

3959
    private static void TakesParams(params object[] goo)
3960
    {
3961
        System.Console.WriteLine(goo.Length);
3962 3963 3964
    }
}");
        }
V
Victor Zaytsev 已提交
3965

M
Martin Strecker 已提交
3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992
        [WorkItem(20630, "https://github.com/dotnet/roslyn/issues/20630")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task DontRemoveCastOnCallToAttributeWithParamsArgs()
        {
            await TestMissingInRegularAndScriptAsync(
@"
using System;
using System.Reflection;

sealed class MarkAttribute : Attribute
{
  public readonly string[] Arr;

  public MarkAttribute(params string[] arr)
  {
    Arr = arr;
  }
}
[Mark([|(string)|]null)]   // wrong instance of: IDE0004 Cast is redundant.
static class Program
{
  static void Main()
  {
  }
}");
        }

3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066
        [WorkItem(29264, "https://github.com/dotnet/roslyn/issues/29264")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task DontRemoveCastOnDictionaryIndexer()
        {
            await TestMissingInRegularAndScriptAsync(
@"
using System;
using System.Reflection;
using System.Collections.Generic;

static class Program
{
    enum TestEnum
    {
        Test,
    }

    static void Main()
    {
        Dictionary<int, string> Icons = new Dictionary<int, string>
        {
            [[|(int)|] TestEnum.Test] = null,
        };
    }
}");
        }

        [WorkItem(29264, "https://github.com/dotnet/roslyn/issues/29264")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task RemoveCastOnDictionaryIndexer()
        {
            await TestInRegularAndScriptAsync(
                @"
using System;
using System.Reflection;
using System.Collections.Generic;

static class Program
{
    enum TestEnum
    {
        Test,
    }

    static void Main()
    {
        Dictionary<int, string> Icons = new Dictionary<int, string>
        {
            [[|(int)|] 0] = null,
        };
    }
}",
                @"
using System;
using System.Reflection;
using System.Collections.Generic;

static class Program
{
    enum TestEnum
    {
        Test,
    }

    static void Main()
    {
        Dictionary<int, string> Icons = new Dictionary<int, string>
        {
            [0] = null,
        };
    }
}");
        }

M
Martin Strecker 已提交
4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129
        [WorkItem(20630, "https://github.com/dotnet/roslyn/issues/20630")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task DontRemoveCastOnCallToAttributeWithParamsArgsAndProperty()
        {
            await TestMissingInRegularAndScriptAsync(
@"
using System;
sealed class MarkAttribute : Attribute
{
    public MarkAttribute(params string[] arr)
    {
    }
    public int Prop { get; set; }
}

[Mark([|(string)|]null, Prop = 1)] 
static class Program
{
}");
        }

        [WorkItem(20630, "https://github.com/dotnet/roslyn/issues/20630")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task DontRemoveCastOnCallToAttributeWithParamsArgsPropertyAndOtherArg()
        {
            await TestMissingInRegularAndScriptAsync(
@"
using System;
sealed class MarkAttribute : Attribute
{
    public MarkAttribute(bool otherArg, params string[] arr)
    {
    }
    public int Prop { get; set; }
}

[Mark(true, [|(string)|]null, Prop = 1)] 
static class Program
{
}");
        }

        [WorkItem(20630, "https://github.com/dotnet/roslyn/issues/20630")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task DontRemoveCastOnCallToAttributeWithParamsArgsNamedArgsAndProperty()
        {
            await TestMissingInRegularAndScriptAsync(
@"
using System;
sealed class MarkAttribute : Attribute
{
    public MarkAttribute(bool otherArg, params string[] arr)
    {
    }
    public int Prop { get; set; }
}

[Mark(arr: [|(string)|]null, otherArg: true, Prop = 1)]
static class Program
{
}");
        }

4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150
        [WorkItem(20630, "https://github.com/dotnet/roslyn/issues/20630")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task DontRemoveCastOnCallToAttributeWithParamsArgsNamedArgsWithIncorrectMethodDefintion()
        {
            await TestMissingInRegularAndScriptAsync(
@"
using System;
sealed class MarkAttribute : Attribute
{
    public MarkAttribute(bool otherArg, params string wrongDefined)
    {
    }
    public int Prop { get; set; }
}

[Mark(true, [|(string)|]null, Prop = 1)]
static class Program
{
}");
        }

M
Martin Strecker 已提交
4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182
        [WorkItem(20630, "https://github.com/dotnet/roslyn/issues/20630")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task RemoveCastOnCallToAttributeWithParamsArgsWithImplicitCast()
        {
            await TestInRegularAndScriptAsync(
@"
using System;
sealed class MarkAttribute : Attribute
{
    public MarkAttribute(bool otherArg, params object[] arr)
    {
    }
    public int Prop { get; set; }
}

[Mark(arr: ([|object[])new[]|] { ""Hello"", ""World"" }, otherArg: true, Prop = 1)]
static class Program
{
}",
@"
using System;
sealed class MarkAttribute : Attribute
{
    public MarkAttribute(bool otherArg, params object[] arr)
    {
    }
    public int Prop { get; set; }
}

[Mark(arr: (new[] { ""Hello"", ""World"" }), otherArg: true, Prop = 1)]
static class Program
{
4183 4184
}");
        }
4185

4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220
        [WorkItem(20630, "https://github.com/dotnet/roslyn/issues/20630")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task RemoveCastOnCallToAttributeWithCastInPropertySetter()
        {
            await TestInRegularAndScriptAsync(
@"
using System;
sealed class MarkAttribute : Attribute
{
    public MarkAttribute()
    {
    }
    public int Prop { get; set; }
}

[Mark(Prop = [|(int)1|])]
static class Program
{
}",
@"
using System;
sealed class MarkAttribute : Attribute
{
    public MarkAttribute()
    {
    }
    public int Prop { get; set; }
}

[Mark(Prop = 1)]
static class Program
{
}");
        }

V
Victor Zaytsev 已提交
4221
        [WorkItem(18510, "https://github.com/dotnet/roslyn/issues/18510")]
V
Victor Zaytsev 已提交
4222 4223 4224 4225
        [Theory, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        [InlineData("-")]
        [InlineData("+")]
        public async Task DontRemoveCastOnInvalidUnaryOperatorEnumValue1(string op)
V
Victor Zaytsev 已提交
4226
        {
V
Victor Zaytsev 已提交
4227
            await TestMissingInRegularAndScriptAsync(
V
Victor Zaytsev 已提交
4228
$@"
V
Victor Zaytsev 已提交
4229
enum Sign
V
Victor Zaytsev 已提交
4230
    {{
V
Victor Zaytsev 已提交
4231 4232
        Positive = 1,
        Negative = -1
V
Victor Zaytsev 已提交
4233
    }}
V
Victor Zaytsev 已提交
4234 4235

    class T
V
Victor Zaytsev 已提交
4236
    {{
4237
        void Goo()
V
Victor Zaytsev 已提交
4238
        {{
V
Victor Zaytsev 已提交
4239
            Sign mySign = Sign.Positive;
V
Victor Zaytsev 已提交
4240 4241 4242
            Sign invertedSign = (Sign) ( [|{op}((int) mySign)|] );
        }}
    }}");
V
Victor Zaytsev 已提交
4243 4244 4245
        }

        [WorkItem(18510, "https://github.com/dotnet/roslyn/issues/18510")]
V
Victor Zaytsev 已提交
4246 4247 4248 4249
        [Theory, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        [InlineData("-")]
        [InlineData("+")]
        public async Task DontRemoveCastOnInvalidUnaryOperatorEnumValue2(string op)
V
Victor Zaytsev 已提交
4250 4251
        {
            await TestMissingInRegularAndScriptAsync(
V
Victor Zaytsev 已提交
4252
$@"
V
Victor Zaytsev 已提交
4253
enum Sign
V
Victor Zaytsev 已提交
4254
    {{
V
Victor Zaytsev 已提交
4255 4256
        Positive = 1,
        Negative = -1
V
Victor Zaytsev 已提交
4257
    }}
V
Victor Zaytsev 已提交
4258 4259

    class T
V
Victor Zaytsev 已提交
4260
    {{
4261
        void Goo()
V
Victor Zaytsev 已提交
4262
        {{
V
Victor Zaytsev 已提交
4263
            Sign mySign = Sign.Positive;
V
Victor Zaytsev 已提交
4264 4265 4266
            Sign invertedSign = (Sign) ( [|{op}(int) mySign|] );
        }}
    }}");
V
Victor Zaytsev 已提交
4267 4268 4269 4270
        }

        [WorkItem(18510, "https://github.com/dotnet/roslyn/issues/18510")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
V
Victor Zaytsev 已提交
4271
        public async Task RemoveCastOnValidUnaryOperatorEnumValue()
V
Victor Zaytsev 已提交
4272
        {
V
Victor Zaytsev 已提交
4273
            await TestInRegularAndScriptAsync(
V
Victor Zaytsev 已提交
4274 4275 4276 4277 4278 4279 4280 4281 4282
@"
enum Sign
    {
        Positive = 1,
        Negative = -1
    }

    class T
    {
4283
        void Goo()
V
Victor Zaytsev 已提交
4284 4285
        {
            Sign mySign = Sign.Positive;
V
Victor Zaytsev 已提交
4286
            Sign invertedSign = (Sign) ( [|~(int) mySign|] );
V
Victor Zaytsev 已提交
4287
        }
V
Victor Zaytsev 已提交
4288
    }",
V
Victor Zaytsev 已提交
4289 4290 4291 4292 4293 4294 4295 4296 4297
@"
enum Sign
    {
        Positive = 1,
        Negative = -1
    }

    class T
    {
4298
        void Goo()
V
Victor Zaytsev 已提交
4299 4300
        {
            Sign mySign = Sign.Positive;
4301
            Sign invertedSign = (Sign) ( ~mySign);
V
Victor Zaytsev 已提交
4302
        }
V
Victor Zaytsev 已提交
4303 4304
    }");
        }
4305

Š
Šimon Koníček 已提交
4306
        [WorkItem(25456, "https://github.com/dotnet/roslyn/issues/25456#issuecomment-373549735")]
4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task DontIntroduceDefaultLiteralInSwitchCase()
        {
            await TestMissingInRegularAndScriptAsync(
@"
class C
{
    void M()
    {
        switch (true)
        {
            case [|(bool)default|]:
                break;
        }
    }
4322
}", parameters: new TestParameters(new CSharpParseOptions(LanguageVersion.CSharp7_1)));
4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339
        }

        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task DontIntroduceDefaultLiteralInSwitchCase_CastInsideParentheses()
        {
            await TestMissingInRegularAndScriptAsync(
@"
class C
{
    void M()
    {
        switch (true)
        {
            case ([|(bool)default|]):
                break;
        }
    }
4340
}", parameters: new TestParameters(new CSharpParseOptions(LanguageVersion.CSharp7_1)));
4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357
        }

        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task DontIntroduceDefaultLiteralInSwitchCase_DefaultInsideParentheses()
        {
            await TestMissingInRegularAndScriptAsync(
@"
class C
{
    void M()
    {
        switch (true)
        {
            case [|(bool)(default)|]:
                break;
        }
    }
4358
}", parameters: new TestParameters(new CSharpParseOptions(LanguageVersion.CSharp7_1)));
4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383
        }

        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task DontIntroduceDefaultLiteralInSwitchCase_RemoveDoubleCast()
        {
            await TestInRegularAndScript1Async(
@"
class C
{
    void M()
    {
        switch (true)
        {
            case (bool)[|(bool)default|]:
                break;
        }
    }
}",
@"
class C
{
    void M()
    {
        switch (true)
        {
4384
            case (bool)default:
4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405
                break;
        }
    }
}", parameters: new TestParameters(new CSharpParseOptions(LanguageVersion.CSharp7_1)));
        }

        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task DontIntroduceDefaultLiteralInPatternSwitchCase()
        {
            await TestMissingInRegularAndScriptAsync(
@"
class C
{
    void M()
    {
        switch (true)
        {
            case [|(bool)default|] when true:
                break;
        }
    }
4406
}", parameters: new TestParameters(new CSharpParseOptions(LanguageVersion.CSharp7_1)));
4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423
        }

        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task DontIntroduceDefaultLiteralInPatternSwitchCase_CastInsideParentheses()
        {
            await TestMissingInRegularAndScriptAsync(
@"
class C
{
    void M()
    {
        switch (true)
        {
            case ([|(bool)default|]) when true:
                break;
        }
    }
4424
}", parameters: new TestParameters(new CSharpParseOptions(LanguageVersion.CSharp7_1)));
4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441
        }

        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task DontIntroduceDefaultLiteralInPatternSwitchCase_DefaultInsideParentheses()
        {
            await TestMissingInRegularAndScriptAsync(
@"
class C
{
    void M()
    {
        switch (true)
        {
            case [|(bool)(default)|] when true:
                break;
        }
    }
4442
}", parameters: new TestParameters(new CSharpParseOptions(LanguageVersion.CSharp7_1)));
4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467
        }

        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task DontIntroduceDefaultLiteralInPatternSwitchCase_RemoveDoubleCast()
        {
            await TestInRegularAndScript1Async(
@"
class C
{
    void M()
    {
        switch (true)
        {
            case (bool)[|(bool)default|] when true:
                break;
        }
    }
}",
@"
class C
{
    void M()
    {
        switch (true)
        {
4468
            case (bool)default when true:
4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497
                break;
        }
    }
}", parameters: new TestParameters(new CSharpParseOptions(LanguageVersion.CSharp7_1)));
        }

        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task DontIntroduceDefaultLiteralInPatternSwitchCase_RemoveInsideWhenClause()
        {
            await TestInRegularAndScript1Async(
@"
class C
{
    void M()
    {
        switch (true)
        {
            case (bool)default when [|(bool)default|]:
                break;
        }
    }
}",
@"
class C
{
    void M()
    {
        switch (true)
        {
4498
            case (bool)default when default:
4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515
                break;
        }
    }
}", parameters: new TestParameters(new CSharpParseOptions(LanguageVersion.CSharp7_1)));
        }

        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task DontIntroduceDefaultLiteralInPatternIs()
        {
            await TestMissingInRegularAndScriptAsync(
@"
class C
{
    void M()
    {
        if (true is [|(bool)default|]);
    }
4516
}", parameters: new TestParameters(new CSharpParseOptions(LanguageVersion.CSharp7_1)));
4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529
        }

        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task DontIntroduceDefaultLiteralInPatternIs_CastInsideParentheses()
        {
            await TestMissingInRegularAndScriptAsync(
@"
class C
{
    void M()
    {
        if (true is ([|(bool)default|]));
    }
4530
}", parameters: new TestParameters(new CSharpParseOptions(LanguageVersion.CSharp7_1)));
4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543
        }

        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task DontIntroduceDefaultLiteralInPatternIs_DefaultInsideParentheses()
        {
            await TestMissingInRegularAndScriptAsync(
@"
class C
{
    void M()
    {
        if (true is [|(bool)(default)|]);
    }
4544
}", parameters: new TestParameters(new CSharpParseOptions(LanguageVersion.CSharp7_1)));
4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563
        }

        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task DontIntroduceDefaultLiteralInPatternIs_RemoveDoubleCast()
        {
            await TestInRegularAndScript1Async(
@"
class C
{
    void M()
    {
        if (true is (bool)[|(bool)default|]);
    }
}",
@"
class C
{
    void M()
    {
4564
        if (true is (bool)default) ;
4565 4566 4567
    }
}", parameters: new TestParameters(new CSharpParseOptions(LanguageVersion.CSharp7_1)));
        }
4568

D
David Wengier 已提交
4569
        [WorkItem(27239, "https://github.com/dotnet/roslyn/issues/27239")]
4570
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
4571
        public async Task DontOfferToRemoveCastWhereNoConversionExists()
4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583
        {
            await TestMissingInRegularAndScriptAsync(
                @"
using System;

class C
{
    void M()
    {
        object o = null;
        TypedReference r2 = [|(TypedReference)o|];
    }
4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607
}");
        }

        [WorkItem(28412, "https://github.com/dotnet/roslyn/issues/28412")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task DontOfferToRemoveCastWhenAccessingHiddenProperty()
        {
            await TestMissingInRegularAndScriptAsync(@"
using System.Collections.Generic;
class Fruit
{
    public IDictionary<string, object> Properties { get; set; }
}
class Apple : Fruit
{
    public new IDictionary<string, object> Properties { get; }
}
class Tester
{
    public void Test()
    {
        var a = new Apple();
        ([|(Fruit)a|]).Properties[""Color""] = ""Red"";
    }
4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670
}");
        }

        [WorkItem(31963, "https://github.com/dotnet/roslyn/issues/31963")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task DontOfferToRemoveCastInConstructorWhenItNeeded()
        {
            await TestMissingInRegularAndScriptAsync(@"
class IntegerWrapper
{
    public IntegerWrapper(int value)
    {
    }
}
enum Goo
{
    First,
    Second
}
class Tester
{
    public void Test()
    {
        var a = new IntegerWrapper([|(int)Goo.First|]);
    }
}");
        }

        [WorkItem(31963, "https://github.com/dotnet/roslyn/issues/31963")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task DontOfferToRemoveCastInBaseConstructorInitializerWhenItNeeded()
        {
            await TestMissingInRegularAndScriptAsync(
@"
class B
{
    B(int a)
    {
    }
}
class C : B
{
    C(double a) : base([|(int)a|])
    {
    }
}");
        }

        [WorkItem(31963, "https://github.com/dotnet/roslyn/issues/31963")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task DontOfferToRemoveCastInConstructorInitializerWhenItNeeded()
        {
            await TestMissingInRegularAndScriptAsync(
@"
class B
{
    B(int a)
    {
    }

    B(double a) : this([|(int)a|])
    {
    }
4671 4672
}");
        }
4673
    }
S
Sam Harwell 已提交
4674
}