public static void GenericMethodsArgumentTypeParameterEquals() { var syntaxTree = CSharpSyntaxTree.ParseText( @" namespace N { public class C<T> { public int M1(T x) => 1; public int M2(T x) => 2; } }"); var compilation = CSharpCompilation.Create("test", new[] { syntaxTree }); var semanticModel = compilation.GetSemanticModel(syntaxTree); var node1 = syntaxTree.FindMethodDeclaration("M1"); var symbol1 = semanticModel.GetDeclaredSymbol(node1, CancellationToken.None).Parameters[0].Type; var node2 = syntaxTree.FindMethodDeclaration("M2"); var symbol2 = semanticModel.GetDeclaredSymbol(node2, CancellationToken.None).Parameters[0].Type; Assert.AreEqual(true, SymbolComparer.Equal((ISymbol)symbol1, (ISymbol)symbol1)); Assert.AreEqual(true, SymbolComparer.Equal((ISymbol)symbol1, (ISymbol)symbol2)); Assert.AreEqual(true, TypeSymbolComparer.Equal((ITypeSymbol)symbol1, (ITypeSymbol)symbol1)); Assert.AreEqual(true, TypeSymbolComparer.Equal((ITypeSymbol)symbol1, (ITypeSymbol)symbol2)); Assert.AreEqual(SymbolComparer.Default.GetHashCode(symbol1), TypeSymbolComparer.GetHashCode(symbol1)); Assert.AreEqual(SymbolComparer.Default.GetHashCode(symbol1), TypeSymbolComparer.GetHashCode(symbol2)); }
public static void Equals() { var syntaxTree = CSharpSyntaxTree.ParseText( @" namespace N { public class C1 { } public class C2 { } }"); var compilation = CSharpCompilation.Create("test", new[] { syntaxTree }); var semanticModel = compilation.GetSemanticModel(syntaxTree); var node1 = syntaxTree.FindTypeDeclaration("C1"); var symbol1 = semanticModel.GetDeclaredSymbol(node1, CancellationToken.None); var node2 = syntaxTree.FindTypeDeclaration("C2"); var symbol2 = semanticModel.GetDeclaredSymbol(node2, CancellationToken.None); Assert.AreEqual(true, SymbolComparer.Equal((ISymbol)symbol1, (ISymbol)symbol1)); Assert.AreEqual(false, SymbolComparer.Equal((ISymbol)symbol1, (ISymbol)symbol2)); Assert.AreEqual(true, TypeSymbolComparer.Equal((ITypeSymbol)symbol1, (ITypeSymbol)symbol1)); Assert.AreEqual(false, TypeSymbolComparer.Equal((ITypeSymbol)symbol1, (ITypeSymbol)symbol2)); Assert.AreEqual(SymbolComparer.Default.GetHashCode(symbol1), TypeSymbolComparer.GetHashCode(symbol1)); Assert.AreNotEqual(SymbolComparer.Default.GetHashCode(symbol1), TypeSymbolComparer.GetHashCode(symbol2)); }
public static void GenericPropertiesTypeParameterEquals() { var syntaxTree = CSharpSyntaxTree.ParseText( @" namespace N { public class C<T> { public T P1 { get; } public T P2 { get; } } }"); var compilation = CSharpCompilation.Create("test", new[] { syntaxTree }); var semanticModel = compilation.GetSemanticModel(syntaxTree); var node1 = syntaxTree.FindPropertyDeclaration("P1"); var symbol1 = semanticModel.GetDeclaredSymbol(node1, CancellationToken.None).Type; var node2 = syntaxTree.FindPropertyDeclaration("P2"); var symbol2 = semanticModel.GetDeclaredSymbol(node2, CancellationToken.None).Type; Assert.AreEqual(true, SymbolComparer.Equal((ISymbol)symbol1, (ISymbol)symbol1)); Assert.AreEqual(true, SymbolComparer.Equal((ISymbol)symbol1, (ISymbol)symbol2)); Assert.AreEqual(true, TypeSymbolComparer.Equal((ITypeSymbol)symbol1, (ITypeSymbol)symbol1)); Assert.AreEqual(true, TypeSymbolComparer.Equal((ITypeSymbol)symbol1, (ITypeSymbol)symbol2)); Assert.AreEqual(SymbolComparer.Default.GetHashCode(symbol1), TypeSymbolComparer.GetHashCode(symbol1)); Assert.AreEqual(SymbolComparer.Default.GetHashCode(symbol1), TypeSymbolComparer.GetHashCode(symbol2)); }
private static void Handle(SyntaxNodeAnalysisContext context) { if (!context.IsExcludedFromAnalysis() && context.ContainingSymbol is INamedTypeSymbol { IsAbstract : false, IsStatic : false } type&& type.IsAssignableToEither(KnownSymbols.IValueConverter, KnownSymbols.IMultiValueConverter, context.SemanticModel.Compilation) && context.Node is ClassDeclarationSyntax classDeclaration && type.DeclaredAccessibility != Accessibility.Private && type.DeclaredAccessibility != Accessibility.Protected) { if (!type.IsAssignableTo(KnownSymbols.MarkupExtension, context.SemanticModel.Compilation)) { if (ValueConverter.TryGetDefaultFieldsOrProperties(type, context.SemanticModel.Compilation, out var defaults)) { foreach (var fieldOrProperty in defaults) { if (fieldOrProperty.Value(context.CancellationToken) is { } assignedValue&& context.SemanticModel.TryGetType(assignedValue, context.CancellationToken, out var assignedType) && !TypeSymbolComparer.Equal(assignedType, type)) { context.ReportDiagnostic(Diagnostic.Create(Descriptors.WPF0074DefaultMemberOfWrongType, assignedValue.GetLocation())); } } } else if (!Virtual.HasVirtualOrAbstractOrProtectedMembers(type) && !type.Constructors.TryFirst(x => x.Parameters.Length > 0, out _) && !Mutable.HasMutableInstanceMembers(type) && !classDeclaration.Modifiers.Any(SyntaxKind.PartialKeyword)) { context.ReportDiagnostic(Diagnostic.Create(Descriptors.WPF0070ConverterDoesNotHaveDefaultField, classDeclaration.Identifier.GetLocation())); } } if (type.IsAssignableTo(KnownSymbols.IValueConverter, context.SemanticModel.Compilation)) { if (Attribute.TryFind(classDeclaration, KnownSymbols.ValueConversionAttribute, context.SemanticModel, context.CancellationToken, out var attribute)) { if (ValueConverter.TryGetConversionTypes(classDeclaration, context.SemanticModel, context.CancellationToken, out var sourceType, out var targetType)) { if (sourceType is { } &&
private static void Handle(SyntaxNodeAnalysisContext context) { if (context.Node is ObjectCreationExpressionSyntax { ArgumentList : { } argumentList } objectCreation&& context.ContainingSymbol is { } containingSymbol&& (objectCreation.Type == KnownSymbols.RoutedCommand || objectCreation.Type == KnownSymbols.RoutedUICommand) && context.SemanticModel.TryGetSymbol(objectCreation, context.CancellationToken, out var ctor)) { if (ctor.TryFindParameter("ownerType", out var parameter)) { if (objectCreation.TryFindArgument(parameter, out var ownerTypeArg) && ownerTypeArg.TryGetTypeofValue(context.SemanticModel, context.CancellationToken, out var type) && !TypeSymbolComparer.Equal(type, containingSymbol.ContainingType)) { context.ReportDiagnostic( Diagnostic.Create( Descriptors.WPF0121RegisterContainingTypeAsOwnerForRoutedCommand, ownerTypeArg.GetLocation(), context.ContainingSymbol.ContainingType.ToMinimalDisplayString(context.SemanticModel, objectCreation.SpanStart))); } } if (TryGetBackingMember(objectCreation, context, out var fieldOrProperty, out var memberDeclaration)) { if (ctor.TryFindParameter("name", out var nameParameter)) { if (objectCreation.TryFindArgument(nameParameter, out var nameArg) && nameArg.TryGetStringValue(context.SemanticModel, context.CancellationToken, out var registeredName)) { if (registeredName != fieldOrProperty.Name) { context.ReportDiagnostic( Diagnostic.Create( Descriptors.WPF0120RegisterContainingMemberAsNameForRoutedCommand, nameArg.GetLocation(), ImmutableDictionary <string, string?> .Empty.Add(nameof(IdentifierNameSyntax), fieldOrProperty.Name), fieldOrProperty.Name)); } else if (nameArg.Expression.IsKind(SyntaxKind.StringLiteralExpression)) { context.ReportDiagnostic( Diagnostic.Create( Descriptors.WPF0150UseNameofInsteadOfLiteral, nameArg.GetLocation(), ImmutableDictionary <string, string?> .Empty.Add(nameof(IdentifierNameSyntax), fieldOrProperty.Name), fieldOrProperty.Name)); } else if (!nameArg.Expression.IsNameof()) { context.ReportDiagnostic( Diagnostic.Create( Descriptors.WPF0151UseNameofInsteadOfConstant, nameArg.GetLocation(), ImmutableDictionary <string, string?> .Empty.Add(nameof(IdentifierNameSyntax), fieldOrProperty.Name), fieldOrProperty.Name)); } } } else { context.ReportDiagnostic( Diagnostic.Create( Descriptors.WPF0122RegisterRoutedCommand, argumentList.GetLocation(), ImmutableDictionary.CreateRange(new[] { new KeyValuePair <string, string?>(nameof(IdentifierNameSyntax), fieldOrProperty.Name), new KeyValuePair <string, string?>(nameof(TypeOfExpressionSyntax), context.ContainingSymbol.ContainingType.ToMinimalDisplayString(context.SemanticModel, objectCreation.SpanStart)), }))); } if (!fieldOrProperty.IsStaticReadOnly()) { context.ReportDiagnostic(Diagnostic.Create(Descriptors.WPF0123BackingMemberShouldBeStaticReadonly, BackingFieldOrProperty.FindIdentifier(memberDeclaration).GetLocation())); } } } }