Пример #1
0
        public Inspection Analyze(SyntaxNode node)
        {
            if (!(node is BlockSyntax root) ||
                root.DescendantNodes().Any() ||
                root.Parent is SimpleLambdaExpressionSyntax ||
                root.Parent is LambdaExpressionSyntax ||
                root.Parent is ParenthesizedLambdaExpressionSyntax)
            {
                return(null);
            }

            Description   = "This code has a blank block to do nothing. Sometimes this means the code missed to implement here";
            CodeReviewUrl = "https://confluence.devfactory.com/display/CodeReview/Blank+code+block";

            if (root.Parent is IfStatementSyntax)
            {
                Description   = "This method contains an unnecessary empty if statement";
                Severity      = Constants.SeverityType.Yellow.ToString();
                CodeReviewUrl = "https://confluence.devfactory.com/display/CodeReview/Empty+and+unnecessary+if+statement";
            }
            else if (root.Parent is CatchClauseSyntax)
            {
                Description   = @"The exception is ignored (""swallowed"") by the try-catch block.";
                CodeReviewUrl = "https://confluence.devfactory.com/display/CodeReview/Catching+and+Ignoring+Exception";
            }

            return(Inspection.Create(this, node.LineNumber() + 1, node.ToFullString()));
        }
Пример #2
0
        public Inspection Analyze(SyntaxNode node)
        {
            if (_model == null)
            {
                return(null);
            }

            if (!(node is ObjectCreationExpressionSyntax))
            {
                return(null);
            }

            var symbolicInfo = _model.GetTypeInfo(node);

            if (symbolicInfo.Type == null)
            {
                return(null);
            }

            var isIDisposable = symbolicInfo
                                .Type
                                .AllInterfaces.Any(x => x.Name == nameof(IDisposable) || x.MetadataName == nameof(IDisposable));

            return(isIDisposable
                ? Inspection.Create(this, node.LineNumber() + 1, node.ToFullString())
                : null);
        }
Пример #3
0
        public Inspection Analyze(SyntaxNode node)
        {
            if (!(node is MethodDeclarationSyntax method))
            {
                return(null);
            }

            var attributes = method.AttributeLists.Where(x => x.Attributes.Any(y => y.Name.ToString() == "TestCase")).SelectMany(x => x.Attributes).ToList();

            if (!attributes.Any())
            {
                return(null);
            }

            if (attributes.Count == 1)
            {
                return(Inspection.Create(this, node.LineNumber() + 1, node.ToFullString()));
            }

            var firstAttribute = attributes.First();

            for (var attrArgIndex = 0; attrArgIndex < firstAttribute.ArgumentList.Arguments.Count; attrArgIndex++)
            {
                if (attributes.Skip(1).All(x => x.ArgumentList.Arguments[attrArgIndex].ToString() ==
                                           firstAttribute.ArgumentList.Arguments[attrArgIndex].ToString()))
                {
                    return(Inspection.Create(this, node.LineNumber() + 1, node.ToFullString()));
                }
            }

            return(null);
        }
Пример #4
0
        public Inspection Analyze(SyntaxNode node)
        {
            if (!(node is StructDeclarationSyntax))
            {
                return(null);
            }

            return(Inspection.Create(this, node.LineNumber() + 1, node.ToFullString()));
        }
Пример #5
0
        public Inspection Analyze(SyntaxNode node)
        {
            if (!(node is EnumDeclarationSyntax root) ||
                root.Members.Any(x => x.EqualsValue.Value.ToString() == "0"))
            {
                return(null);
            }

            return(Inspection.Create(this, node.LineNumber() + 1, node.ToFullString()));
        }
        public Inspection Analyze(SyntaxNode node)
        {
            if (!(node is SwitchStatementSyntax root) ||
                root.DescendantNodes()
                .OfType <DefaultSwitchLabelSyntax>().Any())
            {
                return(null);
            }

            return(Inspection.Create(this, node.LineNumber() + 1, node.ToFullString()));
        }
        public Inspection Analyze(SyntaxNode node)
        {
            if (!(node is ParameterListSyntax root) ||
                root.Parent is ParenthesizedLambdaExpressionSyntax ||
                root.Parameters.Count <= 7)
            {
                return(null);
            }

            return(Inspection.Create(this, node.LineNumber() + 1, node.Parent.ToFullString()));
        }
        public Inspection Analyze(SyntaxNode node)
        {
            var root = node as ObjectCreationExpressionSyntax;

            var exceptionWithoutContext = root?.Parent is ThrowStatementSyntax &&
                                          !root.ArgumentList.Arguments.Any();

            if (root == null || !exceptionWithoutContext)
            {
                return(null);
            }

            return(Inspection.Create(this, node.LineNumber() + 1, node.Parent.ToFullString()));
        }
        public Inspection Analyze(SyntaxNode node)
        {
            if (!(node is FieldDeclarationSyntax root) ||
                root.Modifiers.ToString().Contains("private") ||
                root.Modifiers.ToString().Contains("internal") ||
                root.Modifiers.ToString().Contains("const") ||
                root.Modifiers.ToString().Contains("static") ||
                string.IsNullOrWhiteSpace(root.Modifiers.ToString()) ||
                root.Declaration.Variables.Count > 1)
            {
                return(null);
            }

            return(Inspection.Create(this, node.LineNumber() + 1, node.ToFullString()));
        }
