public async Task TestSyntaxErrorAtEndOfFileAsync() { string testCode = @"namespace StyleCopAnalyzers_Test { public class Class1 { private void Test() { var hello = ""world""; } } } ? "; DiagnosticResult[] expected = { DiagnosticResult.CompilerError("CS1031").WithMessage("Type expected").WithLocation(10, 2), }; await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); }
public async Task TestIncompleteMemberAsync() { string testCode = @"public class TestClass { public string Test; public static string public static string } "; // We don't care about the syntax errors. DiagnosticResult[] expected = { // /0/Test0.cs(5,5): error CS1585: Member modifier 'public' must precede the member type and name DiagnosticResult.CompilerError("CS1585").WithLocation(5, 5).WithArguments("public"), // /0/Test0.cs(6,1): error CS1519: Invalid token '}' in class, record, struct, or interface member declaration DiagnosticResult.CompilerError("CS1519").WithLocation(6, 1).WithArguments("}"), }; await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); }
public async Task CA1812_Basic_NoDiagnostic_MainMethodIsDifferentlyCased() { await new VerifyVB.Test { TestState = { OutputKind = OutputKind.ConsoleApplication, Sources = { @"Friend Class C Private Shared Sub mAiN() End Sub End Class", }, }, ExpectedDiagnostics = { // error BC30737: No accessible 'Main' method with an appropriate signature was found in 'TestProject'. DiagnosticResult.CompilerError("BC30737"), } }.RunAsync(); }
public async Task Test_CS8603_return_expression_in_parentheses() { var test = @"class Test { private object Method(object? target1, object? target2) { var x = target1?.ToString(); if (x == null) return new object(); return ({|#0:target2 ?? target1|}); } }"; var suppressed = new[] { DiagnosticResult.CompilerError("CS8603").WithLocation(0) }; await VerifyCS.VerifySuppressorAsync(test, suppressed); }
public async Task IgnoresCodeWithCallToNonStaticFromError() { this.TestCode = @" using Remora.Results; public class Program { public static void Main() { Result a; Result b; a.FromError(b); } } "; this.ExpectedDiagnostics.Clear(); this.ExpectedDiagnostics.Add(DiagnosticResult.CompilerError("CS0176").WithLocation(10, 21)); await RunAsync(); }
public async Task TestAccessorWithoutBodyAsync() { var testCode = @" public class Foo { public int Prop { get; set { } } }"; DiagnosticResult[] expected = { DiagnosticResult.CompilerError("CS0501").WithMessage("'Foo.Prop.get' must declare a body because it is not marked abstract, extern, or partial").WithLocation(6, 9), }; await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); }
public async Task TestOverloadResolutionFailureAsync() { var testCode = @" using System; using System.Linq.Expressions; public class TypeName { public void Test(string argument) { } public void Test() { Test(delegate { }); } }"; var expected = DiagnosticResult.CompilerError("CS1660").WithLocation(13, 14); await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); }
public async Task TestInvalidOverloadAsync() { var testCode = @" using System; using System.Linq.Expressions; public unsafe class TypeName { void Method(int* data) { throw null; } void Caller() => Method(delegate { }); }"; var fixedCode = @" using System; using System.Linq.Expressions; public unsafe class TypeName { void Method(int* data) { throw null; } void Caller() => Method(delegate { }); }"; var expected = DiagnosticResult.CompilerError("CS1660").WithLocation(7, 29); await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false); }
public async Task Test_CS8602_expression() { var test = @"class Test { private void Method(object? target1, object? target2) { var x = target1?.ToString(); if (x == null) return; var y = ({|#0:target2 ?? target1|}).ToString(); } }"; var suppressed = new[] { DiagnosticResult.CompilerError("CS8602").WithLocation(0) }; await VerifyCS.VerifySuppressorAsync(test, suppressed); }
public async Task TestNotOnFileWithNoGlobalStatements() { var code = @" $$ class C { } "; await new VerifyCS.Test { TestCode = code, FixedCode = code, LanguageVersion = LanguageVersion.CSharp10, TestState = { OutputKind = OutputKind.ConsoleApplication }, ExpectedDiagnostics = { // error CS5001: Program does not contain a static 'Main' method suitable for an entry point DiagnosticResult.CompilerError("CS5001"), } }.RunAsync(); }
public async Task MethodWithForEachVariableAwait() { var source = @"class C { async System.Threading.Tasks.Task MAsync() { await foreach (var (a, b) in {|#0:new(int, int)[] { }|}) { } } }"; await VerifyCS.VerifyCodeFixAsync( source, // /0/Test0.cs(5,38): error CS1061: 'bool' does not contain a definition for 'GetAwaiter' and no accessible extension method 'GetAwaiter' accepting a first argument of type 'bool' could be found (are you missing a using directive or an assembly reference?) DiagnosticResult .CompilerError("CS1061") .WithLocation(0) .WithArguments("bool", "GetAwaiter"), source ); }
public async Task TestIncompleteMemberAsync() { // Tests that the analyzer does not crash on incomplete members string testCode = @"public interface OuterType { event System.Action TestEvent; public string public string } "; // We don't care about the syntax errors. var expected = new[] { // /0/Test0.cs(5,5): error CS1585: Member modifier 'public' must precede the member type and name DiagnosticResult.CompilerError("CS1585").WithLocation(5, 5).WithArguments("public"), // /0/Test0.cs(6,1): error CS1519: Invalid token '}' in class, record, struct, or interface member declaration DiagnosticResult.CompilerError("CS1519").WithLocation(6, 1).WithArguments("}"), }; await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); }
protected override DiagnosticResult[] GetExpectedResultTestRegressionMethodGlobalNamespace(string code) { if (code == "public void {|#0:TestMember|}() { }") { return(new[] { // /0/Test0.cs(4,1): error CS0106: The modifier 'public' is not valid for this item DiagnosticResult.CompilerError("CS0106").WithSpan(4, 1, 4, 7).WithArguments("public"), // /0/Test0.cs(4,1): error CS8320: Feature 'top-level statements' is not available in C# 7.2. Please use language version 9.0 or greater. DiagnosticResult.CompilerError("CS8320").WithSpan(4, 1, 4, 29).WithArguments("top-level statements", "9.0"), // /0/Test0.cs(4,1): error CS8805: Program using top-level statements must be an executable. DiagnosticResult.CompilerError("CS8805").WithSpan(4, 1, 4, 29), }); } return(new[] { DiagnosticResult.CompilerError("CS0116").WithMessage("A namespace cannot directly contain members such as fields, methods or statements").WithLocation(0), Diagnostic().WithLocation(0), }); }
public async Task VerifyInvalidMemberSyntaxInCodeFixAsync() { string testCode = @"class Program { static void Main(string[] args) { { }[;] } } "; DiagnosticResult[] expected = { DiagnosticResult.CompilerError("CS1513").WithLocation(6, 10), Diagnostic().WithLocation(6, 10), DiagnosticResult.CompilerError("CS1001").WithLocation(6, 11), DiagnosticResult.CompilerError("CS1001").WithLocation(6, 11), DiagnosticResult.CompilerError("CS1022").WithLocation(8, 1), }; await VerifyCSharpFixAsync(testCode, expected, testCode, CancellationToken.None).ConfigureAwait(false); }
public async Task ShouldNotBeValidWhenParameterIsUnreferenced() { var test = @" using Cake.Core; namespace CakeAddinTest { public static class CakeAddin { [{|#1:CakeMethodAlias|}] public static void {|#0:MyAliasMethod|}(this ICakeContext context, string ohyes) { ohyes = ohyes; } } }"; var expectedDiagnosics = new[] { DiagnosticResult.CompilerError("CS0246").WithLocation(1).WithArguments("CakeMethodAlias"), DiagnosticResult.CompilerError("CS0246").WithLocation(1).WithArguments("CakeMethodAliasAttribute"), VerifyCS.Diagnostic(Identifiers.AliasMethodMarkedRule).WithLocation(0).WithArguments("MyAliasMethod"), }; await VerifyCS.VerifyAnalyzerAsync(test, expectedDiagnosics); }
public async Task CA1812_Basic_NoDiagnostic_MainMethodIsDifferentlyCased() { await new VerifyVB.Test { TestCode = @"Friend Class C Private Shared Sub mAiN() End Sub End Class", SolutionTransforms = { (solution, projectId) => { var compilationOptions = solution.GetProject(projectId).CompilationOptions; return(solution.WithProjectCompilationOptions(projectId, compilationOptions.WithOutputKind(OutputKind.ConsoleApplication))); } }, ExpectedDiagnostics = { // error BC30737: No accessible 'Main' method with an appropriate signature was found in 'TestProject'. DiagnosticResult.CompilerError("BC30737"), } }.RunAsync(); }
public async Task MethodWithUsingNoAwait() { await VerifyCS.VerifyCodeFixAsync( @"class C { async System.Threading.Tasks.Task {|CS1998:MAsync|}() { using ({|#0:var x = new object()|}) { } } }", // /0/Test0.cs(5,16): error CS1674: 'object': type used in a using statement must be implicitly convertible to 'System.IDisposable'. DiagnosticResult.CompilerError("CS1674").WithLocation(0).WithArguments("object"), @"class C { void M() { using ({|#0:var x = new object()|}) { } } }"); }
public async Task VisualBasic_NoDiagnosticCases_NestedOperationAnalyzerRegistration() { var source = @" Imports System Imports System.Collections.Immutable Imports Microsoft.CodeAnalysis Imports Microsoft.CodeAnalysis.Diagnostics <DiagnosticAnalyzer(LanguageNames.VisualBasic)> _ MustInherit Class MyAnalyzer Inherits DiagnosticAnalyzer Public Overrides ReadOnly Property SupportedDiagnostics() As ImmutableArray(Of DiagnosticDescriptor) Get Throw New NotImplementedException() End Get End Property Public Overrides Sub Initialize(context As AnalysisContext) context.RegisterCompilationStartAction(Function(compilationContext) compilationContext.RegisterOperationBlockStartAction(Function(operationBlockContext) AnalyzeOperationBlockStart(operationBlockContext) End Function) End Function) context.RegisterCompilationStartAction(Function(compilationContext) compilationContext.RegisterOperationAction(AddressOf AnalyzeOperation, OperationKind.Invocation) End Function) context.RegisterCompilationStartAction(Function(compilationContext) compilationContext.RegisterOperationBlockAction(AddressOf AnalyzeOperationBlock) End Function) End Sub Private Shared Sub AnalyzeOperation(context As OperationAnalysisContext) End Sub Private Shared Sub AnalyzeOperationBlock(context As OperationBlockAnalysisContext) End Sub Private Shared Sub AnalyzeOperationBlockStart(context As OperationBlockStartAnalysisContext) context.RegisterOperationAction(AddressOf AnalyzeOperation, OperationKind.Invocation) End Sub End Class "; await new VerifyVB.Test { ReferenceAssemblies = ReferenceAssemblies.Default, TestState = { Sources = { source }, ExpectedDiagnostics = { // Test0.vb(7) : error BC30002: Type 'DiagnosticAnalyzer' is not defined. DiagnosticResult.CompilerError("BC30002").WithSpan(7, 2, 7, 20).WithArguments("DiagnosticAnalyzer"), // Test0.vb(7) : error BC30451: 'LanguageNames' is not declared. It may be inaccessible due to its protection level. DiagnosticResult.CompilerError("BC30451").WithSpan(7, 21, 7, 34).WithArguments("LanguageNames"), // Test0.vb(9) : error BC30002: Type 'DiagnosticAnalyzer' is not defined. DiagnosticResult.CompilerError("BC30002").WithSpan(9, 11, 9, 29).WithArguments("DiagnosticAnalyzer"), // Test0.vb(10) : error BC30284: property 'SupportedDiagnostics' cannot be declared 'Overrides' because it does not override a property in a base class. DiagnosticResult.CompilerError("BC30284").WithSpan(10, 37, 10, 57).WithArguments("property","SupportedDiagnostics"), // Test0.vb(10) : error BC30002: Type 'ImmutableArray' is not defined. DiagnosticResult.CompilerError("BC30002").WithSpan(10, 63, 10, 102).WithArguments("ImmutableArray"), // Test0.vb(10) : error BC30002: Type 'DiagnosticDescriptor' is not defined. DiagnosticResult.CompilerError("BC30002").WithSpan(10, 81, 10, 101).WithArguments("DiagnosticDescriptor"), // Test0.vb(16) : error BC30284: sub 'Initialize' cannot be declared 'Overrides' because it does not override a sub in a base class. DiagnosticResult.CompilerError("BC30284").WithSpan(16, 23, 16, 33).WithArguments("sub","Initialize"), // Test0.vb(16) : error BC30002: Type 'AnalysisContext' is not defined. DiagnosticResult.CompilerError("BC30002").WithSpan(16, 45, 16, 60).WithArguments("AnalysisContext"), // Test0.vb(32) : error BC30002: Type 'OperationAnalysisContext' is not defined. DiagnosticResult.CompilerError("BC30002").WithSpan(32, 49, 32, 73).WithArguments("OperationAnalysisContext"), // Test0.vb(35) : error BC30002: Type 'OperationBlockAnalysisContext' is not defined. DiagnosticResult.CompilerError("BC30002").WithSpan(35, 54, 35, 83).WithArguments("OperationBlockAnalysisContext"), // Test0.vb(38) : error BC30002: Type 'OperationBlockStartAnalysisContext' is not defined. DiagnosticResult.CompilerError("BC30002").WithSpan(38, 59, 38, 93).WithArguments("OperationBlockStartAnalysisContext"), // Test0.vb(39) : error BC30451: 'OperationKind' is not declared. It may be inaccessible due to its protection level. DiagnosticResult.CompilerError("BC30451").WithSpan(39, 63, 39, 76).WithArguments("OperationKind") }, } }.RunAsync(); }
public async Task Test_ComplexMethod() { var test = @" namespace N { using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; class C { class ProjectFile { public string? ProjectName { get; set; } public string? UniqueProjectName { get; set; } } private static FileInfo? FindProject(DirectoryInfo directory, string solutionFolder) { return null; } static void UpdateProjectNames(DirectoryInfo solutionFolder, IList<ProjectFile> allProjectFiles, CancellationToken? cancellationToken) { var fileNamesByDirectory = allProjectFiles.GroupBy(file => file.ToString()).ToArray(); var solutionFolderLength = solutionFolder.FullName.Length + 1; foreach (var directoryFiles in fileNamesByDirectory) { cancellationToken?.ThrowIfCancellationRequested(); var directoryPath = directoryFiles?.Key; if (string.IsNullOrEmpty(directoryPath)) continue; var directory = new DirectoryInfo(directoryPath); var project = FindProject(directory, solutionFolder.FullName); var projectName = directory.Name; string? uniqueProjectName = null; if (project != null) { projectName = Path.ChangeExtension(project.Name, null); var fullProjectName = project.FullName; if (fullProjectName.Length >= solutionFolderLength) // project found is in solution tree { uniqueProjectName = fullProjectName.Substring(solutionFolderLength); } } foreach (var file in directoryFiles) { file.ProjectName = projectName; file.UniqueProjectName = uniqueProjectName; } } } } } "; var supressed = new[] { DiagnosticResult.CompilerError("CS8602").WithSpan(58, 42, 58, 56), }; await VerifyCS.VerifySuppressorAsync(test, supressed); }
public async Task IgnoreGeneratedCode() { var code1 = @" #nullable enable$$ class Example { string? value; } "; var generatedCode1 = @"// <auto-generated/> #nullable enable class Example2 { string? value; } "; var generatedCode2 = @"// <auto-generated/> #nullable disable class Example3 { string value; } "; var generatedCode3 = @"// <auto-generated/> #nullable restore class Example4 { string {|#0:value|}; } "; var fixedCode1 = @" class Example { string? value; } "; await new VerifyCS.Test { TestState = { Sources = { code1, generatedCode1, generatedCode2, generatedCode3, }, }, FixedState = { Sources = { fixedCode1, generatedCode1, generatedCode2, generatedCode3, }, ExpectedDiagnostics = { // /0/Test3.cs(7,10): error CS8618: Non-nullable field 'value' must contain a non-null value when exiting constructor. Consider declaring the field as nullable. DiagnosticResult.CompilerError("CS8618").WithLocation(0), }, }, SolutionTransforms = { s_enableNullableInFixedSolution }, }.RunAsync(); }
public async Task RewriteIfBranch() { var test = @" using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Diagnostics; namespace ConsoleApplication1 { public class Test { public static int Main() // CS0161 { int i = 5; if (i < 10) { // return i; } else { // Uncomment the following line to resolve. return 1; } } } }"; var fixtest = @" using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Diagnostics; namespace ConsoleApplication1 { public class Test { public static int Main() // CS0161 { int i = 5; if (i < 10) { throw new NotImplementedException(); // return i; } else { // Uncomment the following line to resolve. return 1; } } } }"; var diagnostics = new DiagnosticResult[] { // Test0.cs(13,27): error CS0161: 'Test.Main()': not all code paths return a value DiagnosticResult.CompilerError("CS0161").WithSpan(13, 27, 13, 31).WithArguments("ConsoleApplication1.Test.Main()") }; await Verifier.VerifyCodeFixAsync(test, diagnostics, fixtest); }
public async Task CSharp_NoDiagnosticCases_NestedOperationAnalyzerRegistration() { var source = @" using System; using System.Collections.Immutable; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Diagnostics; [DiagnosticAnalyzer(LanguageNames.CSharp)] class MyAnalyzer : DiagnosticAnalyzer { public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get { throw new NotImplementedException(); } } public override void Initialize(AnalysisContext context) { context.RegisterCompilationStartAction(compilationContext => { compilationContext.RegisterOperationBlockStartAction(operationBlockContext => { AnalyzeOperationBlockStart(operationBlockContext); }); }); context.RegisterCompilationStartAction(compilationContext => { compilationContext.RegisterOperationAction(AnalyzeOperation, OperationKind.Invocation); }); context.RegisterCompilationStartAction(compilationContext => { compilationContext.RegisterOperationBlockAction(AnalyzeOperationBlock); }); } private static void AnalyzeOperation(OperationAnalysisContext context) { } private static void AnalyzeOperationBlock(OperationBlockAnalysisContext context) { } private static void AnalyzeOperationBlockStart(OperationBlockStartAnalysisContext context) { context.RegisterOperationAction(AnalyzeOperation, OperationKind.Invocation); } }"; await new VerifyCS.Test { ReferenceAssemblies = ReferenceAssemblies.Default, TestState = { Sources = { source }, ExpectedDiagnostics = { // Test0.cs(3,26): error CS0234: The type or namespace name 'Immutable' does not exist in the namespace 'System.Collections' (are you missing an assembly reference?) DiagnosticResult.CompilerError("CS0234").WithSpan(3, 26, 3, 35).WithArguments("Immutable", "System.Collections"), // Test0.cs(4,17): error CS0234: The type or namespace name 'CodeAnalysis' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) DiagnosticResult.CompilerError("CS0234").WithSpan(4, 17, 4, 29).WithArguments("CodeAnalysis", "Microsoft"), // Test0.cs(5,17): error CS0234: The type or namespace name 'CodeAnalysis' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) DiagnosticResult.CompilerError("CS0234").WithSpan(5, 17, 5, 29).WithArguments("CodeAnalysis", "Microsoft"), // Test0.cs(7,2): error CS0246: The type or namespace name 'DiagnosticAnalyzer' could not be found (are you missing a using directive or an assembly reference?) DiagnosticResult.CompilerError("CS0246").WithSpan(7, 2, 7, 20).WithArguments("DiagnosticAnalyzer"), // Test0.cs(7,2): error CS0246: The type or namespace name 'DiagnosticAnalyzerAttribute' could not be found (are you missing a using directive or an assembly reference?) DiagnosticResult.CompilerError("CS0246").WithSpan(7, 2, 7, 20).WithArguments("DiagnosticAnalyzerAttribute"), // Test0.cs(7,21): error CS0103: The name 'LanguageNames' does not exist in the current context DiagnosticResult.CompilerError("CS0103").WithSpan(7, 21, 7, 34).WithArguments("LanguageNames"), // Test0.cs(8,20): error CS0246: The type or namespace name 'DiagnosticAnalyzer' could not be found (are you missing a using directive or an assembly reference?) DiagnosticResult.CompilerError("CS0246").WithSpan(8, 20, 8, 38).WithArguments("DiagnosticAnalyzer"), // Test0.cs(10,21): error CS0246: The type or namespace name 'ImmutableArray<>' could not be found (are you missing a using directive or an assembly reference?) DiagnosticResult.CompilerError("CS0246").WithSpan(10, 21, 10, 57).WithArguments("ImmutableArray<>"), // Test0.cs(10,36): error CS0246: The type or namespace name 'DiagnosticDescriptor' could not be found (are you missing a using directive or an assembly reference?) DiagnosticResult.CompilerError("CS0246").WithSpan(10, 36, 10, 56).WithArguments("DiagnosticDescriptor"), // Test0.cs(10,58): error CS0115: 'MyAnalyzer.SupportedDiagnostics': no suitable method found to override DiagnosticResult.CompilerError("CS0115").WithSpan(10, 58, 10, 78).WithArguments("MyAnalyzer.SupportedDiagnostics"), // Test0.cs(18,37): error CS0246: The type or namespace name 'AnalysisContext' could not be found (are you missing a using directive or an assembly reference?) DiagnosticResult.CompilerError("CS0246").WithSpan(18, 37, 18, 52).WithArguments("AnalysisContext"), // Test0.cs(39,42): error CS0246: The type or namespace name 'OperationAnalysisContext' could not be found (are you missing a using directive or an assembly reference?) DiagnosticResult.CompilerError("CS0246").WithSpan(39, 42, 39, 66).WithArguments("OperationAnalysisContext"), // Test0.cs(43,47): error CS0246: The type or namespace name 'OperationBlockAnalysisContext' could not be found (are you missing a using directive or an assembly reference?) DiagnosticResult.CompilerError("CS0246").WithSpan(43, 47, 43, 76).WithArguments("OperationBlockAnalysisContext"), // Test0.cs(47,52): error CS0246: The type or namespace name 'OperationBlockStartAnalysisContext' could not be found (are you missing a using directive or an assembly reference?) DiagnosticResult.CompilerError("CS0246").WithSpan(47, 52, 47, 86).WithArguments("OperationBlockStartAnalysisContext"), // Test0.cs(49,59): error CS0103: The name 'OperationKind' does not exist in the current context DiagnosticResult.CompilerError("CS0103").WithSpan(49, 59, 49, 72).WithArguments("OperationKind"), }, }, }.RunAsync(); }
public async Task FixMethod() { var test = @" using System; using System.IO; using System.Linq; using System.Threading.Tasks; public class TestClass { private string _workspaceFile; public async Task<Configuration[]> LoadStorageContentAsync() { var fileInfo = new FileInfo(_workspaceFile); if (!fileInfo.Exists) { Log.Error($""No configuration storage located at {fileInfo.FullName}.""); return Task.FromResult(Array.Empty<Configuration>()); } using (var stream = new StreamReader(new FileStream(fileInfo.FullName, FileMode.Open))) { try { var storage = new Storage(); return Task.FromResult(storage.Configurations.ToArray()); } catch (Exception e) { Log.Error(e); return Task.FromResult(Array.Empty<Configuration>()); } } } public class Log { public static void Error(Exception p0) { throw new NotImplementedException(); } public static void Error(string p0) { throw new NotImplementedException(); } } public class Storage { public Configuration[] Configurations { get; set; } } public class Configuration { public Guid Id { get; set; } public string ConfigurationName { get; set; } } }"; var fixtest = @" using System; using System.IO; using System.Linq; using System.Threading.Tasks; public class TestClass { private string _workspaceFile; public async Task<Configuration[]> LoadStorageContentAsync() { var fileInfo = new FileInfo(_workspaceFile); if (!fileInfo.Exists) { Log.Error($""No configuration storage located at {fileInfo.FullName}.""); return Array.Empty<Configuration>(); } using (var stream = new StreamReader(new FileStream(fileInfo.FullName, FileMode.Open))) { try { var storage = new Storage(); return storage.Configurations.ToArray(); } catch (Exception e) { Log.Error(e); return Array.Empty<Configuration>(); } } } public class Log { public static void Error(Exception p0) { throw new NotImplementedException(); } public static void Error(string p0) { throw new NotImplementedException(); } } public class Storage { public Configuration[] Configurations { get; set; } } public class Configuration { public Guid Id { get; set; } public string ConfigurationName { get; set; } } }"; var diagnostics = new[] { // Test0.cs(17,11): error CS4016: Since this is an async method, the return expression must be of type 'TestClass.Configuration[]' rather than 'Task<TestClass.Configuration[]>' DiagnosticResult.CompilerError("CS4016").WithSpan(17, 11, 17, 56) .WithArguments("TestClass.Configuration[]"), // Test0.cs(25,12): error CS4016: Since this is an async method, the return expression must be of type 'TestClass.Configuration[]' rather than 'Task<TestClass.Configuration[]>' DiagnosticResult.CompilerError("CS4016").WithSpan(25, 12, 25, 61) .WithArguments("TestClass.Configuration[]"), // Test0.cs(30,12): error CS4016: Since this is an async method, the return expression must be of type 'TestClass.Configuration[]' rather than 'Task<TestClass.Configuration[]>' DiagnosticResult.CompilerError("CS4016").WithSpan(30, 12, 30, 57) .WithArguments("TestClass.Configuration[]") }; await Verifier.VerifyCodeFixAsync(test, diagnostics, fixtest); }
public async Task CSharpAwaitAwaitTask() { var code = @" using System.Threading.Tasks; public class C { public async Task M() { Task<Task> t = null; await [|await [|t|]|]; // both have warnings. await [|await t.ConfigureAwait(false)|]; // outer await is wrong. await (await [|t|]).ConfigureAwait(false); // inner await is wrong. } } "; var fixedCode = @" using System.Threading.Tasks; public class C { public async Task M() { Task<Task> t = null; await (await t.ConfigureAwait(false)).ConfigureAwait(false); // both have warnings. await (await t.ConfigureAwait(false)).ConfigureAwait(false); // outer await is wrong. await (await t.ConfigureAwait(false)).ConfigureAwait(false); // inner await is wrong. } } "; // 🐛 the Fix All should not be producing this invalid code var fixAllCode = @" using System.Threading.Tasks; public class C { public async Task M() { Task<Task> t = null; await {|#1:(await t.ConfigureAwait(false)).ConfigureAwait(false)|}.{|#0:ConfigureAwait|}(false); // both have warnings. await (await t.ConfigureAwait(false)).ConfigureAwait(false); // outer await is wrong. await (await t.ConfigureAwait(false)).ConfigureAwait(false); // inner await is wrong. } } "; await new VerifyCS.Test { TestState = { Sources = { code } }, FixedState = { Sources = { fixedCode } }, BatchFixedState = { Sources = { fixAllCode }, ExpectedDiagnostics = { #if !NETCOREAPP // /0/Test0.cs(9,69): error CS1061: 'ConfiguredTaskAwaitable' does not contain a definition for 'ConfigureAwait' and no accessible extension method 'ConfigureAwait' accepting a first argument of type 'ConfiguredTaskAwaitable' could be found (are you missing a using directive or an assembly reference?) DiagnosticResult.CompilerError("CS1061").WithLocation(0).WithArguments("System.Runtime.CompilerServices.ConfiguredTaskAwaitable", "ConfigureAwait"), #else // /0/Test0.cs(9,15): error CS1929: 'ConfiguredTaskAwaitable' does not contain a definition for 'ConfigureAwait' and the best extension method overload 'TaskAsyncEnumerableExtensions.ConfigureAwait(IAsyncDisposable, bool)' requires a receiver of type 'IAsyncDisposable' DiagnosticResult.CompilerError("CS1929").WithLocation(1).WithArguments("System.Runtime.CompilerServices.ConfiguredTaskAwaitable", "ConfigureAwait", "System.Threading.Tasks.TaskAsyncEnumerableExtensions.ConfigureAwait(System.IAsyncDisposable, bool)", "System.IAsyncDisposable"), #endif }, }, NumberOfFixAllIterations = 2, }.RunAsync(); }
public async Task CSharpAwaitAwaitTask() { var code = @" using System.Threading.Tasks; public class C { public async Task M() { Task<Task> t = null; await [|await [|t|]|]; // both have warnings. await [|await t.ConfigureAwait(false)|]; // outer await is wrong. await (await [|t|]).ConfigureAwait(false); // inner await is wrong. } } "; var fixedCode = @" using System.Threading.Tasks; public class C { public async Task M() { Task<Task> t = null; await (await t.ConfigureAwait(false)).ConfigureAwait(false); // both have warnings. await (await t.ConfigureAwait(false)).ConfigureAwait(false); // outer await is wrong. await (await t.ConfigureAwait(false)).ConfigureAwait(false); // inner await is wrong. } } "; var fixAllCode = @" using System.Threading.Tasks; public class C { public async Task M() { Task<Task> t = null; await (await t.ConfigureAwait(false)).ConfigureAwait(false).ConfigureAwait(false); // both have warnings. await (await t.ConfigureAwait(false)).ConfigureAwait(false); // outer await is wrong. await (await t.ConfigureAwait(false)).ConfigureAwait(false); // inner await is wrong. } } "; await new VerifyCS.Test { TestState = { Sources = { code } }, FixedState = { Sources = { fixedCode } }, BatchFixedState = { Sources = { fixAllCode }, ExpectedDiagnostics = { // 🐛 the Fix All should not be producing this invalid code DiagnosticResult.CompilerError("CS1061").WithSpan(9, 69, 9, 83).WithMessage("'ConfiguredTaskAwaitable' does not contain a definition for 'ConfigureAwait' and no accessible extension method 'ConfigureAwait' accepting a first argument of type 'ConfiguredTaskAwaitable' could be found (are you missing a using directive or an assembly reference?)"), }, }, NumberOfFixAllIterations = 2, }.RunAsync(); }