dotnet / roslyn-analyzers

MIT License
1.59k stars 465 forks source link

False positive for CA2000 #3008

Open vsfeedback opened 4 years ago

vsfeedback commented 4 years ago

This issue has been moved from a ticket on Developer Community.


Visual Studio 2019 version 16.3.6,

Microsoft.CodeAnalysis.FxCopAnalyzers 2.9.7

I'm trying to fix all warnings and I did everything like here:

https://docs.microsoft.com/ru-ru/visualstudio/code-quality/ca2000?view=vs-2019

But your analizer still gives CA2000


Minimal example: https://ideone.com/aBqGlS


I can make the analizer happy with the code:

https://ideone.com/TYNay2

But I still think that your analizer has false positive for CA2000...


Original Comments

Visual Studio Feedback System on 11/3/2019, 10:55 PM:

We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.

Visual Studio Feedback System on 11/4/2019, 08:58 AM:

This issue is currently being investigated. Our team will get back to you if either more information is needed, a workaround is available, or the issue is resolved.


Original Solutions

(no solutions)

IADTIL commented 4 years ago

I have just had what I believe to be the same, or similar issue. I get this warning when inserting (Insert) a disposable object into a collection, though significantly, the warning is not shown when the same object is added (Add)! Some "minimal code" to reproduce it is below, along with the warning itself, followed by information about what versions are installed:


Minimal code:

using System;
using System.Collections.ObjectModel;

namespace InsertTest
{
    class Program
    {
        class Dummy : IDisposable
        {
            public Dummy()
            {
            }

            public void Dispose()
            {
            }
        }

        class DummyCollection : Collection<Dummy>
        {
            public DummyCollection()
            {
            }
        }

        static void Main()
        {
            DummyCollection dummies = new DummyCollection
            {
                new Dummy(),
            }; // No warning reported
            dummies.Add(new Dummy());  // No warning reported
            dummies.Insert(0, new Dummy());  // Warning CA2000
            dummies.Add(new Dummy());  // No warning reported
        }
    }
}

The warning:

Severity Code Description Tool Project File Line Source Suppression State Warning CA2000 Call System.IDisposable.Dispose on object created by 'new Dummy()' before all references to it are out of scope. Microsoft.NetCore.Analyzers InsertTest C:\Users\ianda\Documents\Visual Studio 2019\Projects\InsertTest\InsertTest\Program.cs 34 IntelliSense Active


Application information (Visual Studio):

Microsoft Visual Studio Community 2019 Version 16.4.4 VisualStudio.16.Release/16.4.4+29728.190 Microsoft .NET Framework Version 4.8.03752

Installed Version: Community

ASP.NET and Web Tools 2019 16.4.460.23317 ASP.NET and Web Tools 2019

Azure App Service Tools v3.0.0 16.4.460.23317 Azure App Service Tools v3.0.0

C# Tools 3.4.1-beta4-19614-01+165046097562cfe65b09c2e9a9d8f7cd88526f2c C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Clear Recent 1.1.1 Clears recent projects and files lists

Common Azure Tools 1.10 Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

IntelliCode Extension 1.0 IntelliCode Visual Studio Extension Detailed Info

NuGet Package Manager 5.4.0 NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/

ProjectServicesPackage Extension 1.0 ProjectServicesPackage Visual Studio Extension Detailed Info

ResXManager 1.38.3034.0 Manage localization of all ResX-Based resources in one place. Shows all resources of a solution and let's you edit the strings and their localizations in a well-arranged data grid.

Sandcastle Help File Builder SHFB Visual Studio integration for the Sandcastle Help File Builder. https://GitHub.com/EWSoftware/SHFB

TypeScript Tools 16.0.11031.2001 TypeScript Tools for Microsoft Visual Studio

Visual Basic Tools 3.4.1-beta4-19614-01+165046097562cfe65b09c2e9a9d8f7cd88526f2c Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Visual Studio Code Debug Adapter Host Package 1.0 Interop layer for hosting Visual Studio Code debug adapters in Visual Studio

Visual Studio Spell Check Everywhere VSSpellCheckEverywhere An extension that enables spell checking within any Visual Studio file editor or tool window that uses WPF text boxes. https://GitHub.com/EWSoftware/VSSpellChecker

Visual Studio Spell Checker VSSpellChecker An editor extension that checks the spelling of comments, strings, and plain text as you type or interactively with tool windows. https://GitHub.com/EWSoftware/VSSpellChecker

WiX Toolset Visual Studio Extension 1.0.0.4 WiX Toolset Visual Studio Extension version 1.0.0.4 Copyright (c) .NET Foundation and contributors. All rights reserved.


Microsoft.CodeAnalysis.FxCopAnalyzers 2.9.8

Microsoft recommended code quality rules and .NET API usage rules, including the most important FxCop rules, implemented as analyzers using the .NET Compiler Platform (Roslyn). These analyzers check your code for security, performance, and design issues, among others. The documentation for FxCop analyzers can be found at https://docs.microsoft.com/visualstudio/code-quality/install-fxcop-analyzers

Dependencies Microsoft.CodeAnalysis.VersionCheckAnalyzer (= 2.9.8) Microsoft.CodeQuality.Analyzers (= 2.9.8) Microsoft.NetCore.Analyzers (= 2.9.8) Microsoft.NetFramework.Analyzers (= 2.9.8)

Evangelink commented 4 years ago

Hi @IADTIL, could you move your comment as a new ticket? IMO that's not related to the FP that was raised. On your case that's a false negative (a place where an issue should be raised).

@mavasani I am not sure to understand why there is this special case here: https://github.com/dotnet/roslyn-analyzers/blob/master/src/Utilities/FlowAnalysis/FlowAnalysis/Framework/DataFlow/DataFlowOperationVisitor.cs#L1968-L1969

IADTIL commented 4 years ago

False positive or negative? No idea. It just doesn't handle collections correctly. So even if you make the collection disposable and add a Dispose method to call Dispose on all items in the collection, and then place that collection in a using statement, the warning is still displayed for Insert but not for Add. It would be extra irritating if this warning was incorrectly shown for Add too, so please don't do it!!

Minimal code modification:

        class DummyCollection : Collection<Dummy>, IDisposable
        {
            public DummyCollection()
            {
            }

            public void Dispose()
            {
                foreach (Dummy d in this)
                {
                    d.Dispose();
                }
                GC.SuppressFinalize(this);
            }
        }

        static void Main()
        {
            using (DummyCollection dummies = new DummyCollection())
            {
                dummies.Add(new Dummy());  // No warning reported. Good!
                dummies.Insert(0, new Dummy());  // Warning CA2000.  Bad!
                dummies.Add(new Dummy());  // No warning reported. Good!
            }
        }
VioletS commented 4 years ago

I also have a false positive on CA2000.

I have a disposable class MyImageType with a Bitmap member. In my Capture function, I capture a bitmap, prepare some other relevant data and pass it to the disposable class.

MyImageType CaptureMyImage()
{
    var image = Capture(); //returns a Bitmap
    var captureSettings = new CaptureSettings(some, variables, here);
    return new MyImageType(image, captureSettings);
}

CA2000 suggests image goes out of scope here and should be disposed.