private void CheckController(SymbolAnalysisContext c)
        {
            if (!IsEnabled(c.Options))
            {
                return;
            }
            var attributes = c.Symbol.GetAttributes();

            if (attributes.IsEmpty)
            {
                return;
            }

            var attributeWithFalseParameter = attributes.FirstOrDefault(a =>
                                                                        a.ConstructorArguments.Length == 1 &&
                                                                        a.ConstructorArguments[0].Kind == TypedConstantKind.Primitive &&
                                                                        a.ConstructorArguments[0].Value is bool enableValidationValue &&
                                                                        !enableValidationValue &&
                                                                        a.AttributeClass.Is(KnownType.System_Web_Mvc_ValidateInputAttribute));

            if (attributeWithFalseParameter != null)
            {
                c.ReportDiagnosticWhenActive(Diagnostic.Create(rule, attributeWithFalseParameter.ApplicationSyntaxReference.GetSyntax().GetLocation()));
            }
        }
Esempio n. 2
0
        private static void AnalyzeNamedTypes(SymbolAnalysisContext context)
        {
            var namedType = (INamedTypeSymbol)context.Symbol;

            if (!namedType.IsClassOrStruct() || namedType.ContainingType != null)
            {
                return;
            }

            var removableDeclarationCollector = new CSharpRemovableDeclarationCollector(namedType, context.Compilation);

            var declaredPrivateMethodsWithReturn = CollectRemovableMethods(removableDeclarationCollector).ToList();

            if (!declaredPrivateMethodsWithReturn.Any())
            {
                return;
            }

            var invocations = removableDeclarationCollector.TypeDeclarations.SelectMany(x => FilterInvocations(x)).ToList();

            foreach (var declaredPrivateMethodWithReturn in declaredPrivateMethodsWithReturn)
            {
                var matchingInvocations = invocations
                                          .Where(invocation => invocation.Symbol.OriginalDefinition.Equals(declaredPrivateMethodWithReturn.Symbol))
                                          .ToList();

                // Method invocation is noncompliant when there is at least 1 invocation of the method, and no invocation is using the return value. The case of 0 invocation is handled by S1144.
                if (matchingInvocations.Any() && !matchingInvocations.Any(x => IsReturnValueUsed(x)))
                {
                    context.ReportDiagnosticWhenActive(Diagnostic.Create(Rule, declaredPrivateMethodWithReturn.SyntaxNode.ReturnType.GetLocation()));
                }
            }
        }
Esempio n. 3
0
 public static void ReportDiagnosticIfNonGenerated(this SymbolAnalysisContext context, GeneratedCodeRecognizer generatedCodeRecognizer, Diagnostic diagnostic)
 {
     if (ShouldAnalyze(generatedCodeRecognizer, diagnostic.Location.SourceTree, context.Compilation, context.Options))
     {
         context.ReportDiagnosticWhenActive(diagnostic);
     }
 }
Esempio n. 4
0
 public static void ReportDiagnosticIfNonGenerated(
     this SymbolAnalysisContext context,
     GeneratedCodeRecognizer generatedCodeRecognizer,
     Diagnostic diagnostic,
     Compilation compilation)
 {
     if (!diagnostic.Location.SourceTree.IsGenerated(generatedCodeRecognizer, compilation))
     {
         context.ReportDiagnosticWhenActive(diagnostic);
     }
 }
        private static void ReportPublicExternalMethods(SymbolAnalysisContext c)
        {
            var methodSymbol = (IMethodSymbol)c.Symbol;

            if (methodSymbol.IsExtern &&
                methodSymbol.IsPubliclyAccessible() &&
                methodSymbol.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax() is MethodDeclarationSyntax methodDeclaration)
            {
                c.ReportDiagnosticWhenActive(Diagnostic.Create(rule, methodDeclaration.Identifier.GetLocation(),
                                                               MakeThisMethodPrivateMessage));
            }
        }
 protected void CheckExpectedFieldIsUsed(IFieldSymbol expectedField, FieldData?actualField, SymbolAnalysisContext context)
 {
     if (actualField.HasValue && actualField.Value.Field != expectedField)
     {
         context.ReportDiagnosticWhenActive(Diagnostic.Create(
                                                Rule,
                                                actualField.Value.LocationNode.GetLocation(),
                                                actualField.Value.AccessorKind == AccessorKind.Getter ? "getter" : "setter",
                                                expectedField.Name
                                                ));
     }
 }
        protected void CheckExpectedFieldIsUsed(IMethodSymbol methodSymbol, IFieldSymbol expectedField, IEnumerable <FieldData> actualFields, SymbolAnalysisContext context)
        {
            var expectedFieldIsUsed = actualFields.Any(a => a.Field == expectedField);

            if (!expectedFieldIsUsed || !actualFields.Any())
            {
                var locationAndAccessorType = GetLocationAndAccessor(actualFields, methodSymbol);
                if (locationAndAccessorType.Item1 != null)
                {
                    context.ReportDiagnosticWhenActive(Diagnostic.Create(
                                                           Rule,
                                                           locationAndAccessorType.Item1,
                                                           locationAndAccessorType.Item2,
                                                           expectedField.Name
                                                           ));
                }
            }

            Tuple <Location, string> GetLocationAndAccessor(IEnumerable <FieldData> fields, IMethodSymbol method)
            {
                Location location     = null;
                string   accessorType = null;

                if (fields.Count() == 1)
                {
                    var fieldWithValue = fields.First();
                    location     = fieldWithValue.LocationNode.GetLocation();
                    accessorType = fieldWithValue.AccessorKind == AccessorKind.Getter ? "getter" : "setter";
                }
                else
                {
                    Debug.Assert(method != null, "Method symbol should not be null at this point");
                    location     = method?.Locations.First();
                    accessorType = method?.MethodKind == MethodKind.PropertyGet ? "getter" : "setter";
                }
                return(Tuple.Create(location, accessorType));
            }
        }