为啥此 Roslyn 分析仪测试失败?

Posted

技术标签:

【中文标题】为啥此 Roslyn 分析仪测试失败?【英文标题】:Why is this Roslyn analyzer test failing?为什么此 Roslyn 分析仪测试失败? 【发布时间】:2021-10-17 18:51:37 【问题描述】:

我试图理解为什么这个测试失败了:

Maak.Test.InitializeNewUnitTest.NoPropertiesInitialized_Diagnostic

Assert.AreEqual 失败。预期:。实际:。语境: 测试状态诊断 预期诊断将在“38”列结束 实际上在“39”列

预期诊断: // /0/Test0.cs(13,9,13,38): 警告 MaakInitializeNew VerifyCS.Diagnostic().WithSpan(13, 9, 13, 38),

实际诊断: // /0/Test0.cs(13,9): 警告 MaakInitializeNew: 可以完全初始化 VerifyCS.Diagnostic().WithSpan(13, 9, 13, 39),

测试:

[TestMethod]
        public async Task NoPropertiesInitialized_Diagnostic()
        
            await VerifyCS.VerifyCodeFixAsync(@"
using System;

public class MyClass

    public string Name  get; set; 


class Program

    static void Main()
    
        [|var myClass = new MyClass  |];
    

", @"
using System;

public class MyClass

    public string Name  get; set; 


class Program

    static void Main()
    
        var myClass = new MyClass  Name = ""Stan"" ;
    

");
        

分析器代码:

public override void Initialize(AnalysisContext context)
        
            // See https://github.com/dotnet/roslyn/blob/main/docs/analyzers/Analyzer%20Actions%20Semantics.md for more information
            context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
            context.EnableConcurrentExecution();
            context.RegisterSyntaxNodeAction(AnalyzeNode, SyntaxKind.LocalDeclarationStatement);
        

        private static void AnalyzeNode(SyntaxNodeAnalysisContext context)
        
            var localDeclaration = (LocalDeclarationStatementSyntax)context.Node;
            var typeDeclaration = localDeclaration.Declaration.Type;
            var symbolInfo = context.SemanticModel.GetSymbolInfo(typeDeclaration);
            var typeSymbol = symbolInfo.Symbol;
            
            if(typeSymbol == null)
                return;

            // Special case: Ensure that 'var' isn't actually an alias to another type. (e.g. using var = System.String).
            var aliasInfo = context.SemanticModel.GetAliasInfo(typeDeclaration);
            if (aliasInfo != null)
                return;

            var namedSymbol = context.Compilation.GetTypeByMetadataName(typeSymbol.MetadataName);

            if (namedSymbol?.TypeKind != TypeKind.Class)
                return;

            var hasDefaultConstructor = (namedSymbol?.Constructors)?.SingleOrDefault(c => !c.Parameters.Any()) != null;
            var properties = namedSymbol?.GetMembers()
                .Where(m => m.Kind == SymbolKind.Property
                            && m.DeclaredAccessibility == Accessibility.Public
                            && !((IPropertySymbol)m).IsReadOnly
                            && !((IPropertySymbol)m).IsStatic)
                .Select(m => new
                
                    Name = m.Name,
                    Type = ((IPropertySymbol)m).Type
                )
                .ToList();
            var hasValidProperties = properties?.Any() != false;

            if (!hasValidProperties)
                return;

            var initializerExpressions = (localDeclaration.Declaration.Variables.FirstOrDefault().Initializer.Value
                    as ObjectCreationExpressionSyntax)?.Initializer.Expressions.ToList();
            var except = properties.Select(p => p.Name)
                .Except(initializerExpressions.Select(e => (e as AssignmentExpressionSyntax)?.Left.ToString()))
                .ToList();
            
            if(except.Count == 0)
                return;

            context.ReportDiagnostic(Diagnostic.Create(Rule, context.Node.GetLocation()));
        

【问题讨论】:

【参考方案1】:

报告正确的位置修复了它:

context.ReportDiagnostic(Diagnostic.Create(Rule, localDeclaration.Declaration.GetLocation()));

【讨论】:

以上是关于为啥此 Roslyn 分析仪测试失败?的主要内容,如果未能解决你的问题,请参考以下文章

从 Roslyn 代码分析器调用分析程序集的方法

升级到 Roslyn 分析仪 6.0 版后获取 CA2007

Roslyn 分析器:类标记为部分

使用 Roslyn 代码分析在引用的程序集中查找符号

在 Roslyn 分析器/源生成器中,有啥方法可以区分直接和传递程序集引用

Roslyn代码分析从无错误的解决方案返回错误的构建错误