Skip to content

Commit 94f574e

Browse files
Merge pull request #3732 from siegfriedpammer/investigate-warnings
Reduce build warnings: lift IDisposable, fix CS9336 precedence bug, dispose evicted assemblies
2 parents 775ff37 + eec031a commit 94f574e

18 files changed

Lines changed: 99 additions & 25 deletions

File tree

ICSharpCode.Decompiler/CSharp/Syntax/IAnnotatable.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
using System;
2020
using System.Collections.Generic;
21+
using System.Diagnostics.CodeAnalysis;
2122
using System.Linq;
2223
using System.Threading;
2324

@@ -115,6 +116,8 @@ public AnnotationList(int initialCapacity) : base(initialCapacity)
115116
{
116117
}
117118

119+
[SuppressMessage("Reliability", "CA2002:Do not lock on objects with weak identity",
120+
Justification = "AnnotationList is a private nested type — the surrounding Annotatable class deliberately locks on the AnnotationList instance to serialize annotation reads/writes; external code cannot obtain a reference to it.")]
118121
public object Clone()
119122
{
120123
lock (this)

ICSharpCode.Decompiler/CSharp/Syntax/TypeMembers/ExtensionDeclaration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class ExtensionDeclaration : EntityDeclaration
2424
{
2525
public readonly static TokenRole ExtensionKeywordRole = new TokenRole("extension");
2626

27-
public override SymbolKind SymbolKind => throw new System.NotImplementedException();
27+
public override SymbolKind SymbolKind => SymbolKind.TypeDefinition;
2828

2929
public AstNodeCollection<TypeParameterDeclaration> TypeParameters {
3030
get { return GetChildrenByRole(Roles.TypeParameter); }

ICSharpCode.Decompiler/IL/Instructions/CompoundAssignmentInstruction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ internal static bool IsBinaryCompatibleWithType(BinaryNumericInstruction binary,
215215
return false; // operator not supported on pointer types
216216
}
217217
}
218-
else if ((type.IsKnownType(KnownTypeCode.IntPtr) || type.IsKnownType(KnownTypeCode.UIntPtr)) && type.Kind is not TypeKind.NInt or TypeKind.NUInt)
218+
else if ((type.IsKnownType(KnownTypeCode.IntPtr) || type.IsKnownType(KnownTypeCode.UIntPtr)) && type.Kind is not (TypeKind.NInt or TypeKind.NUInt))
219219
{
220220
// If the LHS is C# 9 IntPtr (but not nint or C# 11 IntPtr):
221221
// "target.intptr *= 2;" is compiler error, but

ICSharpCode.Decompiler/Metadata/DotNetCorePathFinder.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ static unsafe string GetRealPath(string path, Encoding encoding)
318318
fixed (byte* input = bytes)
319319
{
320320

321-
byte* output = GetRealPath(input, null);
321+
byte* output = NativeMethods.GetRealPath(input, null);
322322
if (output == null)
323323
{
324324
return null;
@@ -330,15 +330,18 @@ static unsafe string GetRealPath(string path, Encoding encoding)
330330
}
331331
byte[] result = new byte[len];
332332
Marshal.Copy((IntPtr)output, result, 0, result.Length);
333-
Free(output);
333+
NativeMethods.Free(output);
334334
return encoding.GetString(result);
335335
}
336336
}
337337

338-
[DllImport("libc", EntryPoint = "realpath")]
339-
static extern unsafe byte* GetRealPath(byte* path, byte* resolvedPath);
338+
static class NativeMethods
339+
{
340+
[DllImport("libc", EntryPoint = "realpath")]
341+
internal static extern unsafe byte* GetRealPath(byte* path, byte* resolvedPath);
340342

341-
[DllImport("libc", EntryPoint = "free")]
342-
static extern unsafe void Free(void* ptr);
343+
[DllImport("libc", EntryPoint = "free")]
344+
internal static extern unsafe void Free(void* ptr);
345+
}
343346
}
344347
}

ICSharpCode.Decompiler/Metadata/MetadataFile.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using System.Collections.Generic;
2323
using System.Collections.Immutable;
2424
using System.Diagnostics;
25+
using System.Diagnostics.CodeAnalysis;
2526
using System.Linq;
2627
using System.Reflection.Metadata;
2728
using System.Reflection.PortableExecutable;
@@ -45,7 +46,7 @@ namespace ICSharpCode.Decompiler.Metadata
4546
/// decompiled type systems.
4647
/// </remarks>
4748
[DebuggerDisplay("{Kind}: {FileName}")]
48-
public class MetadataFile
49+
public class MetadataFile : IDisposable
4950
{
5051
public enum MetadataFileKind
5152
{
@@ -285,6 +286,8 @@ public virtual int GetContainingSectionIndex(int rva)
285286
throw new BadImageFormatException("This metadata file does not support sections.");
286287
}
287288

289+
[SuppressMessage("Design", "CA1065:Do not raise exceptions in unexpected locations",
290+
Justification = "Throw signals that this MetadataFileKind has no PE sections; derived PE-like kinds override.")]
288291
public virtual ImmutableArray<SectionHeader> SectionHeaders => throw new BadImageFormatException("This metadata file does not support sections.");
289292

290293
/// <summary>
@@ -297,6 +300,16 @@ public IModuleReference WithOptions(TypeSystemOptions options)
297300
return new MetadataFileWithOptions(this, options);
298301
}
299302