Пример #10
0
        public Inspection Analyze(SyntaxNode node)
        {
            if (!(node is IdentifierNameSyntax syntax) || !syntax.Identifier.ValueText.Equals("ShouldSatisfyAllConditions", StringComparison.InvariantCulture))
            {
                return(null);
            }

            var invocation = node.Ancestors <InvocationExpressionSyntax>().FirstOrDefault();

            if (invocation == null || invocation.ArgumentList.Arguments.Count > 1)
            {
                return(null);
            }

            return(Inspection.Create(this, node.LineNumber() + 1, node.ToFullString()));
        }
Пример #11
0
        public Inspection Analyze(SyntaxNode node)
        {
            if (_nodeLocations == null || !_nodeLocations.Any())
            {
                return(null);
            }

            if (!(node is ParameterSyntax parameter))
            {
                return(null);
            }

            return(_nodeLocations.All(x => x != parameter.Identifier.GetLocation())
                ? null
                : Inspection.Create(this, node.LineNumber() + 1, node.ToFullString()));
        }
Пример #12
0
        public Inspection Analyze(SyntaxNode context)
        {
            if (!(context is UsingStatementSyntax node))
            {
                return(null);
            }

            if (!node.Expression.ToString().Contains("ShimsContext.Create()"))
            {
                return(null);
            }

            return(node.Statement.ToString().Contains("Shim")
                ? null
                : Inspection.Create(this, node.LineNumber() + 1, node.ToFullString()));
        }
Пример #13
0
        public Inspection Analyze(SyntaxNode node)
        {
            if (!(node is MethodDeclarationSyntax syntax) ||
                syntax.DescendantNodes <IdentifierNameSyntax>()
                .Any(x => x.Identifier.ValueText.Equals("ShouldSatisfyAllConditions", StringComparison.InvariantCulture)))
            {
                return(null);
            }

            return(syntax.DescendantNodes <IdentifierNameSyntax>()
                   .Count(x => x.Identifier.ValueText.StartsWith("ShouldBe") ||
                          x.Identifier.ValueText.StartsWith("ShouldNotBe") ||
                          x.Identifier.ValueText.StartsWith("ShouldThrow")) <= 2
                ? null
                : Inspection.Create(this, node.LineNumber() + 1, node.ToFullString()));
        }
Пример #14
0
        public Inspection Analyze(SyntaxNode node)
        {
            if (_nodeLocations == null || !_nodeLocations.Any())
            {
                return(null);
            }

            if (!(node is VariableDeclaratorSyntax variable))
            {
                return(null);
            }

            return(_nodeLocations.All(x => x != variable.Identifier.GetLocation())
                ? null
                : Inspection.Create(this, node.LineNumber() + 1, node.ToFullString()));
        }
Пример #15
0
        public Inspection Analyze(SyntaxNode node)
        {
            if (!(node is ForEachStatementSyntax root))
            {
                return(null);
            }

            var enumerable = root.Expression.ToString();

            if (!(enumerable.Contains(".ToArray()") ||
                  enumerable.Contains(".ToList()")))
            {
                return(null);
            }

            return(Inspection.Create(this, node.LineNumber() + 1, node.Parent.ToFullString()));
        }
Пример #16
0
        public Inspection Analyze(SyntaxNode node)
        {
            var root     = node as ClassDeclarationSyntax;
            var keywords = new[]
            {
                "Manager",
                "Processor",
                "Data",
                "Info"
            };

            if (root != null &&
                keywords.Any(x => root.Identifier.ValueText.EndsWith(x)))
            {
                return(Inspection.Create(this, node.LineNumber() + 1, node.ToFullString()));
            }

            return(null);
        }
Пример #17
0
        public Inspection Analyze(SyntaxNode node)
        {
            if (!(node is MemberAccessExpressionSyntax))
            {
                return(null);
            }

            if (!node.ToString().EndsWith("AllInstances"))
            {
                return(null);
            }

            var method = node.Ancestors <MethodDeclarationSyntax>().FirstOrDefault();

            return(method?.DescendantNodes <ObjectCreationExpressionSyntax>()
                   .Any(obj => node.ToString() == $"{obj.Type}.AllInstances") == true
                ? Inspection.Create(this, node.LineNumber() + 1, node.ToFullString())
                : null);
        }
