Remove dead code
Many libraries and tests has some dead code. Please remove it per library.
At minimum we should run FxCop
(list might not be complete, following is just a list of folders under src/)
- [ ] Microsoft.Win32.Primitives
- [ ] Microsoft.Win32.Registry
- [ ] System.Collections.Concurrent
- [ ] System.Collections.Immutable
- [ ] System.Collections.NonGeneric
- [ ] System.Collections.Specialized
- [ ] System.ComponentModel.Annotations
- [ ] System.ComponentModel.Primitives
- [ ] System.ComponentModel
- [ ] System.Console
- [ ] System.Diagnostics.FileVersionInfo
- [ ] System.Diagnostics.Process
- [ ] System.Globalization.Extensions
- [ ] System.IO.FileSystem.DriveInfo
- [ ] System.IO.FileSystem.Primitives
- [ ] System.IO.FileSystem
- [ ] System.IO.MemoryMappedFiles
- [ ] System.IO.Pipes
- [x] System.IO.UnmanagedMemoryStream (https://github.com/dotnet/corefx/pull/1080)
- [ ] System.Linq.Parallel
- [ ] System.Numerics.Vectors
- [ ] System.Reflection.Metadata
- [x] System.Resources.ResourceWriter
- [ ] System.Runtime
- [ ] System.Text.Encoding.CodePages
- [ ] System.Text.RegularExpressions
- [x] System.Threading.Tasks.Dataflow
- [ ] System.Xml.XDocument
- [ ] System.Xml.XPath.XDocument
- [ ] System.Xml.XPath.XmlDocument
- [ ] System.Xml.XPath
- [ ] System.Xml.XmlDocument
Could we open an issue for each library that has dead code so that we can close each issue when we're done with it? Or is this issue tracking the opening of all of those other issues? :smile:
We can add checkboxes here and just select them once it is done. I don't personally like hundreds similar issues.
@krwq What is your idea for finding this dead code? As for running FxCop at some point when we switch to using only the Roslyn compiler we will be enabling the code analytics that come with it.
As for the check-boxes I've personally found them way to easy to accidently check/uncheck. I think for now keep that list is good but perhaps instead of a check-box we just update each item with the associated PR/commit id when it is done so we don't accidently lose the check mark.
I just added this item so we won't forget to do it. Any tool is fine. If we are switching to Roslyn compiler soon then let's wait until then so we can test their features in real scenario.
I agree about adding PR to the item when checking.
FWIW, I too would prefer multiple issues. It would also make it easier for the community to help since the scope of the work would be clear and multiple folks could work on independent parts in parallel.
Feel free to create multiple issues. I'd rather leave one until we actually start working on that. It might appear that the scope of this work is pretty small and we will just do all libraries in 1 commit.
Just want to note that this should include removing dead #ifdef'd regions and making sure that each library has the right set of preprocessor symbols defined.
Would it be possible to update the development documentation with the 'official' set of #if symbols that are allowed, and when they should be used?
The existing preprocessor symbols in corefx with #if declarations are:
- ASSERT_ATTRIBUTE_ORDER
- ASSERT_VERBOSE
- BUFFER_BUILDER_TRACING
- CLS_Compliant
- CONCURRENT_COLLECTIONS
- DEBUG
- DESKTOP
- ECMA
- EqualsStructurally
- FEATURE_CORECLR
- FEATURE_FILEVERSIONINFO
- FEATURE_LEGACYNETCF
- FEATURE_NETCORE
- FEATURE_RANDOMIZED_STRING_HASHING
- FEATURE_TRACESWITCH
- FEATURE_TRACING
- INTERNAL_GLOBALIZATION_EXTENSIONS
- ONLYZEROBOUND
- PFXTRACE
- RunAs
- SILVERLIGHT
- SILVERLIGHT_DISABLE_SECURITY
- SUPPORT_THREAD_ABORT
- USE_OVERLAPPED
- USE_WINRT
- Verbose
- WINCORESYS
- XML10_FIFTH_EDITION
- XMLCHARTYPE_GEN_RESOURCE
- XMLCHARTYPE_USE_RESOURCE
Preprocessor symbols have historically been added as required for large new features (e.g. FEATURE_*, SILVERLIGHT). I'm not sure it would make sense to document an "official" set at this point because some usages of these symbols may be unnecessary, but were left in because removing them was not necessary or they were required in the desktop framework.
FYI I'm preparing a PR to open-source a tool to analyze conditional regions & optionally remove unnecessary ones in the codeformatter repo. I'd like removing unnecessary regions with this tool to be part of the work described by this bug.
In the System.Resources.ResourceWriter.Generate() I noticed a list created (https://github.com/dotnet/corefx/blob/7116584186f8f3a886616aaf8cb5d4a982c60e27/src/System.Resources.ResourceWriter/src/System/Resources/ResourceWriter.cs#L115) that is only used to write the count (always 0) to the output stream later in the code. As far as I understand, in the framework's version of the class it is supposed to capture the names of the user defined resource types. In the CoreFX implementation of the class the writer seems to only support string type of resources, so while the list instance is still used, technically it's dead code. Would it be OK if I removed it? (I am sure I will find and take care of other obvious dead code)
FYI I'd like to contribute, but I do not have much experience with GitHub collaboration workflow. I realized that removing dead code in an isolated class would be a safe and reasonable way to learn and at the same time add a bit of value.
@plabovich we would love for you to contribute. I don't know much about that particular code you point out but it does certainly look like some dead code, so feel free to submit a PR for cleaning it up. If nothing else it may point out something we missed and the owner @ramarag can help figure out if that is in fact dead code or if we are missing some implementation.
Yes the list mentioned above was an artifact of the port from the Resourcewriter which allowed arbitrary types to be written to the resource stream. We can delete the code and just write a zero to the correct position in the resource stream
reopen this issue
Yes the closing of this was github trying to be smart about closing issues when the associated PR is closed :)
@weshaggard just stumbled over this, any reason this is still closed?
@akoeplinger no reason, beyond me not hitting the reopen button months ago :)
What is the minimum FxCop ruleset to check for dead code? Is the following set quite enough?
CA1804: Remove unused locals CA1811: Avoid uncalled private code CA1812: Avoid uninstantiated internal classes CA1821: Remove empty finalizers CA1823: Avoid unused private fields CS0162: Unreachable code detected
@alexanderkozlenko - if you are still interested in contributing here (sorry reply is absurdly late here) that seems like a good start. @alexperovich can check your list.
That list looks right. Just need to add the rules to CodeAnalysis.ruleset and fix the errors.
Actually, it looks like we will need to add new analyzer assemblies for these rules. I will add these to dotnet/corefx#14095 to track.