public void Dispose_ResetsValueToDefault()
        {
            IDisposableSource<string> disposableSource = new DisposableSource<string>(source);

            disposableSource.Dispose();

            Assert.Null(source.Value);
        }
Пример #2
0
        public void Dispose_ResetsValueToDefault()
        {
            IDisposableSource <string> disposableSource = new DisposableSource <string>(source);

            disposableSource.Dispose();

            Assert.Null(source.Value);
        }
 private CustomAnalysisContext(SyntaxNodeAnalysisContext context, SyntaxNode node, SyntaxNode originalNode,
                               DisposableSource source, INamedTypeSymbol type, IDetector detector, IConfiguration configuration)
 {
     Context       = context;
     Source        = source;
     Type          = type;
     Detector      = detector;
     Configuration = configuration;
     Node          = node;
     OriginalNode  = originalNode;
 }
        public void Dispose_CallsAction()
        {
            bool called = false;

            IDisposableSource<string> disposableSource = new DisposableSource<string>(source, () => called = true);

            Assert.False(called);

            disposableSource.Dispose();

            Assert.True(called);
        }
Пример #5
0
        public void Dispose_CallsAction()
        {
            bool called = false;

            IDisposableSource <string> disposableSource = new DisposableSource <string>(source, () => called = true);

            Assert.False(called);

            disposableSource.Dispose();

            Assert.True(called);
        }
Пример #6
0
        public void DontTouchWaveSourceTest()
        {
            using (var source = new DisposableSource(GetWaveSource()))
            {
                _soundOut.Initialize(source);
                _soundOut.Play();

                Thread.Sleep(100);

                Assert.AreEqual(PlaybackState.Playing, _soundOut.PlaybackState);

                _soundOut.Dispose();

                Assert.IsFalse(source.IsDisposed);
            }
        }
        private static void AnalyseNodeInArgumentList(SyntaxNodeAnalysisContext context,
                                                      SyntaxNode node, DisposableSource source)
        {
            var objectCreation = node.Parent.Parent.Parent as ObjectCreationExpressionSyntax;
            var t = context.SemanticModel.GetReturnTypeOf(objectCreation);

            if (t == null)
            {
                return;           //return type could not be determind
            }
            if (Detector.IsTrackedType(t, objectCreation, context.SemanticModel))
            {
                return;
            }

            context.ReportNotDisposedAnonymousObject(source);
        }
        private static void AnalyseNodeWithinVariableDeclarator(SyntaxNodeAnalysisContext context,
                                                                SyntaxNode node, DisposableSource source)
        {
            var identifier = node.GetIdentifierIfIsPartOfVariableDeclarator();//getIdentifier

            if (identifier == null)
            {
                return;
            }
            if (node.IsLocalDeclaration()) //var m = new MemoryStream();
            {
                AnalyseNodeWithinLocalDeclaration(context, node, identifier);
            }
            else if (node.IsFieldDeclaration()) //_field = new MemoryStream();
            {
                AnalyseNodeInFieldDeclaration(context, node, identifier, source);
            }
        }
Пример #9
0
        public void SoundOutBehaviour()
        {
            using (var source = new DisposableSource(GetLoopingWaveSource()))
            {
                Assert.AreEqual(PlaybackState.Stopped, _soundOut.PlaybackState);
                _soundOut.Initialize(source);
                Assert.AreEqual(PlaybackState.Stopped, _soundOut.PlaybackState);
                _soundOut.Play();
                Assert.AreEqual(PlaybackState.Playing, _soundOut.PlaybackState);
                Thread.Sleep(20);
                _soundOut.Pause();
                Assert.AreEqual(PlaybackState.Paused, _soundOut.PlaybackState);
                Thread.Sleep(20);
                _soundOut.Resume();
                Assert.AreEqual(PlaybackState.Playing, _soundOut.PlaybackState);
                Thread.Sleep(20);
                _soundOut.Dispose();

                Assert.IsFalse(source.IsDisposed, "{0} disposed source.", _soundOut.GetType().FullName);
            }
        }