Пример #18
0
        public Inspection Analyze(SyntaxNode node)
        {
            if (!(node is PropertyDeclarationSyntax property))
            {
                return(null);
            }

            if (property.DescendantNodes <ArrowExpressionClauseSyntax>().Count < 4)
            {
                return(null);
            }

            if (!(property.DescendantNodes <InvocationExpressionSyntax>().Any() ||
                  property.DescendantNodes <ObjectCreationExpressionSyntax>().Any()))
            {
                return(null);
            }

            return(Inspection.Create(this, node.LineNumber() + 1, node.ToFullString()));
        }
        public Inspection Analyze(SyntaxNode node)
        {
            if (!(node is MemberAccessExpressionSyntax))
            {
                return(null);
            }

            if (!node.ToString().Contains(".AllInstances."))
            {
                return(null);
            }

            var method = node.Ancestors <MethodDeclarationSyntax>().FirstOrDefault();

            return(method?
                   .DescendantNodes <MemberAccessExpressionSyntax>()
                   .Count(x => x.ToString() == node.ToString()) > 1
                ? Inspection.Create(this, node.LineNumber() + 1, node.ToFullString())
                : null);
        }
Пример #20
0
        public Inspection Analyze(SyntaxNode node)
        {
            if (_model == null)
            {
                return(null);
            }

            if (!(node is PropertyDeclarationSyntax property))
            {
                return(null);
            }

            var privateType = !(property.Modifiers.ToString().Contains(PublicModifier) ||
                                property.Modifiers.ToString().Contains(ProtectedModifier));

            if (privateType ||
                property.AccessorList == null ||
                property.AccessorList.Accessors.Count == 1 &&
                property.AccessorList.Accessors[0].ToFullString().Contains(Getter))
            {
                return(null);
            }

            var symbolicInfo = _model.GetTypeInfo(node.ChildNodes().FirstOrDefault());

            if (symbolicInfo.Type == null)
            {
                return(null);
            }

            var iEnumerable = symbolicInfo
                              .Type
                              .AllInterfaces.Any(x =>
                                                 x.Name == nameof(IEnumerable) ||
                                                 x.MetadataName == nameof(IEnumerable));

            return(iEnumerable
                ? Inspection.Create(this, node.LineNumber() + 1, node.ToFullString())
                : null);
        }
Пример #21
0
        public Inspection Analyze(SyntaxNode node)
        {
            if (!(node is InitializerExpressionSyntax initializer))
            {
                return(null);
            }

            if (!initializer.ChildNodes().All(y => y.Kind() == SyntaxKind.ObjectCreationExpression &&
                                              (y as ObjectCreationExpressionSyntax)?.Type?.ToString() == "TestCaseData"))
            {
                return(null);
            }

            var testCases = initializer.ChildNodes().Cast <ObjectCreationExpressionSyntax>().ToList();

            if (!testCases.Any())
            {
                return(null);
            }

            if (testCases.Count == 1)
            {
                return(Inspection.Create(this, node.LineNumber() + 1, node.ToFullString()));
            }

            var firstAttribute = testCases.First();

            for (var attrArgIndex = 0; attrArgIndex < firstAttribute.ArgumentList.Arguments.Count; attrArgIndex++)
            {
                if (testCases.Skip(1).All(x => x.ArgumentList.Arguments[attrArgIndex].ToString() ==
                                          firstAttribute.ArgumentList.Arguments[attrArgIndex].ToString()))
                {
                    return(Inspection.Create(this, node.LineNumber() + 1, node.ToFullString()));
                }
            }

            return(null);
        }
Пример #22
0
        public Inspection Analyze(SyntaxNode node)
        {
            if (!(node is ParameterListSyntax root) ||
                root.Parent is ParenthesizedLambdaExpressionSyntax ||
                !root.Parameters.Any() ||
                root.Parameters.All(x =>
            {
                var type = x.Type.ToString();
                return(type != BoolKeyword && type != BooleanKeyword);
            }))
            {
                return(null);
            }

            var method = node.Ancestors <MethodDeclarationSyntax>().FirstOrDefault();

            if (method != null && method.AttributeLists.Any())
            {
                return(null);
            }

            return(Inspection.Create(this, node.LineNumber() + 1, node.Parent.ToFullString()));
        }
        public Inspection Analyze(SyntaxNode node)
        {
            var root = node as IdentifierNameSyntax;
            var genericExceptions = new[]
            {
                "Exception",
                "ApplicationException",
                "SystemException",
                "ExecutionEngineException",
                "IndexOutOfRangeException",
                "NullReferenceException",
                "OutOfMemoryException"
            };

            if (root == null ||
                genericExceptions.All(x => root.Identifier.ValueText != x) ||
                !(root.Parent is ObjectCreationExpressionSyntax &&
                  root.Parent.Parent is ThrowStatementSyntax))
            {
                return(null);
            }

            return(Inspection.Create(this, node.LineNumber() + 1, node.Parent.ToFullString()));
        }
