Skip to content

Commit 6a81dd5

Browse files
authored
Fix all build warnings (castleproject#723)
* Fix nullable reference type warnings * Fix CA2022 ("avoid inexact read") in `ModuleScope` * Fix `ref`/`in` warning due to changed method signature on .NET 9+ * Make exception types serializable only for the .NET Framework * Suppress two instances of SYSLIB0050 ("Formatter-based serialization is obsolete") * Update `ref/` contract files
1 parent bf0297a commit 6a81dd5

11 files changed

Lines changed: 38 additions & 24 deletions

File tree

ref/Castle.Core-net8.0.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2131,12 +2131,10 @@ public void WarnFormat(System.Exception exception, string format, params object[
21312131
public void WarnFormat(System.IFormatProvider formatProvider, string format, params object[] args) { }
21322132
public void WarnFormat(System.Exception exception, System.IFormatProvider formatProvider, string format, params object[] args) { }
21332133
}
2134-
[System.Serializable]
21352134
public class LoggerException : System.Exception
21362135
{
21372136
public LoggerException() { }
21382137
public LoggerException(string message) { }
2139-
protected LoggerException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { }
21402138
public LoggerException(string message, System.Exception innerException) { }
21412139
}
21422140
public enum LoggerLevel
@@ -2331,12 +2329,10 @@ public interface IResourceFactory
23312329
Castle.Core.Resource.IResource Create(Castle.Core.Resource.CustomUri uri);
23322330
Castle.Core.Resource.IResource Create(Castle.Core.Resource.CustomUri uri, string basePath);
23332331
}
2334-
[System.Serializable]
23352332
public class ResourceException : System.Exception
23362333
{
23372334
public ResourceException() { }
23382335
public ResourceException(string message) { }
2339-
protected ResourceException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { }
23402336
public ResourceException(string message, System.Exception innerException) { }
23412337
}
23422338
public class StaticContentResource : Castle.Core.Resource.AbstractResource
@@ -2449,7 +2445,6 @@ public System.Type CreateInterfaceProxyTypeWithTarget(System.Type interfaceToPro
24492445
public System.Type CreateInterfaceProxyTypeWithTargetInterface(System.Type interfaceToProxy, System.Type[]? additionalInterfacesToProxy, Castle.DynamicProxy.ProxyGenerationOptions options) { }
24502446
public System.Type CreateInterfaceProxyTypeWithoutTarget(System.Type interfaceToProxy, System.Type[]? additionalInterfacesToProxy, Castle.DynamicProxy.ProxyGenerationOptions options) { }
24512447
}
2452-
[System.Serializable]
24532448
public sealed class DynamicProxyException : System.Exception { }
24542449
public interface IChangeProxyTarget
24552450
{

ref/Castle.Core-net9.0.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2131,12 +2131,10 @@ public void WarnFormat(System.Exception exception, string format, params object[
21312131
public void WarnFormat(System.IFormatProvider formatProvider, string format, params object[] args) { }
21322132
public void WarnFormat(System.Exception exception, System.IFormatProvider formatProvider, string format, params object[] args) { }
21332133
}
2134-
[System.Serializable]
21352134
public class LoggerException : System.Exception
21362135
{
21372136
public LoggerException() { }
21382137
public LoggerException(string message) { }
2139-
protected LoggerException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { }
21402138
public LoggerException(string message, System.Exception innerException) { }
21412139
}
21422140
public enum LoggerLevel
@@ -2331,12 +2329,10 @@ public interface IResourceFactory
23312329
Castle.Core.Resource.IResource Create(Castle.Core.Resource.CustomUri uri);
23322330
Castle.Core.Resource.IResource Create(Castle.Core.Resource.CustomUri uri, string basePath);
23332331
}
2334-
[System.Serializable]
23352332
public class ResourceException : System.Exception
23362333
{
23372334
public ResourceException() { }
23382335
public ResourceException(string message) { }
2339-
protected ResourceException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { }
23402336
public ResourceException(string message, System.Exception innerException) { }
23412337
}
23422338
public class StaticContentResource : Castle.Core.Resource.AbstractResource
@@ -2449,7 +2445,6 @@ public System.Type CreateInterfaceProxyTypeWithTarget(System.Type interfaceToPro
24492445
public System.Type CreateInterfaceProxyTypeWithTargetInterface(System.Type interfaceToProxy, System.Type[]? additionalInterfacesToProxy, Castle.DynamicProxy.ProxyGenerationOptions options) { }
24502446
public System.Type CreateInterfaceProxyTypeWithoutTarget(System.Type interfaceToProxy, System.Type[]? additionalInterfacesToProxy, Castle.DynamicProxy.ProxyGenerationOptions options) { }
24512447
}
2452-
[System.Serializable]
24532448
public sealed class DynamicProxyException : System.Exception { }
24542449
public interface IChangeProxyTarget
24552450
{

ref/Castle.Core-netstandard2.0.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2131,12 +2131,10 @@ public void WarnFormat(System.Exception exception, string format, params object[
21312131
public void WarnFormat(System.IFormatProvider formatProvider, string format, params object[] args) { }
21322132
public void WarnFormat(System.Exception exception, System.IFormatProvider formatProvider, string format, params object[] args) { }
21332133
}
2134-
[System.Serializable]
21352134
public class LoggerException : System.Exception
21362135
{
21372136
public LoggerException() { }
21382137
public LoggerException(string message) { }
2139-
protected LoggerException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { }
21402138
public LoggerException(string message, System.Exception innerException) { }
21412139
}
21422140
public enum LoggerLevel
@@ -2331,12 +2329,10 @@ public interface IResourceFactory
23312329
Castle.Core.Resource.IResource Create(Castle.Core.Resource.CustomUri uri);
23322330
Castle.Core.Resource.IResource Create(Castle.Core.Resource.CustomUri uri, string basePath);
23332331
}
2334-
[System.Serializable]
23352332
public class ResourceException : System.Exception
23362333
{
23372334
public ResourceException() { }
23382335
public ResourceException(string message) { }
2339-
protected ResourceException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { }
23402336
public ResourceException(string message, System.Exception innerException) { }
23412337
}
23422338
public class StaticContentResource : Castle.Core.Resource.AbstractResource
@@ -2449,7 +2445,6 @@ public System.Type CreateInterfaceProxyTypeWithTarget(System.Type interfaceToPro
24492445
public System.Type CreateInterfaceProxyTypeWithTargetInterface(System.Type interfaceToProxy, System.Type[]? additionalInterfacesToProxy, Castle.DynamicProxy.ProxyGenerationOptions options) { }
24502446
public System.Type CreateInterfaceProxyTypeWithoutTarget(System.Type interfaceToProxy, System.Type[]? additionalInterfacesToProxy, Castle.DynamicProxy.ProxyGenerationOptions options) { }
24512447
}
2452-
[System.Serializable]
24532448
public sealed class DynamicProxyException : System.Exception { }
24542449
public interface IChangeProxyTarget
24552450
{

src/Castle.Core/Core/Logging/LoggerException.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ namespace Castle.Core.Logging
1717
using System;
1818
using System.Runtime.Serialization;
1919

20+
#if FEATURE_SERIALIZATION
2021
[Serializable]
22+
#endif
2123
public class LoggerException : Exception
2224
{
2325
public LoggerException()
@@ -32,8 +34,10 @@ public LoggerException(string message, Exception innerException) : base(message,
3234
{
3335
}
3436

37+
#if FEATURE_SERIALIZATION
3538
protected LoggerException(SerializationInfo info, StreamingContext context) : base(info, context)
3639
{
3740
}
41+
#endif
3842
}
3943
}

src/Castle.Core/Core/Resource/ResourceException.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ namespace Castle.Core.Resource
1717
using System;
1818
using System.Runtime.Serialization;
1919

20+
#if FEATURE_SERIALIZATION
2021
[Serializable]
22+
#endif
2123
public class ResourceException : Exception
2224
{
2325
public ResourceException()
@@ -32,8 +34,10 @@ public ResourceException(string message, Exception innerException) : base(messag
3234
{
3335
}
3436

37+
#if FEATURE_SERIALIZATION
3538
protected ResourceException(SerializationInfo info, StreamingContext context) : base(info, context)
3639
{
3740
}
41+
#endif
3842
}
3943
}

src/Castle.Core/DynamicProxy/DynamicProxyException.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ namespace Castle.DynamicProxy
2323
// Prefer throwing Base Class Library exception types wherever appropriate.
2424
// This exception type is to be used mostly when something inside DynamicProxy goes wrong.
2525
// Think of it as a "failed assertion" / "bug" exception.
26+
#if FEATURE_SERIALIZATION
2627
[Serializable]
28+
#endif
2729
public sealed class DynamicProxyException : Exception
2830
{
2931
internal DynamicProxyException(string message) : base(message)
@@ -34,8 +36,10 @@ internal DynamicProxyException(string message, Exception innerException) : base(
3436
{
3537
}
3638

39+
#if FEATURE_SERIALIZATION
3740
internal DynamicProxyException(SerializationInfo info, StreamingContext context) : base(info, context)
3841
{
3942
}
43+
#endif
4044
}
4145
}

src/Castle.Core/DynamicProxy/Generators/Emitters/ClassEmitter.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,15 @@ internal sealed class ClassEmitter
4343

4444
private GenericTypeParameterBuilder[] genericTypeParams;
4545

46+
// TODO: reconsider the following warning suppression once we have made a decision
47+
// regarding DynamicProxy's future support for serialization. For now, we tolerate
48+
// the `[Serializable]` attribute on types a little longer, because it is not just
49+
// for `BinaryFormatter`'s sake, but also for e.g. the non-obsolete `DataContract-
50+
// erializer` which recognizes it.
51+
#pragma warning disable SYSLIB0050
4652
internal const TypeAttributes DefaultTypeAttributes =
4753
TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.Serializable;
54+
#pragma warning restore SYSLIB0050
4855

4956
public ClassEmitter(TypeBuilder typeBuilder)
5057
{
@@ -195,7 +202,10 @@ public FieldReference CreateField(string name, Type fieldType, bool serializable
195202

196203
if (!serializable)
197204
{
205+
// TODO: as above, see explanation there (at `DefaultTypeAttributes`'s declaration site)
206+
#pragma warning disable SYSLIB0050
198207
atts |= FieldAttributes.NotSerialized;
208+
#pragma warning restore CS0001
199209
}
200210

201211
return CreateField(name, fieldType, atts);

src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/IfNullExpression.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST
2121

2222
internal class IfNullExpression : IExpression, IStatement
2323
{
24-
private readonly IExpressionOrStatement ifNotNull;
24+
private readonly IExpressionOrStatement? ifNotNull;
2525
private readonly IExpressionOrStatement ifNull;
2626
private readonly IExpression expression;
2727

28-
public IfNullExpression(IExpression expression, IExpressionOrStatement ifNull, IExpressionOrStatement ifNotNull = null)
28+
public IfNullExpression(IExpression expression, IExpressionOrStatement ifNull, IExpressionOrStatement? ifNotNull = null)
2929
{
3030
this.expression = expression ?? throw new ArgumentNullException(nameof(expression));
3131
this.ifNull = ifNull;
@@ -39,10 +39,7 @@ public void Emit(ILGenerator gen)
3939
gen.Emit(OpCodes.Brtrue_S, notNull);
4040
ifNull.Emit(gen);
4141
gen.MarkLabel(notNull);
42-
if (ifNotNull != null) // yeah, I know that reads funny :)
43-
{
44-
ifNotNull.Emit(gen);
45-
}
42+
ifNotNull?.Emit(gen);
4643
}
4744
}
4845
}

src/Castle.Core/DynamicProxy/Internal/TypeUtil.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ public static Type[] GetAllInterfaces(this Type type) // NOTE: also used by Win
138138

139139
internal static bool HasAnyOverridableDefaultImplementations(this Type interfaceType)
140140
{
141-
Debug.Assert(interfaceType != null);
142141
Debug.Assert(interfaceType.IsInterface);
143142

144143
var cache = hasAnyOverridableDefaultImplementationsCache;

src/Castle.Core/DynamicProxy/ModuleScope.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,11 @@ public static byte[] GetKeyPair()
165165

166166
var length = (int)stream.Length;
167167
var keyPair = new byte[length];
168-
stream.Read(keyPair, 0, length);
168+
var bytesRead = stream.Read(keyPair, 0, length);
169+
if (bytesRead != length)
170+
{
171+
throw new IOException("Failed to read entire key pair from embedded resource.");
172+
}
169173
return keyPair;
170174
}
171175
}

0 commit comments

Comments
 (0)