public async Task TestNotWithRefReturns() { var code = @"using System; class Program { static ref int GetRef(int[] mem, int addr, int mode) { switch (mode) { case 0: return ref mem[mem[addr]]; case 1: return ref mem[addr]; default: throw new Exception(); } } }"; await VerifyCS.VerifyCodeFixAsync(code, code); }
public async Task NotOnGenericClass() { var text = @"class Test { class Goo<T> { class Bar { void M() { _ = typeof(Bar).Name; } } } } "; await VerifyCS.VerifyCodeFixAsync(text, text); }
public async Task Method_Task_BlockBody_WithLocalFunction() { await VerifyCS.VerifyCodeFixAsync( @" using System.Threading.Tasks; class C { async Task {|CS1998:Goo|}() { if (GetTicks() > 0) { return; } long GetTicks() { return System.DateTime.Now.Ticks; } } }", @" using System.Threading.Tasks; class C { Task Goo() { if (GetTicks() > 0) { return Task.CompletedTask; } long GetTicks() { return System.DateTime.Now.Ticks; } return Task.CompletedTask; } }" ); }
public async Task TestTrivia_01() { await VerifyCS.VerifyCodeFixAsync( @"class Program { int M(int i) { // leading switch [|switch|] (i) // trailing switch { // leading label case 1: // trailing label // leading body return 4; // trailing body case 2: return 5; case 3: return 6; } // leading next statement throw null; // leading next statement } }", @"class Program { int M(int i) { // leading switch return i switch // trailing switch { // leading label // trailing label 1 => 4,// leading body // trailing body 2 => 5, 3 => 6, // leading next statement _ => throw null,// leading next statement }; } }"); }
public async Task Method_TaskOfT_ExpressionBody() { await VerifyCS.VerifyCodeFixAsync( @" using System.Threading.Tasks; class C { async Task<int> {|CS1998:Goo|}() => 2; }", @" using System.Threading.Tasks; class C { Task<int> Goo() => Task.FromResult(2); }" ); }
public async Task TestFixAll() { var code = @" public class C { public bool M1(bool x) { return true [|==|] x; } public bool M2(bool x) { return x [|!=|] false; } public bool M3(bool x) { return x [|==|] true [|==|] true; } }"; var fixedCode = @" public class C { public bool M1(bool x) { return x; } public bool M2(bool x) { return x; } public bool M3(bool x) { return x; } }"; await VerifyCS.VerifyCodeFixAsync(code, fixedCode); }
public async Task Method_Task_BlockBody_WithLambda() { await VerifyCS.VerifyCodeFixAsync( @" using System.Threading.Tasks; class C { async Task {|CS1998:Goo|}() { System.Func<long> getTicks = () => { return System.DateTime.Now.Ticks; }; if (getTicks() > 0) { return; } } }", @" using System.Threading.Tasks; class C { Task Goo() { System.Func<long> getTicks = () => { return System.DateTime.Now.Ticks; }; if (getTicks() > 0) { return Task.CompletedTask; } return Task.CompletedTask; } }" ); }
public async Task TestAssignment_Array() { await VerifyCS.VerifyCodeFixAsync( @"class Program { int[] array = new int[1]; void M(int i) { [|switch|] (i) { case 1: array[0] = 4; break; case 2: array[0] = 5; break; case 3: array[0] = 6; break; default: array[0] = 7; break; } } }", @"class Program { int[] array = new int[1]; void M(int i) { array[0] = i switch { 1 => 4, 2 => 5, 3 => 6, _ => 7, }; } }"); }
public async Task TestMissingOnAllBreak() { var code = @"class Program { void M(int i) { switch (i) { case 1: break; case 2: break; case 3: break; } } }"; await VerifyCS.VerifyCodeFixAsync(code, code); }
public async Task TestNotWithRefConditionalAssignment() { var code = @"using System; class Program { static ref int GetRef(int[] mem, int addr, int mode) { ref int i = ref addr; switch (mode) { case 0: i = ref true ? ref mem[mem[addr]] : ref mem[mem[addr]]; break; default: throw new Exception(); } return ref mem[addr]; } }"; await VerifyCS.VerifyCodeFixAsync(code, code); }
public async Task TestQueryableIsNotConsidered(string methodName) { var source = $@" using System; using System.Linq; using System.Collections.Generic; namespace demo {{ class Test {{ void M() {{ List<int> testvar1 = new List<int> {{ 1, 2, 3, 4, 5, 6, 7, 8 }}; IQueryable<int> testvar2 = testvar1.AsQueryable().Where(x => x % 2 == 0); var output = testvar2.Where(x => x == 4).{methodName}(); }} }} }}"; await VerifyCS.VerifyAnalyzerAsync(source); }
public async Task TestMissingOnGoto() { var code = @"class Program { int M(int i) { switch (i) { case 1: return 0; case 2: goto default; default: return 2; } } }"; await VerifyCS.VerifyCodeFixAsync(code, code); }
public async Task TestMissingOnUseInNextStatement() { var code = @"using System; class Program { public static void Throw(int index) { string name = """"; switch (index) { case 0: name = ""1""; break; case 1: name = ""2""; break; } throw new Exception(name); } }"; await VerifyCS.VerifyCodeFixAsync(code, code); }
public async Task PrimitiveType() { var text = @"class Test { void Method() { var typeName = [|typeof(int).Name|]; } } "; var expected = @"class Test { void Method() { var typeName = nameof(System.Int32); } } "; await VerifyCS.VerifyCodeFixAsync(text, expected); }
public async Task TestMissingOnMultiCaseSection() { var code = @"class Program { void M(int i) { int j; switch (i) { case 1: case 2: j = 4; break; } throw null; } }"; await VerifyCS.VerifyCodeFixAsync(code, code); }
public async Task TestSimpleCaseForNotEqualsFalse() { var code = @" public class C { public bool M1(bool x) { return x [|!=|] false; } }"; var fixedCode = @" public class C { public bool M1(bool x) { return x; } }"; await VerifyCS.VerifyCodeFixAsync(code, fixedCode); }
public async Task TestInArgument() { var code = @" public class C { public bool M1(bool x) { return M1(x [|==|] true); } }"; var fixedCode = @" public class C { public bool M1(bool x) { return M1(x); } }"; await VerifyCS.VerifyCodeFixAsync(code, fixedCode); }
public async Task TestOnLeftHandSide() { var code = @" public class C { public bool M1(bool x) { return true [|==|] x; } }"; var fixedCode = @" public class C { public bool M1(bool x) { return x; } }"; await VerifyCS.VerifyCodeFixAsync(code, fixedCode); }
public async Task TestMissingOnReachableLabel() { var code = @" class C { void M(object o) { if (o != null) { goto label; } throw new System.Exception(); [| var v = 0; |] label: System.Console.WriteLine(); var y = 1; } }"; var fixedCode = @" class C { void M(object o) { if (o != null) { goto label; } throw new System.Exception(); label: System.Console.WriteLine(); var y = 1; } }"; await VerifyCS.VerifyCodeFixAsync(code, fixedCode); }
public async Task TestFixAll4() { await VerifyCS.VerifyCodeFixAsync( @" class C { void M(object o) { for (int i = 0; i < 10; i = i + 1) { for (int j = 0; j < 10; j = j + 1) { goto stop; [| goto outerLoop; |] } outerLoop: return; } stop: return; } }", @" class C { void M(object o) { for (int i = 0; i < 10; i = i + 1) { for (int j = 0; j < 10; j = j + 1) { goto stop; } outerLoop: return; } stop: return; } }"); }
public async Task TestImplicitConversion() { await VerifyCS.VerifyCodeFixAsync( @"using System; class C { public C(String s) => _s = s; private readonly String _s; public static implicit operator String(C value) => value._s; public static implicit operator C(String value) => new C(value); public bool method(C c) { [|switch|] (c) { case ""A"": return true; default: return false; } } }", @"using System; class C { public C(String s) => _s = s; private readonly String _s; public static implicit operator String(C value) => value._s; public static implicit operator C(String value) => new C(value); public bool method(C c) { return (string)c switch { ""A"" => true, _ => false, }; } }" ); }
public async Task Method_Task_EmptyBlockBody() { await VerifyCS.VerifyCodeFixAsync( @" using System.Threading.Tasks; class C { async Task {|CS1998:Goo|}(){} }", @" using System.Threading.Tasks; class C { Task Goo() { return Task.CompletedTask; } }"); }
public async Task TestInfiniteForLoop() { await VerifyCS.VerifyCodeFixAsync( @" class C { void M() { for (;;) { } [| return; |] } }", @" class C { void M() { for (;;) { } } }"); }
public async Task TestSingleUnreachableStatement() { await VerifyCS.VerifyCodeFixAsync( @" class C { void M() { throw new System.Exception(); [| var v = 0; |] } }", @" class C { void M() { throw new System.Exception(); } }"); }
public async Task TestFixAll2() { await VerifyCS.VerifyCodeFixAsync( @" class C { void M(object o) { if (o == null) { goto ReachableLabel; } throw new System.Exception(); [| var v = 0; |][| UnreachableLabel: System.Console.WriteLine(o); |] ReachableLabel: System.Console.WriteLine(o.ToString()); } }", @" class C { void M(object o) { if (o == null) { goto ReachableLabel; } throw new System.Exception(); ReachableLabel: System.Console.WriteLine(o.ToString()); } }"); }
public async Task FixAllInDocumentExplicitCall() { var testCode = @" using System; using System.Linq; using System.Collections.Generic; class C { static void M() { IEnumerable<string> test = new List<string> { ""hello"", ""world"", ""!"" }; var test1 = [|Enumerable.Where(test, x => x.Equals(""!"")).Any()|]; var test2 = [|Enumerable.Where(test, x => x.Equals(""!"")).SingleOrDefault()|]; var test3 = [|Enumerable.Where(test, x => x.Equals(""!"")).Last()|]; var test4 = [|Enumerable.Where(test, x => x.Equals(""!"")).Count()|]; var test5 = [|Enumerable.Where(test, x => x.Equals(""!"")).FirstOrDefault()|]; } }"; var fixedCode = @" using System; using System.Linq; using System.Collections.Generic; class C { static void M() { IEnumerable<string> test = new List<string> { ""hello"", ""world"", ""!"" }; var test1 = Enumerable.Any(test, x => x.Equals(""!"")); var test2 = Enumerable.SingleOrDefault(test, x => x.Equals(""!"")); var test3 = Enumerable.Last(test, x => x.Equals(""!"")); var test4 = Enumerable.Count(test, x => x.Equals(""!"")); var test5 = Enumerable.FirstOrDefault(test, x => x.Equals(""!"")); } }"; await VerifyCS.VerifyCodeFixAsync(testCode, fixedCode); }
public async Task TestAssignment_UseBeforeAssignment() { await VerifyCS.VerifyCodeFixAsync( @"class Program { void M(int i) { int j = 123; M(i); [|switch|] (i) { case 1: j = 4; break; case 2: j = 5; break; case 3: j = 6; break; } throw null; } }", @"class Program { void M(int i) { int j = 123; M(i); j = i switch { 1 => 4, 2 => 5, 3 => 6, _ => throw null, }; } }"); }
public async Task FixAllDocumentVariedSingleLine() { var input = @"class Test { static void Main() { var typeName1 = [|typeof(Test).Name|]; var typeName2 = [|typeof(int).Name|]; var typeName3 = [|typeof(System.String).Name|]; } } "; var expected = @"class Test { static void Main() { var typeName1 = nameof(Test); var typeName2 = nameof(System.Int32); var typeName3 = nameof(System.String); } } "; await VerifyCS.VerifyCodeFixAsync(input, expected); }
public async Task LocalFunction_TaskOfT_ExpressionBody() { await VerifyCS.VerifyCodeFixAsync( @"using System.Threading.Tasks; class C { public void M1() { async Task<int> {|CS1998:Goo|}() => 1; } }", @"using System.Threading.Tasks; class C { public void M1() { Task<int> Goo() => Task.FromResult(1); } }"); }
public async Task TestMissingOnAssignmentMismatch() { var code = @"class Program { int M(int i) { int j = 0; switch (i) { case 1: j = 4; break; case 2: j += 5; break; } return j; } }"; await VerifyCS.VerifyCodeFixAsync(code, code); }