Пример #10
0
        private static void AnalyseNodeInFieldDeclaration(SyntaxNodeAnalysisContext context,
                                                          SyntaxNode node, string identifier, DisposableSource source)
        {
            var disposeMethod = node.FindContainingClass().DescendantNodes <MethodDeclarationSyntax>()
                                .FirstOrDefault(method => method.Identifier.Text == DisposeMethod);

            if (disposeMethod == null)
            {
                //there is no dispose method in this class
                context.ReportNotDisposedField(source);
                return;
            }

            var isDisposed = disposeMethod.DescendantNodes <InvocationExpressionSyntax>()
                             .Select(invo => invo.Expression as MemberAccessExpressionSyntax)
                             .Any(invo =>
            {
                var id            = invo.Expression as IdentifierNameSyntax;
                var member        = id?.Identifier.Text == identifier;
                var callToDispose = invo.Name.Identifier.Text == DisposeMethod;

                return(member && callToDispose);
            });

            if (isDisposed)
            {
                return;
            }
            //there is a dispose method in this class, but ObjectCreation is not disposed
            context.ReportNotDisposedField(source);
        }
Пример #11
0
        public static void ReportNotDisposedProperty(this SyntaxNodeAnalysisContext context, string variableName, DisposableSource source)
        {
            var location = context.Node.GetLocation();

            var properties = ImmutableDictionary.CreateBuilder <string, string>();

            properties.Add(Constants.Variablename, variableName);

            context.ReportDiagnostic(source == DisposableSource.InvokationExpression
                ? Diagnostic.Create(AssignmendFromMethodInvocationToPropertyNotDisposedDescriptor, location, properties.ToImmutable())
                : Diagnostic.Create(AssignmendFromObjectCreationToPropertyNotDisposedDescriptor, location, properties.ToImmutable())
                                     );
        }
Пример #12
0
        public void Value()
        {
            IDisposableSource <string> disposableSource = new DisposableSource <string>(source);

            Assert.Equal("A", disposableSource.Value);
        }
        public static void ReportNotDisposedAnonymousObject(this SyntaxNodeAnalysisContext context, DisposableSource source)
        {
            var location = context.Node.GetLocation();

            context.ReportDiagnostic(source == DisposableSource.InvokationExpression
                ? Diagnostic.Create(AnonymousObjectFromMethodInvokationDescriptor, location)
                : Diagnostic.Create(AnonymousObjectFromObjectCreationDescriptor, location));
        }
        public static void ReportNotDisposedAssignmentToFieldOrProperty(this SyntaxNodeAnalysisContext context, DisposableSource source)
        {
            var location = context.Node.GetLocation();

            context.ReportDiagnostic(Diagnostic.Create(NotDisposedDescriptor, location));
        }
Пример #15
0
        public void Value()
        {
            IDisposableSource<string> disposableSource = new DisposableSource<string>(source);

            Assert.Equal("A", disposableSource.Value);
        }
Пример #16
0
        private static void AnalyseNodeInFieldDeclaration(SyntaxNodeAnalysisContext context,
                                                          SyntaxNode node, string variableName, DisposableSource source)
        {
            if (node.IsDisposedInDisposingMethod(variableName, Configuration, context.SemanticModel))
            {
                return;
            }

            context.ReportNotDisposedField(variableName, source);
        }
Пример #17
0
        public static void ReportNotDisposedProperty(this SyntaxNodeAnalysisContext context, DiagnosticDescriptor rule, string variableName, DisposableSource source)
        {
            var location = context.Node.GetLocation();

            var properties = ImmutableDictionary.CreateBuilder<string, string>();
            properties.Add(Constants.Variablename, variableName);

            context.ReportDiagnostic(Diagnostic.Create(rule, location, properties.ToImmutable()));

        }
