RemoveUnnecessaryCastTests.cs 109.5 KB
Newer Older
J
Jonathon Marolf 已提交
1 2 3
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
4

5
using System.Threading.Tasks;
6
using Microsoft.CodeAnalysis.CodeFixes;
7
using Microsoft.CodeAnalysis.CSharp;
8
using Microsoft.CodeAnalysis.CSharp.RemoveUnnecessaryCast;
9
using Microsoft.CodeAnalysis.Diagnostics;
10
using Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Diagnostics;
11
using Microsoft.CodeAnalysis.Test.Utilities;
12 13 14
using Roslyn.Test.Utilities;
using Xunit;

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

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

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

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

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

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

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

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

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

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

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

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

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

J
Jared Parsons 已提交
197
        [WorkItem(545144, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545144")]
198
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
199
        public async Task DontRemoveCastToObjectFromDelegateComparison()
200 201 202 203
        {
            // 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 已提交
204
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
205
@"using System;
206 207 208 209 210 211 212 213 214

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

J
Jared Parsons 已提交
573
        [WorkItem(545291, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545291")]
574
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
575
        public async Task RemoveUnneededCastInConditionalExpression3()
576
        {
C
CyrusNajmabadi 已提交
577
            await TestInRegularAndScriptAsync(
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
            @"
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;
    }
598
}");
599 600
        }

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

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

J
Jared Parsons 已提交
644
        [WorkItem(545459, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545459")]
645
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
646
        public async Task RemoveUnneededCastInsideADelegateConstructor()
647
        {
C
CyrusNajmabadi 已提交
648
            await TestInRegularAndScriptAsync(
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 674
            @"
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) { }
675
}");
676 677
        }

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

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

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

J
Jared Parsons 已提交
742
        [WorkItem(545578, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545578")]
743
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
744
        public async Task RemoveUnneededCastInsideGotoCaseStatement()
745
        {
C
CyrusNajmabadi 已提交
746
            await TestInRegularAndScriptAsync(
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 772
            @"
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;
        }
    }
773
}");
774 775
        }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

@"
using System;

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

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

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

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

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

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

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

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

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

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

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

J
Jared Parsons 已提交
1160
        [WorkItem(545750, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545750")]
1161
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1162
        public async Task RemoveUnnecessaryCastToBaseType()
1163
        {
C
CyrusNajmabadi 已提交
1164
            await TestInRegularAndScriptAsync(
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 1190
            @"
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 "";
    }
1191
}");
1192 1193
        }

J
Jared Parsons 已提交
1194
        [WorkItem(545855, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545855")]
1195
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1196
        public async Task RemoveUnnecessaryLambdaToDelegateCast()
1197
        {
1198
            await TestAsync(
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 1247
            @"
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;
    }
}
",
1248
    parseOptions: null);
1249 1250
        }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1435
    static void Goo<TAny, TClass>(TAny x, TClass y) 
1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
        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()
    {
1472
        Goo(new S(), new C());
1473 1474
    }

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

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

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

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

J
Jared Parsons 已提交
1510
        [WorkItem(545777, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545777")]
1511
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1512
        public async Task DontRemoveImportantTrailingTrivia()
1513
        {
C
CyrusNajmabadi 已提交
1514
            await TestInRegularAndScriptAsync(
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 1540
            @"
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;
    }
}
1541
");
1542 1543
        }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

J
Jared Parsons 已提交
1829
        [WorkItem(545942, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545942")]
1830
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1831
        public async Task DontRemoveCastFromValueTypeToObjectInReferenceEquality()
1832 1833 1834 1835
        {
            // 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 已提交
1836
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
1837
@"using System;
1838 1839 1840 1841 1842 1843 1844 1845

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

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

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

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

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

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

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

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

1899 1900
        [WorkItem(2691, "https://github.com/dotnet/roslyn/issues/2691")]
        [WorkItem(2987, "https://github.com/dotnet/roslyn/issues/2987")]
1901
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1902
        public async Task DontRemoveNecessaryCastBeforePointerDereference3()
1903 1904 1905 1906
        {
            // 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 已提交
1907
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
1908
@"class C
1909 1910 1911 1912 1913
{
    public unsafe float ReadSingle(byte* ptr)
    {
        return *[|(float*)ptr|];
    }
C
CyrusNajmabadi 已提交
1914
}");
1915 1916 1917 1918
        }

        [WorkItem(2691, "https://github.com/dotnet/roslyn/issues/2691")]
        [WorkItem(2987, "https://github.com/dotnet/roslyn/issues/2987")]
1919
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1920
        public async Task DontRemoveNumericCastInUncheckedExpression()
1921 1922 1923 1924
        {
            // 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 已提交
1925
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
1926
@"class C
1927 1928 1929 1930 1931 1932 1933 1934 1935 1936
{
    private unsafe readonly byte* _endPointer;
    private unsafe byte* _currentPointer;

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

        [WorkItem(2691, "https://github.com/dotnet/roslyn/issues/2691")]
        [WorkItem(2987, "https://github.com/dotnet/roslyn/issues/2987")]
1942
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1943
        public async Task DontRemoveNumericCastInUncheckedStatement()
1944 1945 1946 1947
        {
            // 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 已提交
1948
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
1949
@"class C
1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962
{
    private unsafe readonly byte* _endPointer;
    private unsafe byte* _currentPointer;

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

        [WorkItem(2691, "https://github.com/dotnet/roslyn/issues/2691")]
        [WorkItem(2987, "https://github.com/dotnet/roslyn/issues/2987")]
1968
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1969
        public async Task DontRemoveNumericCastInCheckedExpression()
1970 1971 1972 1973
        {
            // 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 已提交
1974
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
1975
@"class C
1976 1977 1978 1979 1980 1981 1982 1983 1984 1985
{
    private unsafe readonly byte* _endPointer;
    private unsafe byte* _currentPointer;

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

        [WorkItem(2691, "https://github.com/dotnet/roslyn/issues/2691")]
        [WorkItem(2987, "https://github.com/dotnet/roslyn/issues/2987")]
1991
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
1992
        public async Task DontRemoveNumericCastInCheckedStatement()
1993 1994 1995 1996
        {
            // 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 已提交
1997
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
1998
@"class C
1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011
{
    private unsafe readonly byte* _endPointer;
    private unsafe byte* _currentPointer;

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

D
David Barbet 已提交
2015 2016
        [WorkItem(26640, "https://github.com/dotnet/roslyn/issues/26640")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2017 2018
        public async Task DontRemoveCastToByteFromIntInConditionalExpression()
        {
D
David Barbet 已提交
2019 2020 2021
            await TestMissingInRegularAndScriptAsync(
@"class C
{
2022 2023 2024 2025 2026 2027 2028
    object M1(bool b)
    {
        return [|b ? (byte)1 : (byte)0|];
    }
}");
        }

2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049
        [WorkItem(26640, "https://github.com/dotnet/roslyn/issues/26640")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task RemoveCastToDoubleFromIntWithTwoInConditionalExpression()
        {
            await TestInRegularAndScriptAsync(
@"class C
{
    object M1(bool b)
    {
        return [|b ? (double)1 : (double)0|];
    }
}",
@"class C
{
    object M1(bool b)
    {
        return b ? 1 : (double)0;
    }
}");
        }

2050 2051
        [WorkItem(26640, "https://github.com/dotnet/roslyn/issues/26640")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2052
        public async Task DontRemoveCastToDoubleFromIntInConditionalExpression()
D
David Barbet 已提交
2053
        {
2054 2055 2056 2057 2058 2059 2060 2061
            await TestMissingInRegularAndScriptAsync(
@"class C
{
    object M1(bool b)
    {
        return [|b ? 1 : (double)0|];
    }
}");
D
David Barbet 已提交
2062 2063
        }

2064 2065
        [WorkItem(26640, "https://github.com/dotnet/roslyn/issues/26640")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2066
        public async Task DontRemoveCastToUIntFromCharInConditionalExpression()
D
David Barbet 已提交
2067
        {
2068 2069 2070 2071 2072 2073 2074 2075
            await TestMissingInRegularAndScriptAsync(
@"class C
{
    object M1(bool b)
    {
        return [|b ? '1' : (uint)'0'|];
    }
}");
D
David Barbet 已提交
2076
        }
2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095

        [WorkItem(26640, "https://github.com/dotnet/roslyn/issues/26640")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task RemoveUnnecessaryNumericCastToSameTypeInConditionalExpression()
        {
            await TestInRegularAndScriptAsync(
@"class C
{
    object M1(bool b)
    {
        return [|b ? (int)1 : 0|];
    }
}",
@"class C
{
    object M1(bool b)
    {
        return b ? 1 : 0;
    }
C
CyrusNajmabadi 已提交
2096
}");
2097 2098
        }

J
Jared Parsons 已提交
2099
        [WorkItem(545894, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545894")]
2100
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2101
        public async Task DontRemoveNecessaryCastInAttribute()
2102
        {
C
CyrusNajmabadi 已提交
2103
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
2104
@"using System;
2105 2106 2107 2108

[A([|(byte)0)|]]
class A : Attribute
{
C
CyrusNajmabadi 已提交
2109 2110 2111 2112
    public A(object x)
    {
    }
}");
2113 2114
        }

2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130
        [WorkItem(39042, "https://github.com/dotnet/roslyn/issues/39042")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task DontRemoveNecessaryCastForImplicitNumericCastsThatLoseInformation()
        {
            await TestMissingInRegularAndScriptAsync(
@"using System;

class A
{
    public A(long x)
    {
        long y = (long)[|(double)x|];
    }
}");
        }

2131 2132
        #region Interface Casts

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

C
CyrusNajmabadi 已提交
2139
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
2140
@"using System;
2141 2142 2143 2144 2145 2146 2147 2148

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

2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161
    public void Dispose()
    {
        Console.WriteLine(""X.Dispose"");
    }
}

class Y : X, IDisposable
{
    void IDisposable.Dispose()
    {
        Console.WriteLine(""Y.Dispose"");
    }
C
CyrusNajmabadi 已提交
2162
}");
2163 2164
        }

J
Jared Parsons 已提交
2165
        [WorkItem(545890, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545890")]
2166
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2167
        public async Task RemoveCastToInterfaceForSealedType1()
2168 2169
        {
            // Note: The cast below can be removed because C is sealed and the
2170
            // unspecified optional parameters of I.Goo() and C.Goo() have the
2171 2172
            // same default values.

C
CyrusNajmabadi 已提交
2173
            await TestInRegularAndScriptAsync(
2174 2175 2176 2177 2178
@"
using System;

interface I
{
2179
    void Goo(int x = 0);
2180 2181 2182 2183
}

sealed class C : I
{
2184
    public void Goo(int x = 0)
2185 2186 2187 2188 2189 2190
    {
        Console.WriteLine(x);
    }

    static void Main()
    {
2191
        ([|(I)new C()|]).Goo();
2192 2193 2194 2195 2196 2197 2198 2199 2200
    }
}
",

@"
using System;

interface I
{
2201
    void Goo(int x = 0);
2202 2203 2204 2205
}

sealed class C : I
{
2206
    public void Goo(int x = 0)
2207 2208 2209 2210 2211 2212
    {
        Console.WriteLine(x);
    }

    static void Main()
    {
2213
        new C().Goo();
2214 2215
    }
}
2216
");
2217 2218
        }

J
Jared Parsons 已提交
2219
        [WorkItem(545890, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545890")]
2220
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2221
        public async Task RemoveCastToInterfaceForSealedType2()
2222 2223 2224 2225
        {
            // Note: The cast below can be removed because C is sealed and the
            // interface member has no parameters.

C
CyrusNajmabadi 已提交
2226
            await TestInRegularAndScriptAsync(
2227 2228 2229 2230 2231
@"
using System;

interface I
{
2232
    string Goo { get; }
2233 2234 2235 2236
}

sealed class C : I
{
2237
    public string Goo
2238 2239 2240 2241 2242 2243 2244 2245 2246
    {
        get
        {
            return ""Nikov Rules"";
        }
    }

    static void Main()
    {
2247
        Console.WriteLine(([|(I)new C()|]).Goo);
2248 2249 2250 2251 2252 2253 2254 2255 2256
    }
}
",

@"
using System;

interface I
{
2257
    string Goo { get; }
2258 2259 2260 2261
}

sealed class C : I
{
2262
    public string Goo
2263 2264 2265 2266 2267 2268 2269 2270 2271
    {
        get
        {
            return ""Nikov Rules"";
        }
    }

    static void Main()
    {
2272
        Console.WriteLine(new C().Goo);
2273 2274
    }
}
2275
");
2276 2277
        }

J
Jared Parsons 已提交
2278
        [WorkItem(545890, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545890")]
2279
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2280
        public async Task RemoveCastToInterfaceForSealedType3()
2281 2282 2283 2284
        {
            // Note: The cast below can be removed because C is sealed and the
            // interface member has no parameters.

C
CyrusNajmabadi 已提交
2285
            await TestInRegularAndScriptAsync(
2286 2287 2288 2289 2290
@"
using System;

interface I
{
2291
    string Goo { get; }
2292 2293 2294 2295 2296 2297
}

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

2298
    public string Goo
2299 2300 2301 2302 2303 2304 2305 2306 2307
    {
        get
        {
            return ""Nikov Rules"";
        }
    }

    static void Main()
    {
2308
        Console.WriteLine(([|(I)Instance|]).Goo);
2309 2310 2311 2312 2313 2314 2315 2316 2317
    }
}
",

@"
using System;

interface I
{
2318
    string Goo { get; }
2319 2320 2321 2322 2323 2324
}

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

2325
    public string Goo
2326 2327 2328 2329 2330 2331 2332 2333 2334
    {
        get
        {
            return ""Nikov Rules"";
        }
    }

    static void Main()
    {
2335
        Console.WriteLine(Instance.Goo);
2336 2337
    }
}
2338
");
2339 2340
        }

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

C
CyrusNajmabadi 已提交
2348
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
2349
@"using System;
2350 2351 2352

interface I
{
2353
    void Goo(int x = 0);
2354 2355 2356 2357
}

sealed class C : I
{
2358
    public void Goo(int x = 1)
2359 2360 2361 2362 2363 2364
    {
        Console.WriteLine(x);
    }

    static void Main()
    {
2365
        ([|(I)new C()|]).Goo();
2366
    }
C
CyrusNajmabadi 已提交
2367
}");
2368 2369
        }

J
Jared Parsons 已提交
2370
        [WorkItem(545890, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545890")]
2371
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2372
        public async Task RemoveCastToInterfaceForSealedType5()
2373 2374 2375 2376 2377
        {
            // Note: The cast below can be removed (even though C is sealed)
            // because the optional parameters whose default values differ are
            // specified.

C
CyrusNajmabadi 已提交
2378
            await TestInRegularAndScriptAsync(
2379 2380 2381 2382 2383
@"
using System;

interface I
{
2384
    void Goo(int x = 0);
2385 2386 2387 2388
}

sealed class C : I
{
2389
    public void Goo(int x = 1)
2390 2391 2392 2393 2394 2395
    {
        Console.WriteLine(x);
    }

    static void Main()
    {
2396
        ([|(I)new C()|]).Goo(2);
2397 2398 2399 2400 2401 2402 2403 2404 2405
    }
}
",

@"
using System;

interface I
{
2406
    void Goo(int x = 0);
2407 2408 2409 2410
}

sealed class C : I
{
2411
    public void Goo(int x = 1)
2412 2413 2414 2415 2416 2417
    {
        Console.WriteLine(x);
    }

    static void Main()
    {
2418
        new C().Goo(2);
2419 2420
    }
}
2421
");
2422 2423
        }

J
Jared Parsons 已提交
2424
        [WorkItem(545888, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545888")]
2425
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2426
        public async Task DontRemoveCastToInterfaceForSealedType6()
2427 2428 2429 2430 2431
        {
            // 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 已提交
2432
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
2433
@"using System;
2434 2435 2436

interface I
{
2437
    void Goo(int x = 0, int y = 0);
2438 2439 2440 2441
}

sealed class C : I
{
2442
    public void Goo(int y = 0, int x = 0)
2443 2444 2445 2446 2447 2448
    {
        Console.WriteLine(x);
    }

    static void Main()
    {
2449
        ([|(I)new C()|]).Goo(x: 1);
2450
    }
C
CyrusNajmabadi 已提交
2451
}");
2452 2453
        }

J
Jared Parsons 已提交
2454
        [WorkItem(545888, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545888")]
2455
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2456
        public async Task RemoveCastToInterfaceForSealedType7()
2457
        {
C
CyrusNajmabadi 已提交
2458
            await TestInRegularAndScriptAsync(
2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506
@"
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]);
    }
}
2507
");
2508 2509
        }

J
Jared Parsons 已提交
2510
        [WorkItem(545888, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545888")]
2511
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2512
        public async Task DontRemoveCastToInterfaceForSealedType8()
2513 2514 2515 2516 2517
        {
            // 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 已提交
2518
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
2519
@"using System;
2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539

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 已提交
2540
}");
2541 2542
        }

J
Jared Parsons 已提交
2543
        [WorkItem(545883, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545883")]
2544
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2545
        public async Task DontRemoveCastToInterfaceForSealedType9()
2546 2547 2548 2549 2550
        {
            // 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 已提交
2551
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
2552
@"using System;
2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566
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 已提交
2567
}");
2568 2569
        }

J
Jared Parsons 已提交
2570
        [WorkItem(545887, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545887")]
2571
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2572
        public async Task DontRemoveCastToInterfaceForStruct1()
2573 2574 2575 2576
        {
            // Note: The cast below can't be removed because the cast boxes 's' and
            // unboxing would change program behavior.

C
CyrusNajmabadi 已提交
2577
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
2578
@"using System;
2579 2580 2581 2582

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

2584 2585 2586 2587 2588 2589
    void Increment();
}

struct S : IIncrementable
{
    public int Value { get; private set; }
C
CyrusNajmabadi 已提交
2590 2591 2592 2593 2594

    public void Increment()
    {
        Value++;
    }
2595 2596 2597 2598 2599 2600 2601

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

J
Jared Parsons 已提交
2605
        [WorkItem(545834, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545834")]
2606
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2607
        public async Task RemoveCastToInterfaceForStruct2()
2608 2609 2610 2611
        {
            // 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 已提交
2612
            await TestInRegularAndScriptAsync(
2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648
            @"
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();
    }
}
2649
");
2650 2651
        }

J
Jared Parsons 已提交
2652
        [WorkItem(544655, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/544655")]
2653
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2654
        public async Task RemoveCastToICloneableForDelegate()
2655
        {
C
Charles Stoner 已提交
2656
            // Note: The cast below can be removed because delegates are implicitly
2657 2658
            // sealed.

C
CyrusNajmabadi 已提交
2659
            await TestInRegularAndScriptAsync(
2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683
            @"
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();
    }
}
2684
");
2685 2686
        }

J
Jared Parsons 已提交
2687
        [WorkItem(545926, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545926")]
2688
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2689
        public async Task RemoveCastToICloneableForArray()
2690
        {
C
Charles Stoner 已提交
2691
            // Note: The cast below can be removed because arrays are implicitly
2692 2693
            // sealed.

C
CyrusNajmabadi 已提交
2694
            await TestInRegularAndScriptAsync(
2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718
            @"
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(); 
    }
}
2719
");
2720 2721
        }

J
Jared Parsons 已提交
2722
        [WorkItem(529897, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529897")]
2723
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2724
        public async Task RemoveCastToIConvertibleForEnum()
2725
        {
C
Charles Stoner 已提交
2726
            // Note: The cast below can be removed because enums are implicitly
2727 2728
            // sealed.

C
CyrusNajmabadi 已提交
2729
            await TestInRegularAndScriptAsync(
2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753
            @"
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();
    }
}
2754
");
2755 2756 2757 2758 2759 2760
        }

        #endregion

        #region ParamArray Parameter Casts

J
Jared Parsons 已提交
2761
        [WorkItem(545141, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545141")]
2762
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2763
        public async Task DontRemoveCastToObjectInParamArrayArg1()
2764
        {
C
CyrusNajmabadi 已提交
2765
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
2766
@"using System;
2767 2768 2769 2770 2771

class C
{
    static void Main()
    {
2772
        Goo([|(object)null|]);
2773 2774
    }

2775
    static void Goo(params object[] x)
2776 2777 2778
    {
        Console.WriteLine(x.Length);
    }
C
CyrusNajmabadi 已提交
2779
}");
2780 2781
        }

J
Jared Parsons 已提交
2782
        [WorkItem(529911, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529911")]
2783
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2784
        public async Task DontRemoveCastToIntArrayInParamArrayArg2()
2785
        {
C
CyrusNajmabadi 已提交
2786
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
2787
@"using System;
2788 2789 2790 2791 2792

class C
{
    static void Main()
    {
2793
        Goo([|(int[])null|]);
2794 2795
    }

2796
    static void Goo(params object[] x)
2797 2798 2799
    {
        Console.WriteLine(x.Length);
    }
C
CyrusNajmabadi 已提交
2800
}");
2801 2802
        }

J
Jared Parsons 已提交
2803
        [WorkItem(529911, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529911")]
2804
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2805
        public async Task DontRemoveCastToObjectArrayInParamArrayArg3()
2806
        {
C
CyrusNajmabadi 已提交
2807
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
2808
@"using System;
2809 2810 2811 2812 2813

class C
{
    static void Main()
    {
2814
        Goo([|(object[])null|]);
2815 2816
    }

2817
    static void Goo(params object[][] x)
2818 2819 2820
    {
        Console.WriteLine(x.Length);
    }
C
CyrusNajmabadi 已提交
2821
}");
2822 2823
        }

J
Jared Parsons 已提交
2824
        [WorkItem(529911, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529911")]
2825
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2826
        public async Task RemoveCastToObjectArrayInParamArrayArg1()
2827
        {
C
CyrusNajmabadi 已提交
2828
            await TestInRegularAndScriptAsync(
2829 2830 2831
            @"
class C
{
2832
    static void Goo(params object[] x) { }
2833 2834 2835

    static void Main()
    {
2836
        Goo([|(object[])null|]);
2837 2838 2839 2840 2841 2842 2843
    }
}
",

@"
class C
{
2844
    static void Goo(params object[] x) { }
2845 2846 2847

    static void Main()
    {
2848
        Goo(null);
2849 2850
    }
}
2851
");
2852 2853
        }

J
Jared Parsons 已提交
2854
        [WorkItem(529911, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529911")]
2855
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2856
        public async Task RemoveCastToStringArrayInParamArrayArg2()
2857
        {
C
CyrusNajmabadi 已提交
2858
            await TestInRegularAndScriptAsync(
2859 2860 2861
            @"
class C
{
2862
    static void Goo(params object[] x) { }
2863 2864 2865

    static void Main()
    {
2866
        Goo([|(string[])null|]);
2867 2868 2869 2870 2871 2872 2873
    }
}
",

@"
class C
{
2874
    static void Goo(params object[] x) { }
2875 2876 2877

    static void Main()
    {
2878
        Goo(null);
2879 2880
    }
}
2881
");
2882 2883
        }

J
Jared Parsons 已提交
2884
        [WorkItem(529911, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529911")]
2885
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2886
        public async Task RemoveCastToIntArrayInParamArrayArg3()
2887
        {
C
CyrusNajmabadi 已提交
2888
            await TestInRegularAndScriptAsync(
2889 2890 2891
            @"
class C
{
2892
    static void Goo(params int[] x) { }
2893 2894 2895

    static void Main()
    {
2896
        Goo([|(int[])null|]);
2897 2898 2899 2900 2901 2902 2903
    }
}
",

@"
class C
{
2904
    static void Goo(params int[] x) { }
2905 2906 2907

    static void Main()
    {
2908
        Goo(null);
2909 2910
    }
}
2911
");
2912 2913
        }

J
Jared Parsons 已提交
2914
        [WorkItem(529911, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529911")]
2915
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2916
        public async Task RemoveCastToObjectArrayInParamArrayArg4()
2917
        {
C
CyrusNajmabadi 已提交
2918
            await TestInRegularAndScriptAsync(
2919 2920 2921
            @"
class C
{
2922
    static void Goo(params object[] x) { }
2923 2924 2925

    static void Main()
    {
2926
        Goo([|(object[])null|], null);
2927 2928 2929 2930 2931 2932 2933
    }
}
",

@"
class C
{
2934
    static void Goo(params object[] x) { }
2935 2936 2937

    static void Main()
    {
2938
        Goo(null, null);
2939 2940
    }
}
2941
");
2942 2943
        }

J
Jared Parsons 已提交
2944
        [WorkItem(529911, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529911")]
2945
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2946
        public async Task RemoveCastToObjectInParamArrayArg5()
2947
        {
C
CyrusNajmabadi 已提交
2948
            await TestInRegularAndScriptAsync(
2949 2950 2951
            @"
class C
{
2952
    static void Goo(params object[] x) { }
2953 2954 2955

    static void Main()
    {
2956
        Goo([|(object)null|], null);
2957 2958 2959 2960 2961 2962 2963
    }
}
",

@"
class C
{
2964
    static void Goo(params object[] x) { }
2965 2966 2967

    static void Main()
    {
2968
        Goo(null, null);
2969 2970
    }
}
2971
");
2972 2973
        }

2974
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
2975
        public async Task RemoveCastToObjectArrayInParamArrayWithNamedArgument()
2976
        {
C
CyrusNajmabadi 已提交
2977
            await TestInRegularAndScriptAsync(
2978 2979 2980 2981 2982
                @"
class C
{
    static void Main()
    {
2983
        Goo(x: [|(object[])null|]);
2984 2985
    }

2986
    static void Goo(params object[] x) { }
2987 2988 2989 2990 2991 2992 2993
}
",
                @"
class C
{
    static void Main()
    {
2994
        Goo(x: null);
2995 2996
    }

2997
    static void Goo(params object[] x) { }
2998
}
2999
");
3000 3001 3002 3003 3004 3005
        }

        #endregion

        #region ForEach Statements

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

C
CyrusNajmabadi 已提交
3013
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3014
@"using System.Collections;
3015 3016 3017 3018 3019 3020

class Program
{
    static void Main()
    {
        object s = "";
C
CyrusNajmabadi 已提交
3021 3022 3023
        foreach (object x in [|(IEnumerable)s|])
        {
        }
3024
    }
C
CyrusNajmabadi 已提交
3025
}");
3026 3027
        }

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

C
CyrusNajmabadi 已提交
3035
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3036
@"using System.Collections.Generic;
3037 3038 3039 3040 3041 3042

class Program
{
    static void Main()
    {
        object s = "";
C
CyrusNajmabadi 已提交
3043 3044 3045
        foreach (object x in [|(IEnumerable<char>)s|])
        {
        }
3046
    }
C
CyrusNajmabadi 已提交
3047
}");
3048 3049
        }

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

C
CyrusNajmabadi 已提交
3058
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3059
@"using System.Collections;
3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078

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 已提交
3079 3080 3081
        foreach (object x in [|(D)new C()|])
        {
        }
3082 3083 3084 3085
    }
}");
        }

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

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

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

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

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

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

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

C
CyrusNajmabadi 已提交
3135
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3136 3137
@"using System;

3138 3139 3140 3141
class Program
{
    static void Main()
    {
C
CyrusNajmabadi 已提交
3142 3143 3144
        string[] s = {
            ""A""
        };
3145 3146 3147 3148 3149 3150
        foreach (var x in [|(Array)s|])
        {
            var y = x;
            y = 1;
        }
    }
C
CyrusNajmabadi 已提交
3151
}");
3152 3153 3154 3155
        }

        #endregion

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

C
CyrusNajmabadi 已提交
3163
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3164
@"using System;
3165 3166 3167

abstract class Y
{
3168
    public abstract void Goo(int x = 1);
3169 3170 3171 3172 3173 3174
}

class X : Y
{
    static void Main()
    {
3175
        ([|(Y)new X()|]).Goo();
3176 3177
    }

3178
    public override void Goo(int x = 2)
3179 3180 3181
    {
        Console.WriteLine(x);
    }
C
CyrusNajmabadi 已提交
3182
}");
3183 3184
        }

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

C
CyrusNajmabadi 已提交
3192
            await TestInRegularAndScriptAsync(
3193 3194 3195 3196 3197
@"
using System;

abstract class Y
{
3198
    public abstract void Goo(int x = 1);
3199 3200 3201 3202 3203 3204
}

class X : Y
{
    static void Main()
    {
3205
        ([|(Y)new X()|]).Goo();
3206 3207
    }

3208
    public override void Goo(int x = 1)
3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219
    {
        Console.WriteLine(x);
    }
}
",

@"
using System;

abstract class Y
{
3220
    public abstract void Goo(int x = 1);
3221 3222 3223 3224 3225 3226
}

class X : Y
{
    static void Main()
    {
3227
        new X().Goo();
3228 3229
    }

3230
    public override void Goo(int x = 1)
3231 3232 3233 3234
    {
        Console.WriteLine(x);
    }
}
3235
");
3236 3237
        }

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

C
CyrusNajmabadi 已提交
3245
            await TestInRegularAndScriptAsync(
3246 3247 3248 3249 3250 3251 3252
@"
using System;

static class Program
{
    static void Main()
    {
3253
        Action a = ([|(string)""""|]).Goo;
3254 3255
    }

3256
    static void Goo(this string x) { }
3257 3258 3259 3260 3261 3262 3263 3264 3265 3266
}
",

@"
using System;

static class Program
{
    static void Main()
    {
3267
        Action a = """".Goo;
3268 3269
    }

3270
    static void Goo(this string x) { }
3271
}
3272
");
3273 3274
        }

J
Jared Parsons 已提交
3275
        [WorkItem(609497, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/609497")]
3276
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3277
        public async Task Bugfix_609497()
3278
        {
C
CyrusNajmabadi 已提交
3279
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3280
@"using System;
3281
using System.Threading.Tasks;
C
CyrusNajmabadi 已提交
3282

3283 3284 3285 3286
class Program
{
    static void Main()
    {
3287
        Goo().Wait();
3288
    }
C
CyrusNajmabadi 已提交
3289

3290
    static async Task Goo()
3291 3292 3293 3294
    {
        Task task = Task.FromResult(0);
        Console.WriteLine(await [|(dynamic)task|]);
    }
C
CyrusNajmabadi 已提交
3295
}");
3296 3297
        }

J
Jared Parsons 已提交
3298
        [WorkItem(545995, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545995")]
3299
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3300
        public async Task DontRemoveCastToDifferentTypeWithSameName()
3301 3302 3303 3304
        {
            // Note: The cast below cannot be removed because the it results in
            // a different overload being picked.

C
CyrusNajmabadi 已提交
3305
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3306
@"using System;
3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321
using MyInt = System.Int32;

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

class A
{
3322
    static void Goo(int x)
C
CyrusNajmabadi 已提交
3323 3324 3325 3326
    {
        Console.WriteLine(""int"");
    }

3327
    static void Goo(MyInt x)
C
CyrusNajmabadi 已提交
3328 3329 3330 3331
    {
        Console.WriteLine(""MyInt"");
    }

3332 3333
    static void Main()
    {
3334
        Goo([|(MyInt)0|]);
3335
    }
C
CyrusNajmabadi 已提交
3336
}");
3337 3338
        }

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

C
CyrusNajmabadi 已提交
3346
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3347
@"using System;
3348 3349 3350 3351 3352 3353 3354 3355 3356 3357

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

class MyAttributeAttribute : Attribute
{
C
CyrusNajmabadi 已提交
3358 3359 3360 3361 3362 3363 3364 3365
    public MyAttributeAttribute(EEEnum e)
    {
    }

    public MyAttributeAttribute(short e)
    {
    }

3366
    public void Goo(EEEnum e)
C
CyrusNajmabadi 已提交
3367 3368 3369
    {
    }

3370
    public void Goo(short e)
C
CyrusNajmabadi 已提交
3371 3372 3373
    {
    }

3374 3375 3376
    [MyAttribute([|(EEEnum)0x0|])]
    public void Bar()
    {
3377
        Goo((EEEnum)0x0);
3378
    }
C
CyrusNajmabadi 已提交
3379
}");
3380 3381
        }

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

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 已提交
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 DontRemoveCastOnArgumentsWithOtherDynamicArguments()
3411
        {
C
CyrusNajmabadi 已提交
3412
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3413 3414
@"using System;

3415 3416 3417 3418
class Program
{
    static void Main()
    {
3419
        C<string>.InvokeGoo(0);
3420 3421
    }
}
C
CyrusNajmabadi 已提交
3422

3423 3424
class C<T>
{
3425
    public static void InvokeGoo(dynamic x)
3426
    {
3427
        Console.WriteLine(Goo(x, [|(object)""""|], """"));
3428 3429
    }

3430
    static void Goo(int x, string y, T z)
C
CyrusNajmabadi 已提交
3431 3432 3433
    {
    }

3434
    static bool Goo(int x, object y, object z)
C
CyrusNajmabadi 已提交
3435 3436 3437 3438
    {
        return true;
    }
}");
3439 3440
        }

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

C
CyrusNajmabadi 已提交
3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470
        set
        {
        }
    }

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

        set
        {
        }
    }
3471

3472
    void Goo(dynamic xx)
3473
    {
C
CyrusNajmabadi 已提交
3474
        var y = this[x: xx, s: """", d: [|(object)""""|]];
3475
    }
C
CyrusNajmabadi 已提交
3476
}");
3477 3478
        }

J
Jared Parsons 已提交
3479
        [WorkItem(627107, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/627107")]
3480
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3481
        public async Task DontRemoveCastOnArgumentsWithDynamicReceiverOpt()
3482
        {
C
CyrusNajmabadi 已提交
3483
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3484
@"class C
3485
{
3486
    static bool Goo(dynamic d)
3487 3488 3489 3490
    {
        d([|(object)""""|]);
        return true;
    }
C
CyrusNajmabadi 已提交
3491
}");
3492 3493
        }

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

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

J
Jared Parsons 已提交
3524
        [WorkItem(627107, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/627107")]
3525
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3526
        public async Task DontRemoveCastOnArgumentsWithDynamicReceiverOpt_3()
3527
        {
C
CyrusNajmabadi 已提交
3528
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3529
@"class C
3530
{
3531
    static bool Goo(dynamic d)
3532
    {
3533
        d.goo().bar().goo([|(object)""""|]);
3534 3535
        return true;
    }
C
CyrusNajmabadi 已提交
3536
}");
3537 3538
        }

J
Jared Parsons 已提交
3539
        [WorkItem(627107, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/627107")]
3540
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3541
        public async Task DontRemoveCastOnArgumentsWithOtherDynamicArguments_1()
3542
        {
C
CyrusNajmabadi 已提交
3543
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3544 3545
@"using System;

3546 3547 3548 3549
class Program
{
    static void Main()
    {
3550
        C<string>.InvokeGoo(0);
3551 3552 3553 3554 3555
    }
}

class C<T>
{
3556
    public static void InvokeGoo(dynamic x)
3557
    {
3558
        Console.WriteLine(Goo([|(object)""""|], x, """"));
3559 3560
    }

3561
    static void Goo(string y, int x, T z)
C
CyrusNajmabadi 已提交
3562 3563
    {
    }
3564

3565
    static bool Goo(object y, int x, object z)
C
CyrusNajmabadi 已提交
3566 3567 3568 3569
    {
        return true;
    }
}");
3570 3571
        }

J
Jared Parsons 已提交
3572
        [WorkItem(545998, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545998")]
3573
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3574
        public async Task DontRemoveCastWhichWouldChangeAttributeOverloadResolution2()
3575 3576 3577 3578
        {
            // Note: The cast below cannot be removed because it would result in
            // a different attribute constructor being picked

C
CyrusNajmabadi 已提交
3579
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3580 3581
@"using System;

3582 3583 3584
[A(new[] { [|(long)0|] })]
class A : Attribute
{
C
CyrusNajmabadi 已提交
3585 3586 3587 3588
    public A(long[] x)
    {
    }
}");
3589 3590
        }

J
Jared Parsons 已提交
3591
        [WorkItem(529894, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529894")]
3592
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3593
        public async Task DontUnnecessaryCastFromEnumToUint()
3594
        {
C
CyrusNajmabadi 已提交
3595
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3596
@"using System;
3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609

enum E
{
    X = -1
}

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

J
Jared Parsons 已提交
3613
        [WorkItem(529846, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/529846")]
3614
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3615
        public async Task DontUnnecessaryCastFromTypeParameterToObject()
3616
        {
C
CyrusNajmabadi 已提交
3617
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3618
@"class C
3619
{
3620
    static void Goo<T>(T x, object y)
3621
    {
C
CyrusNajmabadi 已提交
3622 3623 3624
        if ([|(object)|]x == y)
        {
        }
3625
    }
C
CyrusNajmabadi 已提交
3626
}");
3627 3628
        }

J
Jared Parsons 已提交
3629
        [WorkItem(640136, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/640136")]
3630
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3631
        public async Task RemoveUnnecessaryCastAndParseCorrect()
3632
        {
C
CyrusNajmabadi 已提交
3633
            await TestInRegularAndScriptAsync(
3634 3635 3636 3637 3638 3639
@"
using System;
using System.Threading.Tasks;
 
class C
{
3640
    void Goo(Task<Action> x)
3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652
    {
        (([|(Task<Action>)x|]).Result)();
    }
}
",

@"
using System;
using System.Threading.Tasks;
 
class C
{
3653
    void Goo(Task<Action> x)
3654
    {
3655
        (x.Result)();
3656 3657
    }
}
3658
");
3659 3660
        }

J
Jared Parsons 已提交
3661
        [WorkItem(626026, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/626026")]
3662
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3663
        public async Task DontRemoveCastIfUserDefinedExplicitCast()
3664
        {
C
CyrusNajmabadi 已提交
3665
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3666
@"class Program
3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684
{
    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 已提交
3685
}");
3686 3687
        }

J
Jared Parsons 已提交
3688
        [WorkItem(768895, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/768895")]
3689
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3690
        public async Task DontRemoveNecessaryCastInTernary()
3691
        {
C
CyrusNajmabadi 已提交
3692
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3693
@"class Program
3694 3695 3696 3697 3698 3699
{
    static void Main(string[] args)
    {
        object x = null;
        int y = [|(bool)x|] ? 1 : 0;
    }
C
CyrusNajmabadi 已提交
3700
}");
3701 3702
        }

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

3724 3725 3726 3727 3728 3729
    enum E
    {
        A,
        B,
        C
    }
C
CyrusNajmabadi 已提交
3730
}");
3731 3732
        }

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

class C
{
}

class D : C
{
3755 3756 3757 3758
}");
        }

        [WorkItem(3254, "https://github.com/dotnet/roslyn/issues/3254")]
3759
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3760
        public async Task DontRemoveCastToTypeParameterWithExceptionConstraint()
3761
        {
C
CyrusNajmabadi 已提交
3762
            await TestMissingInRegularAndScriptAsync(
3763 3764 3765 3766
@"using System;

class Program
{
C
CyrusNajmabadi 已提交
3767
    private static void RequiresCondition<TException>(bool condition, string messageOnFalseCondition) where TException : Exception
3768 3769 3770 3771 3772 3773 3774 3775 3776 3777
    {
        if (!condition)
        {
            throw [|(TException)Activator.CreateInstance(typeof(TException), messageOnFalseCondition)|];
        }
    }
}");
        }

        [WorkItem(3254, "https://github.com/dotnet/roslyn/issues/3254")]
3778
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3779
        public async Task DontRemoveCastToTypeParameterWithExceptionSubTypeConstraint()
3780
        {
C
CyrusNajmabadi 已提交
3781
            await TestMissingInRegularAndScriptAsync(
3782 3783 3784 3785
@"using System;

class Program
{
C
CyrusNajmabadi 已提交
3786
    private static void RequiresCondition<TException>(bool condition, string messageOnFalseCondition) where TException : ArgumentException
3787 3788 3789 3790 3791 3792
    {
        if (!condition)
        {
            throw [|(TException)Activator.CreateInstance(typeof(TException), messageOnFalseCondition)|];
        }
    }
3793
}");
3794
        }
3795 3796 3797 3798 3799

        [WorkItem(8111, "https://github.com/dotnet/roslyn/issues/8111")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task DontRemoveCastThatChangesShapeOfAnonymousTypeObject()
        {
C
CyrusNajmabadi 已提交
3800
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3801
@"class Program
3802 3803 3804 3805 3806
{
    static void Main(string[] args)
    {
        object thing = new { shouldBeAnInt = [|(int)Directions.South|] };
    }
C
CyrusNajmabadi 已提交
3807 3808 3809 3810 3811 3812 3813 3814 3815

    public enum Directions
    {
        North,
        East,
        South,
        West
    }
}");
3816 3817 3818 3819 3820 3821
        }

        [WorkItem(8111, "https://github.com/dotnet/roslyn/issues/8111")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task RemoveCastThatDoesntChangeShapeOfAnonymousTypeObject()
        {
C
CyrusNajmabadi 已提交
3822
            await TestInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3823
@"class Program
3824 3825 3826 3827 3828 3829
{
    static void Main(string[] args)
    {
        object thing = new { shouldBeAnInt = [|(Directions)Directions.South|] };
    }

C
CyrusNajmabadi 已提交
3830 3831 3832 3833 3834 3835 3836 3837 3838 3839
    public enum Directions
    {
        North,
        East,
        South,
        West
    }
}",

@"class Program
3840 3841 3842 3843 3844
{
    static void Main(string[] args)
    {
        object thing = new { shouldBeAnInt = Directions.South };
    }
C
CyrusNajmabadi 已提交
3845 3846 3847 3848 3849 3850 3851 3852 3853

    public enum Directions
    {
        North,
        East,
        South,
        West
    }
}");
3854
        }
3855 3856 3857 3858

        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task Tuple()
        {
C
CyrusNajmabadi 已提交
3859
            await TestInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872
@"class C
{
    void Main()
    {
        (int, string) tuple = [|((int, string))(1, ""hello"")|];
    }
}",
@"class C
{
    void Main()
    {
        (int, string) tuple = (1, ""hello"");
    }
C
CyrusNajmabadi 已提交
3873
}");
3874 3875 3876 3877 3878
        }

        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task TupleWithDifferentNames()
        {
C
CyrusNajmabadi 已提交
3879
            await TestInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892
@"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 已提交
3893
}");
3894
        }
3895

3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918
        [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;
    }
}");
        }

3919 3920 3921 3922 3923
        [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 已提交
3924
            await TestMissingInRegularAndScriptAsync(
C
CyrusNajmabadi 已提交
3925
@"using System;
3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944

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;
        }
    }
3945 3946 3947 3948 3949 3950 3951
}");
        }

        [WorkItem(17029, "https://github.com/dotnet/roslyn/issues/17029")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task DontRemoveCastOnEnumComparison1()
        {
C
CyrusNajmabadi 已提交
3952
            await TestMissingInRegularAndScriptAsync(
3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973
@"
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 已提交
3974
            await TestMissingInRegularAndScriptAsync(
3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988
@"
enum TransferTypeKey
{
    Transfer,
    TransferToBeneficiary
}

class Program
{
    static void Main(dynamic p)
    {
        if ([|(int)TransferTypeKey.TransferToBeneficiary|] != p.TYP)
          throw new InvalidOperationException();
    }
C
CyrusNajmabadi 已提交
3989
}");
3990
        }
3991 3992 3993

        [WorkItem(18978, "https://github.com/dotnet/roslyn/issues/18978")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
3994
        public async Task DontRemoveCastOnCallToMethodWithParamsArgs()
3995 3996 3997 3998 3999 4000 4001 4002
        {
            await TestMissingInRegularAndScriptAsync(
@"
class Program
{
    public static void Main(string[] args)
    {
        var takesArgs = new[] { ""Hello"", ""World"" };
4003
        TakesParams([|(object)|]takesArgs);
4004 4005
    }

4006
    private static void TakesParams(params object[] goo)
4007
    {
4008
        Console.WriteLine(goo.Length);
4009 4010 4011
    }
}");
        }
4012

4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031
        [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);
    }
}");
        }

4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045
        [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);
    }

4046
    private static void TakesParams(params object[] goo)
4047
    {
4048
        System.Console.WriteLine(goo.Length);
4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059
    }
}",
@"
class Program
{
    public static void Main(string[] args)
    {
        var takesArgs = new[] { ""Hello"", ""World"" };
        TakesParams(takesArgs);
    }

4060
    private static void TakesParams(params object[] goo)
4061
    {
4062
        System.Console.WriteLine(goo.Length);
4063 4064 4065
    }
}");
        }
V
Victor Zaytsev 已提交
4066

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
        [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()
  {
  }
}");
        }

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 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167
        [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 已提交
4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 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 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230
        [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
{
}");
        }

4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251
        [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 已提交
4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283
        [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
{
4284 4285
}");
        }
4286

4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321
        [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 已提交
4322
        [WorkItem(18510, "https://github.com/dotnet/roslyn/issues/18510")]
V
Victor Zaytsev 已提交
4323 4324 4325 4326
        [Theory, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        [InlineData("-")]
        [InlineData("+")]
        public async Task DontRemoveCastOnInvalidUnaryOperatorEnumValue1(string op)
V
Victor Zaytsev 已提交
4327
        {
V
Victor Zaytsev 已提交
4328
            await TestMissingInRegularAndScriptAsync(
V
Victor Zaytsev 已提交
4329
$@"
V
Victor Zaytsev 已提交
4330
enum Sign
V
Victor Zaytsev 已提交
4331
    {{
V
Victor Zaytsev 已提交
4332 4333
        Positive = 1,
        Negative = -1
V
Victor Zaytsev 已提交
4334
    }}
V
Victor Zaytsev 已提交
4335 4336

    class T
V
Victor Zaytsev 已提交
4337
    {{
4338
        void Goo()
V
Victor Zaytsev 已提交
4339
        {{
V
Victor Zaytsev 已提交
4340
            Sign mySign = Sign.Positive;
V
Victor Zaytsev 已提交
4341 4342 4343
            Sign invertedSign = (Sign) ( [|{op}((int) mySign)|] );
        }}
    }}");
V
Victor Zaytsev 已提交
4344 4345 4346
        }

        [WorkItem(18510, "https://github.com/dotnet/roslyn/issues/18510")]
V
Victor Zaytsev 已提交
4347 4348 4349 4350
        [Theory, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        [InlineData("-")]
        [InlineData("+")]
        public async Task DontRemoveCastOnInvalidUnaryOperatorEnumValue2(string op)
V
Victor Zaytsev 已提交
4351 4352
        {
            await TestMissingInRegularAndScriptAsync(
V
Victor Zaytsev 已提交
4353
$@"
V
Victor Zaytsev 已提交
4354
enum Sign
V
Victor Zaytsev 已提交
4355
    {{
V
Victor Zaytsev 已提交
4356 4357
        Positive = 1,
        Negative = -1
V
Victor Zaytsev 已提交
4358
    }}
V
Victor Zaytsev 已提交
4359 4360

    class T
V
Victor Zaytsev 已提交
4361
    {{
4362
        void Goo()
V
Victor Zaytsev 已提交
4363
        {{
V
Victor Zaytsev 已提交
4364
            Sign mySign = Sign.Positive;
V
Victor Zaytsev 已提交
4365 4366 4367
            Sign invertedSign = (Sign) ( [|{op}(int) mySign|] );
        }}
    }}");
V
Victor Zaytsev 已提交
4368 4369 4370 4371
        }

        [WorkItem(18510, "https://github.com/dotnet/roslyn/issues/18510")]
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
V
Victor Zaytsev 已提交
4372
        public async Task RemoveCastOnValidUnaryOperatorEnumValue()
V
Victor Zaytsev 已提交
4373
        {
V
Victor Zaytsev 已提交
4374
            await TestInRegularAndScriptAsync(
V
Victor Zaytsev 已提交
4375 4376 4377 4378 4379 4380 4381 4382 4383
@"
enum Sign
    {
        Positive = 1,
        Negative = -1
    }

    class T
    {
4384
        void Goo()
V
Victor Zaytsev 已提交
4385 4386
        {
            Sign mySign = Sign.Positive;
V
Victor Zaytsev 已提交
4387
            Sign invertedSign = (Sign) ( [|~(int) mySign|] );
V
Victor Zaytsev 已提交
4388
        }
V
Victor Zaytsev 已提交
4389
    }",
V
Victor Zaytsev 已提交
4390 4391 4392 4393 4394 4395 4396 4397 4398
@"
enum Sign
    {
        Positive = 1,
        Negative = -1
    }

    class T
    {
4399
        void Goo()
V
Victor Zaytsev 已提交
4400 4401
        {
            Sign mySign = Sign.Positive;
4402
            Sign invertedSign = (Sign) ( ~mySign);
V
Victor Zaytsev 已提交
4403
        }
V
Victor Zaytsev 已提交
4404 4405
    }");
        }
4406

Š
Šimon Koníček 已提交
4407
        [WorkItem(25456, "https://github.com/dotnet/roslyn/issues/25456#issuecomment-373549735")]
4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task DontIntroduceDefaultLiteralInSwitchCase()
        {
            await TestMissingInRegularAndScriptAsync(
@"
class C
{
    void M()
    {
        switch (true)
        {
            case [|(bool)default|]:
                break;
        }
    }
4423
}", parameters: new TestParameters(new CSharpParseOptions(LanguageVersion.CSharp7_1)));
4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440
        }

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

        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task DontIntroduceDefaultLiteralInSwitchCase_DefaultInsideParentheses()
        {
            await TestMissingInRegularAndScriptAsync(
@"
class C
{
    void M()
    {
        switch (true)
        {
            case [|(bool)(default)|]:
                break;
        }
    }
4459
}", parameters: new TestParameters(new CSharpParseOptions(LanguageVersion.CSharp7_1)));
4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484
        }

        [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)
        {
4485
            case (bool)default:
4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506
                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;
        }
    }
4507
}", parameters: new TestParameters(new CSharpParseOptions(LanguageVersion.CSharp7_1)));
4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524
        }

        [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;
        }
    }
4525
}", parameters: new TestParameters(new CSharpParseOptions(LanguageVersion.CSharp7_1)));
4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542
        }

        [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;
        }
    }
4543
}", parameters: new TestParameters(new CSharpParseOptions(LanguageVersion.CSharp7_1)));
4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568
        }

        [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)
        {
4569
            case (bool)default when true:
4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598
                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)
        {
4599
            case (bool)default when default:
4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616
                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|]);
    }
4617
}", parameters: new TestParameters(new CSharpParseOptions(LanguageVersion.CSharp7_1)));
4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630
        }

        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task DontIntroduceDefaultLiteralInPatternIs_CastInsideParentheses()
        {
            await TestMissingInRegularAndScriptAsync(
@"
class C
{
    void M()
    {
        if (true is ([|(bool)default|]));
    }
4631
}", parameters: new TestParameters(new CSharpParseOptions(LanguageVersion.CSharp7_1)));
4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644
        }

        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
        public async Task DontIntroduceDefaultLiteralInPatternIs_DefaultInsideParentheses()
        {
            await TestMissingInRegularAndScriptAsync(
@"
class C
{
    void M()
    {
        if (true is [|(bool)(default)|]);
    }
4645
}", parameters: new TestParameters(new CSharpParseOptions(LanguageVersion.CSharp7_1)));
4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664
        }

        [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()
    {
4665
        if (true is (bool)default) ;
4666 4667 4668
    }
}", parameters: new TestParameters(new CSharpParseOptions(LanguageVersion.CSharp7_1)));
        }
4669

D
David Wengier 已提交
4670
        [WorkItem(27239, "https://github.com/dotnet/roslyn/issues/27239")]
4671
        [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
4672
        public async Task DontOfferToRemoveCastWhereNoConversionExists()
4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684
        {
            await TestMissingInRegularAndScriptAsync(
                @"
using System;

class C
{
    void M()
    {
        object o = null;
        TypedReference r2 = [|(TypedReference)o|];
    }
4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708
}");
        }

        [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"";
    }
4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771
}");
        }

        [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|])
    {
    }
4772 4773
}");
        }
4774
    }
S
Sam Harwell 已提交
4775
}