Пример #24
0
        public Inspection Analyze(SyntaxNode node)
        {
            if (!(node is FieldDeclarationSyntax field))
            {
                return(null);
            }

            var privateField = !(field.Modifiers.ToString().Contains(PublicModifier) ||
                                 field.Modifiers.ToString().Contains(ProtectedModifier));

            if (privateField)
            {
                return(null);
            }

            var containConstant = field.Modifiers.ToString().Contains(ConstantModifier);

            if (!containConstant)
            {
                return(null);
            }

            return(Inspection.Create(this, node.LineNumber() + 1, node.ToFullString()));
        }
Пример #25
0
        public Inspection Analyze(SyntaxNode node)
        {
            var predefinedType = node as PredefinedTypeSyntax;
            var parameterType  = node as ParameterSyntax;
            var genericType    = node as GenericNameSyntax;

            var collectionTypes = new[]
            {
                "Enumerable",
                "ReadOnlyCollection",
                "Collection",
                "ReadOnlyList",
                "Dictionary",
                "List"
            };

            if (predefinedType == null &&
                genericType == null &&
                parameterType == null)
            {
                return(null);
            }

            if (genericType?.Parent is ObjectCreationExpressionSyntax ||
                genericType?.Parent is TypeOfExpressionSyntax ||
                genericType?.Parent is CastExpressionSyntax ||
                genericType?.Parent is BinaryExpressionSyntax ||
                genericType?.Parent is TypeArgumentListSyntax ||
                genericType?.Parent?.Parent is ArgumentSyntax ||
                predefinedType?.Parent is ObjectCreationExpressionSyntax ||
                predefinedType?.Parent is TypeOfExpressionSyntax ||
                predefinedType?.Parent is BinaryExpressionSyntax ||
                predefinedType?.Parent is TypeArgumentListSyntax ||
                predefinedType?.Parent is CastExpressionSyntax ||
                predefinedType?.Parent?.Parent is ArgumentSyntax ||
                parameterType?.Parent is TypeOfExpressionSyntax)
            {
                return(null);
            }

            if (CheckPublicMethodOrField(genericType))
            {
                return(null);
            }

            if (CheckPublicMethodOrField(predefinedType))
            {
                return(null);
            }

            if (predefinedType?.IsVar == true ||
                genericType?.IsVar == true)
            {
                return(null);
            }

            if (parameterType != null &&
                !(parameterType.Modifiers.ToString().Contains(PublicModifier) ||
                  parameterType.Modifiers.ToString().Contains(ProtectedModifier)))
            {
                return(null);
            }

            var type = predefinedType?.ToString() ??
                       parameterType?.Type.ToString() ??
                       genericType.Identifier.ValueText ??
                       string.Empty;

            if (type.Contains("."))
            {
                type = type
                       .Split('.')
                       .Last();
            }

            if (!collectionTypes.Any(x => type.StartsWith(x)))
            {
                return(null);
            }

            return(Inspection.Create(this, node.LineNumber() + 1, node.Parent.ToFullString()));
        }
Пример #26
0
        public Inspection Analyze(SyntaxNode node)
        {
            var          root               = node as IdentifierNameSyntax;
            const string valueKeyWord       = "value";
            var          contextualKeywords = new[]
            {
                "add",
                "alias",
                "ascending",
                "async",
                "await",
                "by",
                "descending",
                "dynamic",
                "equals",
                "from",
                "get",
                "global",
                "group",
                "into",
                "join",
                "let",
                "nameof",
                "on",
                "orderby",
                "partial",
                "remove",
                "select",
                "set",
                valueKeyWord,
                "var",
                "when",
                "where",
                "yield"
            };

            if (root == null ||
                root.Parent is InvocationExpressionSyntax ||
                root.Parent is VariableDeclarationSyntax ||
                root.Parent is ForEachStatementSyntax ||
                root.Parent is ForStatementSyntax ||
                root.Parent is WhileStatementSyntax ||
                root.Parent is SwitchStatementSyntax ||
                root.Parent is DeclarationExpressionSyntax ||
                root.Parent is TypeOfExpressionSyntax ||
                root.Parent is MethodDeclarationSyntax ||
                root.Parent is ParameterSyntax ||
                root.Parent.Parent is GenericNameSyntax ||
                contextualKeywords.All(x => x != root.Identifier.ValueText))
            {
                return(null);
            }

            if (root.Ancestors().Any(x => x.IsKind(SyntaxKind.SetAccessorDeclaration)) &&
                root.Identifier.ValueText == valueKeyWord)
            {
                return(null);
            }

            return(Inspection.Create(this, node.LineNumber() + 1, node.Parent.ToFullString()));
        }