From e73ced2275fa8e1f3690f1941ea02906ef685eb1 Mon Sep 17 00:00:00 2001 From: Tamas Vajk Date: Fri, 2 Oct 2020 11:49:35 +0200 Subject: [PATCH] C#: Add sealed modifier to classes to fix dispose-pattern, remove explicit IDisposable implementations --- csharp/extractor/Semmle.Extraction.CIL/Context.cs | 4 ++-- csharp/extractor/Semmle.Extraction.CIL/Factories.cs | 2 +- .../Semmle.Extraction.CIL/PDB/MetadataPdbReader.cs | 2 +- .../Semmle.Extraction.CIL/PDB/NativePdbReader.cs | 2 +- .../BuildAnalysis.cs | 2 +- .../Semmle.Extraction.CSharp.Standalone/Program.cs | 2 +- csharp/extractor/Semmle.Extraction.CSharp/Analyser.cs | 2 +- csharp/extractor/Semmle.Extraction.Tests/Layout.cs | 2 +- csharp/extractor/Semmle.Extraction.Tests/TrapWriter.cs | 2 +- csharp/extractor/Semmle.Extraction/Context.cs | 2 +- .../extractor/Semmle.Util.Tests/CanonicalPathCache.cs | 7 ++++--- csharp/extractor/Semmle.Util.Tests/LongPaths.cs | 10 +++++----- csharp/extractor/Semmle.Util/Logger.cs | 6 +++--- 13 files changed, 23 insertions(+), 22 deletions(-) diff --git a/csharp/extractor/Semmle.Extraction.CIL/Context.cs b/csharp/extractor/Semmle.Extraction.CIL/Context.cs index 825a05b5ad1..95e59f1544a 100644 --- a/csharp/extractor/Semmle.Extraction.CIL/Context.cs +++ b/csharp/extractor/Semmle.Extraction.CIL/Context.cs @@ -12,7 +12,7 @@ namespace Semmle.Extraction.CIL /// Adds additional context that is specific for CIL extraction. /// One context = one DLL/EXE. /// - partial class Context : IDisposable + sealed partial class Context : IDisposable { readonly FileStream stream; Entities.Assembly? assemblyNull; @@ -58,7 +58,7 @@ namespace Semmle.Extraction.CIL } } - void IDisposable.Dispose() + public void Dispose() { if (pdb != null) pdb.Dispose(); diff --git a/csharp/extractor/Semmle.Extraction.CIL/Factories.cs b/csharp/extractor/Semmle.Extraction.CIL/Factories.cs index c6fb673c940..841afb99a4b 100644 --- a/csharp/extractor/Semmle.Extraction.CIL/Factories.cs +++ b/csharp/extractor/Semmle.Extraction.CIL/Factories.cs @@ -9,7 +9,7 @@ namespace Semmle.Extraction.CIL /// /// Provides methods for creating and caching various entities. /// - public partial class Context + public sealed partial class Context { readonly Dictionary ids = new Dictionary(); diff --git a/csharp/extractor/Semmle.Extraction.CIL/PDB/MetadataPdbReader.cs b/csharp/extractor/Semmle.Extraction.CIL/PDB/MetadataPdbReader.cs index ab929af0499..4901d9f54cb 100644 --- a/csharp/extractor/Semmle.Extraction.CIL/PDB/MetadataPdbReader.cs +++ b/csharp/extractor/Semmle.Extraction.CIL/PDB/MetadataPdbReader.cs @@ -13,7 +13,7 @@ namespace Semmle.Extraction.PDB /// /// PDB information can be in a separate PDB file, or embedded in the DLL. /// - class MetadataPdbReader : IPdb + sealed class MetadataPdbReader : IPdb { class SourceFile : ISourceFile { diff --git a/csharp/extractor/Semmle.Extraction.CIL/PDB/NativePdbReader.cs b/csharp/extractor/Semmle.Extraction.CIL/PDB/NativePdbReader.cs index 052b4f69c8d..f6cca049ed6 100644 --- a/csharp/extractor/Semmle.Extraction.CIL/PDB/NativePdbReader.cs +++ b/csharp/extractor/Semmle.Extraction.CIL/PDB/NativePdbReader.cs @@ -14,7 +14,7 @@ namespace Semmle.Extraction.PDB /// A PDB reader using Microsoft.DiaSymReader.Native. /// This is an unmanaged Windows DLL, which therefore only works on Windows. /// - class NativePdbReader : IPdb + sealed class NativePdbReader : IPdb { sealed class Document : ISourceFile { diff --git a/csharp/extractor/Semmle.Extraction.CSharp.Standalone/BuildAnalysis.cs b/csharp/extractor/Semmle.Extraction.CSharp.Standalone/BuildAnalysis.cs index 3f0a46f7969..dc0c4145dec 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp.Standalone/BuildAnalysis.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp.Standalone/BuildAnalysis.cs @@ -46,7 +46,7 @@ namespace Semmle.BuildAnalyser /// /// Main implementation of the build analysis. /// - class BuildAnalysis : IBuildAnalysis, IDisposable + sealed class BuildAnalysis : IBuildAnalysis, IDisposable { private readonly AssemblyCache assemblyCache; private readonly IProgressMonitor progressMonitor; diff --git a/csharp/extractor/Semmle.Extraction.CSharp.Standalone/Program.cs b/csharp/extractor/Semmle.Extraction.CSharp.Standalone/Program.cs index 0dc72f74237..ea03768f856 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp.Standalone/Program.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp.Standalone/Program.cs @@ -22,7 +22,7 @@ namespace Semmle.Extraction.CSharp.Standalone /// /// Searches for source/references and creates separate extractions. /// - class Analysis : IDisposable + sealed class Analysis : IDisposable { public Analysis(ILogger logger, Options options) { diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Analyser.cs b/csharp/extractor/Semmle.Extraction.CSharp/Analyser.cs index 6b5b2fac317..e9df51880ae 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Analyser.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Analyser.cs @@ -15,7 +15,7 @@ namespace Semmle.Extraction.CSharp /// /// Encapsulates a C# analysis task. /// - public class Analyser : IDisposable + public sealed class Analyser : IDisposable { IExtractor extractor; diff --git a/csharp/extractor/Semmle.Extraction.Tests/Layout.cs b/csharp/extractor/Semmle.Extraction.Tests/Layout.cs index e7f06b02307..26890460fdf 100644 --- a/csharp/extractor/Semmle.Extraction.Tests/Layout.cs +++ b/csharp/extractor/Semmle.Extraction.Tests/Layout.cs @@ -199,7 +199,7 @@ namespace Semmle.Extraction.Tests new Semmle.Extraction.Layout(null, null, "layout.txt")); } - class LoggerMock : ILogger + sealed class LoggerMock : ILogger { public void Dispose() { } diff --git a/csharp/extractor/Semmle.Extraction.Tests/TrapWriter.cs b/csharp/extractor/Semmle.Extraction.Tests/TrapWriter.cs index a1d45c2bdc2..aff2d6a6b45 100644 --- a/csharp/extractor/Semmle.Extraction.Tests/TrapWriter.cs +++ b/csharp/extractor/Semmle.Extraction.Tests/TrapWriter.cs @@ -42,7 +42,7 @@ namespace Semmle.Extraction.Tests Assert.Equal(@"C:\Temp\source_archive\diskstation\share\source\def.cs", TrapWriter.NestPaths(logger, @"C:\Temp\source_archive", $@"{root3}{root3}diskstation\share\source\def.cs").Replace('/', '\\')); } - class LoggerMock : ILogger + sealed class LoggerMock : ILogger { public void Dispose() { } diff --git a/csharp/extractor/Semmle.Extraction/Context.cs b/csharp/extractor/Semmle.Extraction/Context.cs index 8e11b937c18..d075e18c93e 100644 --- a/csharp/extractor/Semmle.Extraction/Context.cs +++ b/csharp/extractor/Semmle.Extraction/Context.cs @@ -283,7 +283,7 @@ namespace Semmle.Extraction public IDisposable StackGuard => new ScopeGuard(this); - private class ScopeGuard : IDisposable + private sealed class ScopeGuard : IDisposable { readonly Context cx; diff --git a/csharp/extractor/Semmle.Util.Tests/CanonicalPathCache.cs b/csharp/extractor/Semmle.Util.Tests/CanonicalPathCache.cs index 1c1d1e9d627..97a6d885ae6 100644 --- a/csharp/extractor/Semmle.Util.Tests/CanonicalPathCache.cs +++ b/csharp/extractor/Semmle.Util.Tests/CanonicalPathCache.cs @@ -6,7 +6,7 @@ using System; namespace SemmleTests.Semmle.Util { - public class CanonicalPathCacheTest : IDisposable + public sealed class CanonicalPathCacheTest : IDisposable { readonly ILogger Logger = new LoggerMock(); readonly string root; @@ -24,9 +24,10 @@ namespace SemmleTests.Semmle.Util root = Win32.IsWindows() ? @"X:\" : "/"; } - void IDisposable.Dispose() + public void Dispose() { File.Delete("abc"); + Logger.Dispose(); } [Fact] @@ -172,7 +173,7 @@ namespace SemmleTests.Semmle.Util CanonicalPathDots(); } - class LoggerMock : ILogger + sealed class LoggerMock : ILogger { public void Dispose() { } diff --git a/csharp/extractor/Semmle.Util.Tests/LongPaths.cs b/csharp/extractor/Semmle.Util.Tests/LongPaths.cs index 9a03fbc2013..d20d3a28b54 100644 --- a/csharp/extractor/Semmle.Util.Tests/LongPaths.cs +++ b/csharp/extractor/Semmle.Util.Tests/LongPaths.cs @@ -10,20 +10,20 @@ namespace SemmleTests.Semmle.Util /// Ensure that the Extractor works with long paths. /// These should be handled by .NET Core. /// - public class LongPaths : IDisposable + public sealed class LongPaths : IDisposable { static readonly string tmpDir = Path.GetTempPath(); static readonly string shortPath = Path.Combine(tmpDir, "test.txt"); static readonly string longPath = Path.Combine(tmpDir, "aaaaaaaaaaaaaaaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbb", "ccccccccccccccccccccccccccccccc", "ddddddddddddddddddddddddddddddddddddd", "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", "fffffffffffffffffffffffffffffffff", - "ggggggggggggggggggggggggggggggggggg","hhhhhhhhhhhhhhhhhhhhhhhhhhhhhh","iiiiiiiiiiiiiiii.txt"); + "ggggggggggggggggggggggggggggggggggg", "hhhhhhhhhhhhhhhhhhhhhhhhhhhhhh", "iiiiiiiiiiiiiiii.txt"); public LongPaths() { CleanUp(); } - void IDisposable.Dispose() + public void Dispose() { CleanUp(); } @@ -34,14 +34,14 @@ namespace SemmleTests.Semmle.Util { File.Delete(shortPath); } - catch(DirectoryNotFoundException) + catch (DirectoryNotFoundException) { } try { File.Delete(longPath); } - catch(DirectoryNotFoundException) + catch (DirectoryNotFoundException) { } } diff --git a/csharp/extractor/Semmle.Util/Logger.cs b/csharp/extractor/Semmle.Util/Logger.cs index 13877448093..5ef5c8fd039 100644 --- a/csharp/extractor/Semmle.Util/Logger.cs +++ b/csharp/extractor/Semmle.Util/Logger.cs @@ -55,7 +55,7 @@ namespace Semmle.Util.Logging /// A logger that outputs to a csharp.log /// file. /// - public class FileLogger : ILogger + public sealed class FileLogger : ILogger { readonly StreamWriter writer; readonly Verbosity verbosity; @@ -100,7 +100,7 @@ namespace Semmle.Util.Logging /// /// A logger that outputs to stdout/stderr. /// - public class ConsoleLogger : ILogger + public sealed class ConsoleLogger : ILogger { readonly Verbosity verbosity; @@ -143,7 +143,7 @@ namespace Semmle.Util.Logging /// /// A combined logger. /// - public class CombinedLogger : ILogger + public sealed class CombinedLogger : ILogger { readonly ILogger logger1; readonly ILogger logger2;