303+
public void Dispose()
304+
{
305+
Dispose(disposing: true);
306+
GC.SuppressFinalize(this);
307+
}
308+
309+
protected virtual void Dispose(bool disposing)
310+
{
311+
}
312+
300313
private class MetadataFileWithOptions : IModuleReference
301314
{
302315
readonly MetadataFile peFile;

ICSharpCode.Decompiler/Metadata/PEFile.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
namespace ICSharpCode.Decompiler.Metadata
3232
{
3333
[DebuggerDisplay("{FileName}")]
34-
public class PEFile : MetadataFile, IDisposable, IModuleReference
34+
public sealed class PEFile : MetadataFile, IModuleReference
3535
{
3636
public PEReader Reader { get; }
3737

@@ -55,9 +55,11 @@ public PEFile(string fileName, PEReader reader, MetadataReaderOptions metadataOp
5555
public override int MetadataOffset => Reader.PEHeaders.MetadataStartOffset;
5656
public override bool IsMetadataOnly => false;
5757

58-
public void Dispose()
58+
protected override void Dispose(bool disposing)
5959
{
60-
Reader.Dispose();
60+
if (disposing)
61+
Reader.Dispose();
62+
base.Dispose(disposing);
6163
}
6264

6365
IModule TypeSystem.IModuleReference.Resolve(ITypeResolveContext context)

ICSharpCode.Decompiler/Metadata/WebCilFile.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
namespace ICSharpCode.Decompiler.Metadata
3434
{
35-
public class WebCilFile : MetadataFile, IDisposable, IModuleReference
35+
public sealed class WebCilFile : MetadataFile, IModuleReference
3636
{
3737
readonly MemoryMappedViewAccessor view;
3838
readonly long webcilOffset;
@@ -245,9 +245,11 @@ public override unsafe SectionData GetSectionData(int rva)
245245
return new MetadataModule(context.Compilation, this, TypeSystemOptions.Default);
246246
}
247247

248-
public void Dispose()
248+
protected override void Dispose(bool disposing)
249249
{
250-
view.Dispose();
250+
if (disposing)
251+
view.Dispose();
252+
base.Dispose(disposing);
251253
}
252254

253255
public struct WebcilHeader

ICSharpCode.Decompiler/Output/PlainTextOutput.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@
2828

2929
namespace ICSharpCode.Decompiler
3030
{
31-
public sealed class PlainTextOutput : ITextOutput
31+
public sealed class PlainTextOutput : ITextOutput, IDisposable
3232
{
3333
readonly TextWriter writer;
34+
readonly bool ownsWriter;
3435
int indent;
3536
bool needsIndent;
3637

@@ -44,11 +45,19 @@ public PlainTextOutput(TextWriter writer)
4445
if (writer == null)
4546
throw new ArgumentNullException(nameof(writer));
4647
this.writer = writer;
48+
this.ownsWriter = false;
4749
}
4850

4951
public PlainTextOutput()
5052
{
5153
this.writer = new StringWriter();
54+
this.ownsWriter = true;
55+
}
56+
57+
public void Dispose()
58+
{
59+
if (ownsWriter)
60+
writer.Dispose();
5261
}
5362

5463
public TextLocation Location {

ICSharpCode.Decompiler/TypeSystem/Implementation/CustomAttribute.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ sealed class CustomAttribute : IAttribute
4040
CustomAttributeValue<IType> value;
4141
bool valueDecoded;
4242
bool hasDecodeErrors;
43+
readonly object syncRoot = new object();
4344

4445
internal CustomAttribute(MetadataModule module, IMethod attrCtor, CustomAttributeHandle handle)
4546
{
@@ -76,7 +77,7 @@ public bool HasDecodeErrors {
7677

7778
void DecodeValue()
7879
{
79-
lock (this)
80+
lock (syncRoot)
8081
{
8182
try
8283
{

ICSharpCode.Decompiler/Util/EmptyList.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using System;
2121
using System.Collections;
2222
using System.Collections.Generic;
23+
using System.Diagnostics.CodeAnalysis;
2324

2425
namespace ICSharpCode.Decompiler.Util
2526
{
@@ -99,6 +100,8 @@ object IEnumerator.Current {
99100
get { throw new NotSupportedException(); }
100101
}
101102

103+
[SuppressMessage("Usage", "CA1063:Implement IDisposable Correctly",
104+
Justification = "Explicit IDisposable implementation for IEnumerator<T>; intentional no-op for the singleton.")]
102105
void IDisposable.Dispose()
103106
{
104107
}

0 commit comments

Comments
 (0)