Пример #18
0
        private void AnalyseNodeInReturnStatementOfProperty(SyntaxNodeAnalysisContext context, SyntaxNode node, DisposableSource source)
        {
            var propertyDeclaration = node.Parent.Parent.Parent.Parent.Parent as PropertyDeclarationSyntax;

            if (propertyDeclaration == null)
            {
                return;                              // should not happen => we cke this before
            }
            if (node.IsDisposedInDisposingMethod(propertyDeclaration.Identifier.Text, Configuration, context.SemanticModel))
            {
                return;
            }
            var rule = GetRule(DiagnosticId, CurrentSeverity);

            context.ReportNotDisposedProperty(rule, propertyDeclaration.Identifier.Text, source);
        }
Пример #19
0
        private void AnalyseNodeInAssignmentExpression(SyntaxNodeAnalysisContext context,
                                                       SyntaxNode node, DisposableSource source)
        {
            //is local or global variable
            var assignmentExrepssion = node.Parent as AssignmentExpressionSyntax;
            var variableName         = (assignmentExrepssion?.Left as IdentifierNameSyntax)?.Identifier.Text;

            var rule = GetRule(DiagnosticId, CurrentSeverity);

            MethodDeclarationSyntax containingMethod;

            if (node.TryFindContainingMethod(out containingMethod))
            {
                if (containingMethod.ContainsDisposeCallFor(variableName, context.SemanticModel, Configuration))
                {
                    return;
                }

                if (containingMethod.HasDecendentVariableDeclaratorFor(variableName))
                {
                    //local declaration in method
                    if (containingMethod.Returns(variableName))
                    {
                        return;
                    }
                    if (node.IsDescendantOfUsingHeader())
                    {
                        return;
                    }
                    if (node.IsArgumentInObjectCreation())
                    {
                        AnalyseNodeInArgumentList(context, node, source);
                        return;
                    }
                    //is part of tracking call
                    context.ReportNotDisposedLocalVariable(rule);
                    return;
                }
                if (node.IsDisposedInDisposingMethod(variableName, Configuration, context.SemanticModel))
                {
                    return;
                }
                if (node.IsArgumentInObjectCreation())
                {
                    AnalyseNodeInArgumentList(context, node, source);
                    return;
                }

                //assignment to field or property
                var containingClass = node.FindContainingClass();
                if (containingClass == null)
                {
                    return;
                }
                if (containingClass.FindFieldNamed(variableName) != null)
                {
                    context.ReportNotDisposedField(rule, variableName, source);
                }
                else
                {
                    context.ReportNotDisposedProperty(rule, variableName, source);
                }

                return;
            }
            ConstructorDeclarationSyntax ctor;

            if (node.TryFindContainingCtor(out ctor))
            {
                if (ctor.HasDecendentVariableDeclaratorFor(variableName))
                {
                    //local variable in ctor
                    if (node.IsDescendantOfUsingHeader())
                    {
                        return;
                    }
                    if (node.IsArgumentInObjectCreation())
                    {
                        AnalyseNodeInArgumentList(context, node, source);
                        return;
                    }
                    if (ctor.ContainsDisposeCallFor(variableName, context.SemanticModel, Configuration))
                    {
                        return;
                    }
                    context.ReportNotDisposedLocalVariable(rule);
                }
                else //field or property
                {
                    if (node.IsDisposedInDisposingMethod(variableName, Configuration, context.SemanticModel))
                    {
                        return;
                    }

                    if (node.IsAssignmentToProperty(variableName))
                    {
                        context.ReportNotDisposedProperty(rule, variableName, source);
                    }
                    else
                    {
                        context.ReportNotDisposedField(rule, variableName, source);
                    }
                }
            }
        }
Пример #20
0
        private void AnalyseNodeInFieldDeclaration(SyntaxNodeAnalysisContext context,
                                                   SyntaxNode node, string variableName, DisposableSource source)
        {
            if (node.IsDisposedInDisposingMethod(variableName, Configuration, context.SemanticModel))
            {
                return;
            }

            var rule = GetRule(DiagnosticId, CurrentSeverity);

            context.ReportNotDisposedField(rule, variableName, source);
        }
