Exemplo n.º 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));
     }
 }
Exemplo n.º 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));
                }
            }
        }
Exemplo n.º 3
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);
            }