public void CSharp_VerifyDiagnostic() { var source = @" using System; using System.Collections.Generic; using System.Collections.Immutable; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.Semantics; using MyNamedType = Microsoft.CodeAnalysis.INamedTypeSymbol; abstract class MyCompilation : Compilation { // Compile error: no public constructor exists on Compilation. } [DiagnosticAnalyzer(LanguageNames.CSharp)] class MyAnalyzer : DiagnosticAnalyzer { private static readonly ITypeSymbol x1; public static readonly CSharpCompilation x2; private readonly List<MyNamedType> x3; private static Dictionary<MyCompilation, MyNamedType> x4; private static readonly IBinaryOperatorExpression x5; public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get { throw new NotImplementedException(); } } public override void Initialize(AnalysisContext context) { } }"; DiagnosticResult[] expected = new[] { GetCSharpExpectedDiagnostic(19, 29, violatingTypeName: typeof(ITypeSymbol).FullName), GetCSharpExpectedDiagnostic(20, 28, violatingTypeName: typeof(CSharpCompilation).FullName), GetCSharpExpectedDiagnostic(21, 27, violatingTypeName: typeof(INamedTypeSymbol).FullName), GetCSharpExpectedDiagnostic(22, 31, violatingTypeName: "MyCompilation"), GetCSharpExpectedDiagnostic(23, 29, violatingTypeName: typeof(IBinaryOperatorExpression).FullName) }; VerifyCSharp(source, TestValidationMode.AllowCompileErrors, expected); }
public void VisualBasic_VerifyRegisterSymbolActionDiagnostic() { var source = @" Imports System Imports System.Collections.Immutable Imports Microsoft.CodeAnalysis Imports Microsoft.CodeAnalysis.Diagnostics <DiagnosticAnalyzer(LanguageNames.CSharp)> 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.RegisterSymbolAction(AddressOf AnalyzeSymbol, SymbolKind.Alias, SymbolKind.ArrayType, SymbolKind.Assembly, SymbolKind.DynamicType, SymbolKind.ErrorType, SymbolKind.Label, SymbolKind.Local, SymbolKind.NetModule, SymbolKind.Parameter, SymbolKind.PointerType, SymbolKind.Preprocessing, SymbolKind.RangeVariable, SymbolKind.TypeParameter) End Sub Private Shared Sub AnalyzeSymbol(context As SymbolAnalysisContext) End Sub Private Shared Sub AnalyzeSyntax(context As SyntaxNodeAnalysisContext) End Sub End Class "; DiagnosticResult[] expected = new[] { GetBasicExpectedDiagnostic(18, 13, unsupportedSymbolKind: SymbolKind.Alias), GetBasicExpectedDiagnostic(19, 13, unsupportedSymbolKind: SymbolKind.ArrayType), GetBasicExpectedDiagnostic(20, 13, unsupportedSymbolKind: SymbolKind.Assembly), GetBasicExpectedDiagnostic(21, 13, unsupportedSymbolKind: SymbolKind.DynamicType), GetBasicExpectedDiagnostic(22, 13, unsupportedSymbolKind: SymbolKind.ErrorType), GetBasicExpectedDiagnostic(23, 13, unsupportedSymbolKind: SymbolKind.Label), GetBasicExpectedDiagnostic(24, 13, unsupportedSymbolKind: SymbolKind.Local), GetBasicExpectedDiagnostic(25, 13, unsupportedSymbolKind: SymbolKind.NetModule), GetBasicExpectedDiagnostic(26, 13, unsupportedSymbolKind: SymbolKind.Parameter), GetBasicExpectedDiagnostic(27, 13, unsupportedSymbolKind: SymbolKind.PointerType), GetBasicExpectedDiagnostic(28, 13, unsupportedSymbolKind: SymbolKind.Preprocessing), GetBasicExpectedDiagnostic(29, 13, unsupportedSymbolKind: SymbolKind.RangeVariable), GetBasicExpectedDiagnostic(30, 13, unsupportedSymbolKind: SymbolKind.TypeParameter), }; VerifyBasic(source, expected); }
public void CSharp_VerifyDiagnostic() { 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.RegisterSymbolAction(AnalyzeSymbol, SymbolKind.Alias, SymbolKind.ArrayType, SymbolKind.Assembly, SymbolKind.DynamicType, SymbolKind.ErrorType, SymbolKind.Label, SymbolKind.Local, SymbolKind.NetModule, SymbolKind.Parameter, SymbolKind.PointerType, SymbolKind.Preprocessing, SymbolKind.RangeVariable, SymbolKind.TypeParameter); } private static void AnalyzeSymbol(SymbolAnalysisContext context) { } private static void AnalyzeSyntax(SyntaxNodeAnalysisContext context) { } }"; DiagnosticResult[] expected = new[] { GetCSharpExpectedDiagnostic(21, 13, unsupportedSymbolKind: SymbolKind.Alias), GetCSharpExpectedDiagnostic(22, 13, unsupportedSymbolKind: SymbolKind.ArrayType), GetCSharpExpectedDiagnostic(23, 13, unsupportedSymbolKind: SymbolKind.Assembly), GetCSharpExpectedDiagnostic(24, 13, unsupportedSymbolKind: SymbolKind.DynamicType), GetCSharpExpectedDiagnostic(25, 13, unsupportedSymbolKind: SymbolKind.ErrorType), GetCSharpExpectedDiagnostic(26, 13, unsupportedSymbolKind: SymbolKind.Label), GetCSharpExpectedDiagnostic(27, 13, unsupportedSymbolKind: SymbolKind.Local), GetCSharpExpectedDiagnostic(28, 13, unsupportedSymbolKind: SymbolKind.NetModule), GetCSharpExpectedDiagnostic(29, 13, unsupportedSymbolKind: SymbolKind.Parameter), GetCSharpExpectedDiagnostic(30, 13, unsupportedSymbolKind: SymbolKind.PointerType), GetCSharpExpectedDiagnostic(31, 13, unsupportedSymbolKind: SymbolKind.Preprocessing), GetCSharpExpectedDiagnostic(32, 13, unsupportedSymbolKind: SymbolKind.RangeVariable), GetCSharpExpectedDiagnostic(33, 13, unsupportedSymbolKind: SymbolKind.TypeParameter), }; VerifyCSharp(source, expected); }
public void VisualBasic_VerifyDiagnostic() { var source = @" Imports System Imports System.Collections.Generic Imports System.Collections.Immutable Imports Microsoft.CodeAnalysis Imports Microsoft.CodeAnalysis.Diagnostics Imports Microsoft.CodeAnalysis.Semantics Imports Microsoft.CodeAnalysis.VisualBasic Imports MyNamedType = Microsoft.CodeAnalysis.INamedTypeSymbol MustInherit Class MyCompilation Inherits Compilation ' Compile error: no public constructor exists on Compilation. End Class <DiagnosticAnalyzer(LanguageNames.VisualBasic)> Class MyAnalyzer Inherits DiagnosticAnalyzer Private Shared ReadOnly x1 As ITypeSymbol Public Shared ReadOnly x2 As VisualBasicCompilation Private ReadOnly x3 As List(Of MyNamedType) Private Shared x4 As Dictionary(Of MyCompilation, MyNamedType) Private Shared ReadOnly x5 As IBinaryOperatorExpression Public Overrides ReadOnly Property SupportedDiagnostics() As ImmutableArray(Of DiagnosticDescriptor) Get Throw New NotImplementedException End Get End Property Public Overrides Sub Initialize(context As AnalysisContext) End Sub End Class "; DiagnosticResult[] expected = new[] { GetBasicExpectedDiagnostic(19, 35, violatingTypeName: typeof(ITypeSymbol).FullName), GetBasicExpectedDiagnostic(20, 34, violatingTypeName: typeof(VisualBasicCompilation).FullName), GetBasicExpectedDiagnostic(21, 36, violatingTypeName: typeof(INamedTypeSymbol).FullName), GetBasicExpectedDiagnostic(22, 40, violatingTypeName: "MyCompilation"), GetBasicExpectedDiagnostic(23, 35, violatingTypeName: typeof(IBinaryOperatorExpression).FullName) }; VerifyBasic(source, TestValidationMode.AllowCompileErrors, expected); }
public void CSharp_CodeActionCreate_VerifyDiagnostics() { var source = @" using System; using System.Collections.Immutable; using System.Threading.Tasks; using Microsoft.CodeAnalysis.CodeFixes; using Microsoft.CodeAnalysis.CodeActions; class C1 : CodeFixProvider { public override ImmutableArray<string> FixableDiagnosticIds { get { throw new NotImplementedException(); } } public override Task RegisterCodeFixesAsync(CodeFixContext context) { // Regular cases. var codeAction1_1 = CodeAction.Create(""Title1_1"", _ => Task.FromResult(context.Document)); var codeAction1_2 = CodeAction.Create(""Title1_2"", createChangedDocument: _ => Task.FromResult(context.Document)); var codeAction1_3 = CodeAction.Create(createChangedDocument: _ => Task.FromResult(context.Document), title: ""Title1_3""); // Null argument for equivalenceKey. var codeAction2_1 = CodeAction.Create(""Title2_1"", _ => Task.FromResult(context.Document), null); var codeAction2_2 = CodeAction.Create(createChangedDocument: _ => Task.FromResult(context.Document), equivalenceKey: null, title: ""Title2_2""); var codeAction2_3 = CodeAction.Create(""Title2_3"", _ => Task.FromResult(context.Document), equivalenceKey: null); return null; } "; var expected = new[] { // Test0.cs(21,29): warning RS1010: Provide an explicit argument for optional parameter 'equivalenceKey', which is non-null and unique across all code actions created by this fixer. GetCSharpCodeActionCreateExpectedDiagnostic(21, 29), // Test0.cs(22,29): warning RS1010: Provide an explicit argument for optional parameter 'equivalenceKey', which is non-null and unique across all code actions created by this fixer. GetCSharpCodeActionCreateExpectedDiagnostic(22, 29), // Test0.cs(23,29): warning RS1010: Provide an explicit argument for optional parameter 'equivalenceKey', which is non-null and unique across all code actions created by this fixer. GetCSharpCodeActionCreateExpectedDiagnostic(23, 29), // Test0.cs(26,29): warning RS1010: Provide an explicit argument for optional parameter 'equivalenceKey', which is non-null and unique across all code actions created by this fixer. GetCSharpCodeActionCreateExpectedDiagnostic(26, 29), // Test0.cs(27,29): warning RS1010: Provide an explicit argument for optional parameter 'equivalenceKey', which is non-null and unique across all code actions created by this fixer. GetCSharpCodeActionCreateExpectedDiagnostic(27, 29), // Test0.cs(28,29): warning RS1010: Provide an explicit argument for optional parameter 'equivalenceKey', which is non-null and unique across all code actions created by this fixer. GetCSharpCodeActionCreateExpectedDiagnostic(28, 29) }; TestCSharpCore(source, expected: expected); }
public void VisualBasic_CustomCodeAction_VerifyDiagnostics() { var source = @" Imports System Imports System.Collections.Immutable Imports System.Threading.Tasks Imports Microsoft.CodeAnalysis.CodeFixes Imports Microsoft.CodeAnalysis.CodeActions Class C1 Inherits CodeFixProvider Public Overrides ReadOnly Property FixableDiagnosticIds() As ImmutableArray(Of String) Get Throw New NotImplementedException() End Get End Property Public Overrides Function RegisterCodeFixesAsync(context As CodeFixContext) As Task Dim codeAction = New MyCodeActionNoEquivalenceKey() Return Nothing End Function "; var expected = new[] { // Test0.vb(17,20): warning RS1011: 'MyCodeActionNoEquivalenceKey' has the default value of 'null' for property 'EquivalenceKey'. Either override this property on 'MyCodeActionNoEquivalenceKey' to return a non-null and unique value across all code actions per-fixer or use such an existing code action. GetBasicCustomCodeActionExpectedDiagnostic(17, 20, "MyCodeActionNoEquivalenceKey") }; TestBasicCore(source, withCustomCodeActions: true, expected: expected); }
public void VisualBasic_CodeActionCreate_VerifyDiagnostics() { var source = @" Imports System Imports System.Collections.Immutable Imports System.Threading.Tasks Imports Microsoft.CodeAnalysis.CodeFixes Imports Microsoft.CodeAnalysis.CodeActions Class C1 Inherits CodeFixProvider Public Overrides ReadOnly Property FixableDiagnosticIds() As ImmutableArray(Of String) Get Throw New NotImplementedException() End Get End Property Public Overrides Function RegisterCodeFixesAsync(context As CodeFixContext) As Task ' Regular cases. Dim codeAction1_1 = CodeAction.Create(""Title1_1"", Function(x) Task.FromResult(context.Document)) Dim codeAction1_2 = CodeAction.Create(""Title1_2"", createChangedDocument := Function(x) Task.FromResult(context.Document)) Dim codeAction1_3 = CodeAction.Create(createChangedDocument := Function(x) Task.FromResult(context.Document), title := ""Title1_3"") ' Null argument for equivalenceKey. Dim codeAction2_1 = CodeAction.Create(""Title2_1"", Function(x) Task.FromResult(context.Document), Nothing) Dim codeAction2_2 = CodeAction.Create(createChangedDocument := Function(x) Task.FromResult(context.Document), equivalenceKey := Nothing, title := ""Title2_2"") Dim codeAction2_3 = CodeAction.Create(""Title2_3"", Function(x) Task.FromResult(context.Document), equivalenceKey := Nothing) Return Nothing End Function "; var expected = new[] { // Test0.vb(18,23): warning RS1010: Provide an explicit argument for optional parameter 'equivalenceKey', which is non-null and unique across all code actions created by this fixer. GetBasicCodeActionCreateExpectedDiagnostic(18, 23), // Test0.vb(19,23): warning RS1010: Provide an explicit argument for optional parameter 'equivalenceKey', which is non-null and unique across all code actions created by this fixer. GetBasicCodeActionCreateExpectedDiagnostic(19, 23), // Test0.vb(20,23): warning RS1010: Provide an explicit argument for optional parameter 'equivalenceKey', which is non-null and unique across all code actions created by this fixer. GetBasicCodeActionCreateExpectedDiagnostic(20, 23), // Test0.vb(23,23): warning RS1010: Provide an explicit argument for optional parameter 'equivalenceKey', which is non-null and unique across all code actions created by this fixer. GetBasicCodeActionCreateExpectedDiagnostic(23, 23), // Test0.vb(24,23): warning RS1010: Provide an explicit argument for optional parameter 'equivalenceKey', which is non-null and unique across all code actions created by this fixer. GetBasicCodeActionCreateExpectedDiagnostic(24, 23), // Test0.vb(25,23): warning RS1010: Provide an explicit argument for optional parameter 'equivalenceKey', which is non-null and unique across all code actions created by this fixer. GetBasicCodeActionCreateExpectedDiagnostic(25, 23) }; TestBasicCore(source, expected: expected); }
public void CSharp_CustomCodeAction_VerifyDiagnostics() { var source = @" using System; using System.Collections.Immutable; using System.Threading.Tasks; using Microsoft.CodeAnalysis.CodeFixes; using Microsoft.CodeAnalysis.CodeActions; class C1 : CodeFixProvider { public override ImmutableArray<string> FixableDiagnosticIds { get { throw new NotImplementedException(); } } public override Task RegisterCodeFixesAsync(CodeFixContext context) { var codeAction = new MyCodeActionNoEquivalenceKey(); return null; } "; var expected = new[] { // Test0.cs(20,26): warning RS1011: 'MyCodeActionNoEquivalenceKey' has the default value of 'null' for property 'EquivalenceKey'. Either override this property on 'MyCodeActionNoEquivalenceKey' to return a non-null and unique value across all code actions per-fixer or use such an existing code action. GetCSharpCustomCodeActionExpectedDiagnostic(20, 26, "MyCodeActionNoEquivalenceKey") }; TestCSharpCore(source, withCustomCodeActions: true, expected: expected); }
public void VisualBasic_VerifyDiagnostic() { var source = @" Imports System Imports System.Collections.Immutable Imports Microsoft.CodeAnalysis Imports Microsoft.CodeAnalysis.Diagnostics <DiagnosticAnalyzer(LanguageNames.CSharp)> Class MyAnalyzer Inherits DiagnosticAnalyzer Private Shared ReadOnly descriptor1 As DiagnosticDescriptor = New DiagnosticDescriptor(""MyDiagnosticId1"", Nothing, Nothing, Nothing, DiagnosticSeverity.Warning, False) Private Shared ReadOnly descriptor2 As DiagnosticDescriptor = New DiagnosticDescriptor(""MyDiagnosticId2"", Nothing, Nothing, Nothing, DiagnosticSeverity.Warning, False) Public Overrides ReadOnly Property SupportedDiagnostics() As ImmutableArray(Of DiagnosticDescriptor) Get Return ImmutableArray.Create(descriptor1) End Get End Property Public Overrides Sub Initialize(context As AnalysisContext) End Sub Private Shared Sub AnalyzeSymbol(context As SymbolAnalysisContext) context.ReportDiagnostic(Diagnostic.Create(descriptor2, Location.None)) Dim diag = Diagnostic.Create(descriptor2, Location.None) context.ReportDiagnostic(diag) End Sub Private Shared Sub AnalyzeSyntax(context As SyntaxNodeAnalysisContext) context.ReportDiagnostic(Diagnostic.Create(descriptor2, Location.None)) Dim diag = Diagnostic.Create(descriptor2, Location.None), diag2 = Diagnostic.Create(descriptor2, Location.None) context.ReportDiagnostic(diag) End Sub Private Shared Sub AnalyzeOperation(context As OperationAnalysisContext) context.ReportDiagnostic(Diagnostic.Create(descriptor2, Location.None)) Dim diag = Diagnostic.Create(descriptor2, Location.None) context.ReportDiagnostic(diag) End Sub End Class "; DiagnosticResult[] expected = new[] { GetBasicExpectedDiagnostic(24, 9, unsupportedDescriptorName: "descriptor2"), GetBasicExpectedDiagnostic(27, 9, unsupportedDescriptorName: "descriptor2"), GetBasicExpectedDiagnostic(31, 9, unsupportedDescriptorName: "descriptor2"), GetBasicExpectedDiagnostic(34, 9, unsupportedDescriptorName: "descriptor2"), GetBasicExpectedDiagnostic(38, 9, unsupportedDescriptorName: "descriptor2"), GetBasicExpectedDiagnostic(41, 9, unsupportedDescriptorName: "descriptor2") }; VerifyBasic(source, expected); }
public void CSharp_VerifyDiagnostic() { var source = @" using System; using System.Collections.Immutable; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Diagnostics; [DiagnosticAnalyzer(LanguageNames.CSharp)] class MyAnalyzer : DiagnosticAnalyzer { private static readonly DiagnosticDescriptor descriptor1 = new DiagnosticDescriptor(""MyDiagnosticId1"", null, null, null, DiagnosticSeverity.Warning, false); private static readonly DiagnosticDescriptor descriptor2 = new DiagnosticDescriptor(""MyDiagnosticId2"", null, null, null, DiagnosticSeverity.Warning, false); public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get { return ImmutableArray.Create(descriptor1); } } public override void Initialize(AnalysisContext context) { } private static void AnalyzeSymbol(SymbolAnalysisContext context) { context.ReportDiagnostic(Diagnostic.Create(descriptor2, Location.None)); var diag = Diagnostic.Create(descriptor2, Location.None); context.ReportDiagnostic(diag); } private static void AnalyzeSyntax(SyntaxNodeAnalysisContext context) { context.ReportDiagnostic(Diagnostic.Create(descriptor2, Location.None)); Diagnostic diag = Diagnostic.Create(descriptor2, Location.None), diag2 = Diagnostic.Create(descriptor2, Location.None); context.ReportDiagnostic(diag); } private static void AnalyzeOperation(OperationAnalysisContext context) { context.ReportDiagnostic(Diagnostic.Create(descriptor2, Location.None)); var diag = Diagnostic.Create(descriptor2, Location.None); context.ReportDiagnostic(diag); } }"; DiagnosticResult[] expected = new[] { GetCSharpExpectedDiagnostic(27, 9, unsupportedDescriptorName: "descriptor2"), GetCSharpExpectedDiagnostic(30, 9, unsupportedDescriptorName: "descriptor2"), GetCSharpExpectedDiagnostic(35, 9, unsupportedDescriptorName: "descriptor2"), GetCSharpExpectedDiagnostic(38, 9, unsupportedDescriptorName: "descriptor2"), GetCSharpExpectedDiagnostic(43, 9, unsupportedDescriptorName: "descriptor2"), GetCSharpExpectedDiagnostic(46, 9, unsupportedDescriptorName: "descriptor2") }; VerifyCSharp(source, expected); }