Пример #21
0
        private void CheckIfObjectCreationTracksNode(SyntaxNodeAnalysisContext context, ObjectCreationExpressionSyntax objectCreation, DisposableSource source)
        {
            var t = context.SemanticModel.GetReturnTypeOf(objectCreation);

            if (t == null)
            {
                return;           //return type could not be determind
            }
            if (Detector.IsTrackedType(t, objectCreation, context.SemanticModel))
            {
                return;
            }

            var rule = GetRule(DiagnosticId, CurrentSeverity);

            context.ReportNotDisposedAnonymousObject(rule, source);
        }
 public static CustomAnalysisContext WithOriginalNode(SyntaxNodeAnalysisContext context, DisposableSource source,
                                                      INamedTypeSymbol type, IDetector detector, IConfiguration configuration)
 {
     return(new CustomAnalysisContext(context, context.Node, context.Node, source, type, detector, configuration));
 }
Пример #23
0
        private static void AnalyseNodeInAssignmentExpression(SyntaxNodeAnalysisContext context,
                                                              SyntaxNode node, DisposableSource source)
        {
            //is local or global variable
            var assignmentExrepssion = node.Parent as AssignmentExpressionSyntax;
            var variableName         = (assignmentExrepssion?.Left as IdentifierNameSyntax)?.Identifier.Text;

            MethodDeclarationSyntax containingMethod;

            if (node.TryFindContainingMethod(out containingMethod))
            {
                if (containingMethod.ContainsDisposeCallFor(variableName))
                {
                    return;
                }

                if (containingMethod.HasDecendentVariableDeclaratorFor(variableName))
                {
                    //local declaration in method
                    if (containingMethod.Returns(variableName))
                    {
                        return;
                    }
                    if (node.IsDescendantOfUsingHeader())
                    {
                        return;
                    }
                    if (node.IsArgumentInObjectCreation())
                    {
                        AnalyseNodeInArgumentList(context, node, source);
                        return;
                    }
                    //is part of tracking call
                    context.ReportNotDisposedLocalDeclaration();
                    return;
                }
                //field declaration
                if (node.IsDisposedInDisposedMethod(variableName))
                {
                    return;
                }
                if (node.IsArgumentInObjectCreation())
                {
                    AnalyseNodeInArgumentList(context, node, source);
                    return;
                }
                context.ReportNotDisposedField(source);
                return;
            }
            ConstructorDeclarationSyntax ctor;

            if (node.TryFindContainingCtor(out ctor))
            {
                if (ctor.HasDecendentVariableDeclaratorFor(variableName))
                {
                    //local variable in ctor
                    if (node.IsDescendantOfUsingHeader())
                    {
                        return;
                    }
                    if (node.IsArgumentInObjectCreation())
                    {
                        AnalyseNodeInArgumentList(context, node, source);
                        return;
                    }
                    if (ctor.ContainsDisposeCallFor(variableName))
                    {
                        return;
                    }
                    context.ReportNotDisposedLocalDeclaration();
                }
                else //field
                {
                    if (node.IsDisposedInDisposedMethod(variableName))
                    {
                        return;
                    }
                    context.ReportNotDisposedField(source);
                    return;
                }
            }
            //part of a property?
        }
Пример #24
0
        public static void ReportNotDisposedAnonymousObject(this SyntaxNodeAnalysisContext context, DiagnosticDescriptor rule, DisposableSource source)
        {
            var location = context.Node.GetLocation();

            context.ReportDiagnostic(Diagnostic.Create(rule, location));
        }
Пример #25
0
        private static void AnalyseNodeInAutoPropertyOrPropertyExpressionBody(SyntaxNodeAnalysisContext context, SyntaxNode node, DisposableSource source)
        {
            var propertyDeclaration = node.Parent.Parent as PropertyDeclarationSyntax;

            if (propertyDeclaration == null)
            {
                return;                              // should not happen => we cke this before
            }
            if (node.IsDisposedInDisposingMethod(propertyDeclaration.Identifier.Text, Configuration, context.SemanticModel))
            {
                return;
            }
            context.ReportNotDisposedProperty(propertyDeclaration.Identifier.Text, source);
        }