Пример #1
0
 private static void HandleAssignment(SyntaxNodeAnalysisContext context)
 {
     if (!context.IsExcludedFromAnalysis() &&
         context.Node is AssignmentExpressionSyntax {
         Left : { } left
     } assignment&&
         !IsInObjectInitializer(assignment) &&
         !IsInConstructor(assignment) &&
         context.SemanticModel.TryGetSymbol(left, context.CancellationToken, out IPropertySymbol? property) &&
         property != KnownSymbols.FrameworkElement.DataContext &&
         ClrProperty.TrySingleBackingField(property, context.SemanticModel, context.CancellationToken, out var backing) &&
         !IsAssignedCreatedInScope(left, context.SemanticModel, context.CancellationToken))
     {
         var propertyArgument = backing.CreateArgument(context.SemanticModel, context.Node.SpanStart).ToString();
         context.ReportDiagnostic(
             Diagnostic.Create(
                 Descriptors.WPF0041SetMutableUsingSetCurrentValue,
                 assignment.GetLocation(),
                 properties: ImmutableDictionary <string, string> .Empty.Add(
                     nameof(BackingFieldOrProperty),
                     propertyArgument),
                 propertyArgument,
                 assignment.Right));
     }
 }
Пример #2
0
        private static void HandleAssignment(SyntaxNodeAnalysisContext context)
        {
            if (context.IsExcludedFromAnalysis())
            {
                return;
            }

            if (context.Node is AssignmentExpressionSyntax assignment)
            {
                if (IsInObjectInitializer(assignment) ||
                    IsInConstructor(assignment))
                {
                    return;
                }

                var property = context.SemanticModel.GetSymbolSafe(assignment.Left, context.CancellationToken) as IPropertySymbol;
                if (property == KnownSymbol.FrameworkElement.DataContext)
                {
                    return;
                }

                if (ClrProperty.TrySingleBackingField(property, context.SemanticModel, context.CancellationToken, out var fieldOrProperty))
                {
                    if (IsCalleePotentiallyCreatedInScope(assignment.Left as MemberAccessExpressionSyntax, context.SemanticModel, context.CancellationToken))
                    {
                        return;
                    }

                    var propertyArg = fieldOrProperty.CreateArgument(context.SemanticModel, context.Node.SpanStart);
                    context.ReportDiagnostic(Diagnostic.Create(Descriptor, assignment.GetLocation(), propertyArg, assignment.Right));
                }
            }
        }
 private static void Handle(SyntaxNodeAnalysisContext context)
 {
     if (!context.IsExcludedFromAnalysis() &&
         context.ContainingSymbol is IPropertySymbol {
         IsStatic : false
     } property&&
         context.Node is PropertyDeclarationSyntax propertyDeclaration &&
         propertyDeclaration.Getter() is { } getter&&
         propertyDeclaration.Setter() is { } setter)
     {
         if (ClrProperty.Match(property, context.SemanticModel, context.CancellationToken) is { SetValue : { } setValue, BackingSet : { } backingSet, GetValue : { } getValue, BackingGet : { } backingGet })
Пример #4
0
 private static void HandleAssignment(SyntaxNodeAnalysisContext context)
 {
     if (!context.IsExcludedFromAnalysis() &&
         context.Node is AssignmentExpressionSyntax {
         Left : { } left
     } assignment&&
         !IsInObjectInitializer(assignment) &&
         !IsInConstructor(assignment) &&
         context.SemanticModel.TryGetSymbol(left, context.CancellationToken, out IPropertySymbol? property) &&
         !IsIgnored(property) &&
         ClrProperty.Match(property, context.SemanticModel, context.CancellationToken) is { BackingSet : { } backingSet } &&
Пример #5
0
            internal static bool TryCreateProperty(AssignmentExpressionSyntax assignment, SemanticModel semanticModel, CancellationToken cancellationToken, out ArgumentSyntax result)
            {
                result = null;
                var property = semanticModel.GetSymbolInfo(assignment.Left, cancellationToken).Symbol as IPropertySymbol;

                if (ClrProperty.TrySingleBackingField(property, semanticModel, cancellationToken, out var fieldSymbol))
                {
                    result = fieldSymbol.CreateArgument(semanticModel, assignment.SpanStart);
                }

                return(result != null);
            }
Пример #6
0
        private static void Handle(SyntaxNodeAnalysisContext context)
        {
            if (!context.IsExcludedFromAnalysis() &&
                !context.ContainingSymbol.IsStatic &&
                context.ContainingSymbol is IPropertySymbol property &&
                property.ContainingType.IsAssignableTo(KnownSymbol.DependencyObject, context.Compilation) &&
                context.Node is PropertyDeclarationSyntax propertyDeclaration &&
                PropertyDeclarationWalker.TryGetCalls(propertyDeclaration, out var getCall, out var setCall))
            {
                if (setCall != null &&
                    propertyDeclaration.TryGetSetter(out var setter) &&
                    setter.Body != null &&
                    setter.Body.Statements.TryFirst(x => !x.Contains(setCall), out var statement))
                {
                    context.ReportDiagnostic(Diagnostic.Create(WPF0036AvoidSideEffectsInClrAccessors.Descriptor, statement.GetLocation()));
                }

                if (getCall != null &&
                    propertyDeclaration.TryGetGetter(out var getter) &&
                    getter.Body != null &&
                    getter.Body.Statements.TryFirst(x => !x.Contains(getCall), out statement))
                {
                    context.ReportDiagnostic(Diagnostic.Create(WPF0036AvoidSideEffectsInClrAccessors.Descriptor, statement.GetLocation()));
                }

                if (getCall.TryGetArgumentAtIndex(0, out var getArg) &&
                    getArg.Expression is IdentifierNameSyntax getIdentifier &&
                    setCall.TryGetArgumentAtIndex(0, out var setArg) &&
                    setArg.Expression is IdentifierNameSyntax setIdentifier &&
                    getIdentifier.Identifier.ValueText != setIdentifier.Identifier.ValueText &&
                    !setIdentifier.Identifier.ValueText.IsParts(getIdentifier.Identifier.ValueText, "Key"))
                {
                    context.ReportDiagnostic(
                        Diagnostic.Create(
                            WPF0032ClrPropertyGetAndSetSameDependencyProperty.Descriptor,
                            propertyDeclaration.GetLocation(),
                            context.ContainingSymbol.Name));
                }

                if (setCall.TryGetMethodName(out var setCallName) &&
                    setCallName != "SetValue")
                {
                    //// ReSharper disable once PossibleNullReferenceException
                    context.ReportDiagnostic(
                        Diagnostic.Create(
                            WPF0035ClrPropertyUseSetValueInSetter.Descriptor,
                            setCall.GetLocation(),
                            context.ContainingSymbol.Name));
                }

                if (ClrProperty.TryGetRegisterField(propertyDeclaration, context.SemanticModel, context.CancellationToken, out var fieldOrProperty))
                {
                    if (DependencyProperty.TryGetRegisteredName(fieldOrProperty, context.SemanticModel, context.CancellationToken, out var registeredName) &&
                        registeredName != property.Name)
                    {
                        context.ReportDiagnostic(
                            Diagnostic.Create(
                                WPF0003ClrPropertyShouldMatchRegisteredName.Descriptor,
                                propertyDeclaration.Identifier.GetLocation(),
                                ImmutableDictionary <string, string> .Empty.Add("ExpectedName", registeredName),
                                property.Name,
                                registeredName));
                    }

                    if (DependencyProperty.TryGetRegisteredType(fieldOrProperty, context.SemanticModel, context.CancellationToken, out var registeredType) &&
                        !registeredType.Equals(property.Type))
                    {
                        context.ReportDiagnostic(
                            Diagnostic.Create(
                                WPF0012ClrPropertyShouldMatchRegisteredType.Descriptor,
                                propertyDeclaration.Type.GetLocation(),
                                ImmutableDictionary <string, string> .Empty.Add(nameof(TypeSyntax), registeredType.ToMinimalDisplayString(context.SemanticModel, context.Node.SpanStart)),
                                property,
                                registeredType));
                    }
                }
            }
        }