private static void Handle(SyntaxNodeAnalysisContext context)
        {
            if (!context.IsExcludedFromAnalysis() &&
                context.Node is AssignmentExpressionSyntax assignment &&
                context.SemanticModel.TryGetSymbol(assignment.Left, context.CancellationToken, out ISymbol left))
            {
                if (context.SemanticModel.TryGetSymbol(assignment.Right, context.CancellationToken, out ISymbol right) &&
                    AreSame(assignment.Left, assignment.Right) &&
                    assignment.FirstAncestorOrSelf <InitializerExpressionSyntax>() == null &&
                    left.Equals(right))
                {
                    context.ReportDiagnostic(Diagnostic.Create(Descriptors.GU0010DoNotAssignSameValue, assignment.GetLocation()));
                }

                if (assignment.Right is IdentifierNameSyntax identifier &&
                    context.ContainingSymbol is IMethodSymbol method &&
                    method.DeclaredAccessibility.IsEither(Accessibility.Internal, Accessibility.Protected, Accessibility.Public) &&
                    method.Parameters.TryFirst(x => x.Name == identifier.Identifier.ValueText, out var parameter) &&
                    parameter.Type.IsReferenceType &&
                    !parameter.HasExplicitDefaultValue &&
                    !NullCheck.IsChecked(parameter, assignment.FirstAncestor <BaseMethodDeclarationSyntax>(), context.SemanticModel, context.CancellationToken))
                {
                    context.ReportDiagnostic(Diagnostic.Create(Descriptors.GU0012NullCheckParameter, assignment.Right.GetLocation()));
                }

                if (IsRedundantAssignment(left, assignment, context))
                {
                    context.ReportDiagnostic(Diagnostic.Create(Descriptors.GU0015DoNotAssignMoreThanOnce, assignment.GetLocation()));
                }
            }
        }
Example #2
0
 private static void Handle(SyntaxNodeAnalysisContext context)
 {
     if (!context.IsExcludedFromAnalysis() &&
         context.Node is ParameterSyntax parameterSyntax &&
         context.ContainingSymbol is IMethodSymbol method &&
         method.DeclaredAccessibility.IsEither(Accessibility.Internal, Accessibility.Protected, Accessibility.Public) &&
         method.TryFindParameter(parameterSyntax.Identifier.ValueText, out var parameter) &&
         parameter.Type.IsReferenceType &&
         parameter.RefKind != RefKind.Out &&
         !parameter.HasExplicitDefaultValue &&
         parameterSyntax.Parent is ParameterListSyntax parameterList &&
         parameterList.Parent is BaseMethodDeclarationSyntax methodDeclaration &&
         !NullCheck.IsChecked(parameter, methodDeclaration, context.SemanticModel, context.CancellationToken))
     {
         context.ReportDiagnostic(Diagnostic.Create(Descriptors.GU0012NullCheckParameter, parameterSyntax.Identifier.GetLocation()));
     }
 }
        private static void Handle(SyntaxNodeAnalysisContext context)
        {
            if (context.IsExcludedFromAnalysis())
            {
                return;
            }

            if (context.Node is ParameterSyntax parameterSyntax &&
                context.ContainingSymbol is IMethodSymbol method &&
                method.DeclaredAccessibility.IsEither(Accessibility.Internal, Accessibility.Protected, Accessibility.Public) &&
                method.Parameters.TryFirst(x => x.Name == parameterSyntax.Identifier.ValueText, out var parameter) &&
                parameter.Type.IsReferenceType &&
                !parameter.HasExplicitDefaultValue &&
                !NullCheck.IsChecked(parameter, parameterSyntax.FirstAncestor <BaseMethodDeclarationSyntax>(), context.SemanticModel, context.CancellationToken))
            {
                context.ReportDiagnostic(Diagnostic.Create(GU0012NullCheckParameter.Descriptor, parameterSyntax.Identifier.GetLocation()));
            }
        }