Skip to content

Commit f2dd3f7

Browse files
committed
Convert codebase to use C# 8 nullability
Thanks to @Brar for considerable work on the feature! Closes #2304
1 parent d922b0a commit f2dd3f7

File tree

194 files changed

+1942
-1810
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

194 files changed

+1942
-1810
lines changed

.appveyor.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ before_build:
2929
build_script:
3030
- dotnet build "test\Npgsql.Tests" -c Debug
3131
- dotnet build "test\Npgsql.PluginTests" -c Debug
32+
- dotnet build "src\Npgsql" -c Release
3233
- msbuild src\VSIX\VSIX.csproj /p:Configuration=Release /v:Minimal
3334
# WIX hasn't been released yet for VS 2019
3435
# - msbuild src\MSI\MSI.wixproj /p:Configuration=Release /v:Minimal

Directory.Build.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<PropertyGroup>
1919
<LangVersion>8.0</LangVersion>
2020
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
21+
<NullableContextOptions>enable</NullableContextOptions>
2122
</PropertyGroup>
2223

2324
<!-- Siging configuration -->

Npgsql.sln.DotSettings

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=ArgumentsStyleStringLiteral/@EntryIndexedValue">DO_NOT_SHOW</s:String>
77
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=MemberCanBePrivate_002EGlobal/@EntryIndexedValue">DO_NOT_SHOW</s:String>
88
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=MemberCanBeProtected_002EGlobal/@EntryIndexedValue">DO_NOT_SHOW</s:String>
9-
<s:String x:Key="/Default/CodeInspection/Highlighting/ValueAnalysisMode/@EntryValue">IMPLICIT_NOTNULL</s:String>
10-
<s:Boolean x:Key="/Default/CodeInspection/ImplicitNullability/Enabled/@EntryValue">True</s:Boolean>
119
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpCodeStyle/ARGUMENTS_LITERAL/@EntryValue">Positional</s:String>
1210
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpCodeStyle/ARGUMENTS_NAMED/@EntryValue">Positional</s:String>
1311
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpCodeStyle/DEFAULT_INTERNAL_MODIFIER/@EntryValue">Implicit</s:String>

src/Npgsql.GeoJSON/CrsMap.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ readonly struct CrsMapEntry
99
{
1010
internal readonly int MinSrid;
1111
internal readonly int MaxSrid;
12-
internal readonly string Authority;
12+
internal readonly string? Authority;
1313

14-
internal CrsMapEntry(int minSrid, int maxSrid, string authority)
14+
internal CrsMapEntry(int minSrid, int maxSrid, string? authority)
1515
{
1616
MinSrid = minSrid;
1717
MaxSrid = maxSrid;
@@ -74,15 +74,15 @@ internal CrsMap Build()
7474

7575
readonly partial struct CrsMap
7676
{
77-
readonly CrsMapEntry[] _overriden;
77+
readonly CrsMapEntry[]? _overriden;
7878

79-
internal CrsMap(CrsMapEntry[] overriden)
79+
internal CrsMap(CrsMapEntry[]? overriden)
8080
=> _overriden = overriden;
8181

82-
internal string GetAuthority(int srid)
82+
internal string? GetAuthority(int srid)
8383
=> GetAuthority(_overriden, srid) ?? GetAuthority(WellKnown, srid);
8484

85-
static string GetAuthority(CrsMapEntry[] entries, int srid)
85+
static string? GetAuthority(CrsMapEntry[]? entries, int srid)
8686
{
8787
if (entries == null)
8888
return null;

src/Npgsql.GeoJSON/GeoJSONHandler.cs

Lines changed: 38 additions & 36 deletions
Large diffs are not rendered by default.

src/Npgsql.Json.NET/JsonHandler.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ public class JsonHandlerFactory : NpgsqlTypeHandlerFactory<string>
1212
{
1313
readonly JsonSerializerSettings _settings;
1414

15-
public JsonHandlerFactory(JsonSerializerSettings settings) => _settings = settings;
15+
public JsonHandlerFactory(JsonSerializerSettings? settings = null)
16+
=> _settings = settings ?? new JsonSerializerSettings();
1617

1718
protected override NpgsqlTypeHandler<string> Create(NpgsqlConnection conn)
1819
=> new JsonHandler(conn, _settings);
@@ -24,7 +25,7 @@ class JsonHandler : Npgsql.TypeHandlers.TextHandler
2425

2526
public JsonHandler(NpgsqlConnection connection, JsonSerializerSettings settings) : base(connection) => _settings = settings;
2627

27-
protected override async ValueTask<T> Read<T>(NpgsqlReadBuffer buf, int len, bool async, FieldDescription fieldDescription = null)
28+
protected override async ValueTask<T> Read<T>(NpgsqlReadBuffer buf, int len, bool async, FieldDescription? fieldDescription = null)
2829
{
2930
var s = await base.Read<string>(buf, len, async, fieldDescription);
3031
if (typeof(T) == typeof(string))
@@ -39,17 +40,17 @@ protected override async ValueTask<T> Read<T>(NpgsqlReadBuffer buf, int len, boo
3940
}
4041
}
4142

42-
protected override int ValidateAndGetLength<T2>(T2 value, ref NpgsqlLengthCache lengthCache, NpgsqlParameter parameter)
43+
protected override int ValidateAndGetLength<T2>(T2 value, ref NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter)
4344
=> typeof(T2) == typeof(string)
4445
? base.ValidateAndGetLength(value, ref lengthCache, parameter)
45-
: ValidateObjectAndGetLength(value, ref lengthCache, parameter);
46-
47-
protected override Task WriteWithLength<T2>(T2 value, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, NpgsqlParameter parameter, bool async)
46+
: ValidateObjectAndGetLength(value!, ref lengthCache, parameter);
47+
48+
protected override Task WriteWithLength<T2>(T2 value, NpgsqlWriteBuffer buf, NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter, bool async)
4849
=> typeof(T2) == typeof(string)
4950
? base.WriteWithLength(value, buf, lengthCache, parameter, async)
50-
: WriteObjectWithLength(value, buf, lengthCache, parameter, async);
51+
: WriteObjectWithLength(value!, buf, lengthCache, parameter, async);
5152

52-
protected override int ValidateObjectAndGetLength(object value, ref NpgsqlLengthCache lengthCache, NpgsqlParameter parameter)
53+
protected override int ValidateObjectAndGetLength(object value, ref NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter)
5354
{
5455
var s = value as string;
5556
if (s == null)
@@ -61,10 +62,10 @@ protected override int ValidateObjectAndGetLength(object value, ref NpgsqlLength
6162
return base.ValidateAndGetLength(s, ref lengthCache, parameter);
6263
}
6364

64-
protected override Task WriteObjectWithLength(object value, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, NpgsqlParameter parameter, bool async)
65+
protected override Task WriteObjectWithLength(object value, NpgsqlWriteBuffer buf, NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter, bool async)
6566
{
6667
if (value == null || value is DBNull)
67-
return base.WriteObjectWithLength(value, buf, lengthCache, parameter, async);
68+
return base.WriteObjectWithLength(DBNull.Value, buf, lengthCache, parameter, async);
6869

6970
if (parameter?.ConvertedValue != null)
7071
value = parameter.ConvertedValue;

src/Npgsql.Json.NET/JsonbHandler.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using System.Threading.Tasks;
34
using Newtonsoft.Json;
45
using Npgsql.BackendMessages;
@@ -12,7 +13,8 @@ public class JsonbHandlerFactory : NpgsqlTypeHandlerFactory<string>
1213
{
1314
readonly JsonSerializerSettings _settings;
1415

15-
public JsonbHandlerFactory(JsonSerializerSettings settings) => _settings = settings;
16+
public JsonbHandlerFactory(JsonSerializerSettings? settings = null)
17+
=> _settings = settings ?? new JsonSerializerSettings();
1618

1719
protected override NpgsqlTypeHandler<string> Create(NpgsqlConnection conn)
1820
=> new JsonbHandler(conn, _settings);
@@ -24,7 +26,7 @@ class JsonbHandler : Npgsql.TypeHandlers.JsonbHandler
2426

2527
public JsonbHandler(NpgsqlConnection connection, JsonSerializerSettings settings) : base(connection) => _settings = settings;
2628

27-
protected override async ValueTask<T> Read<T>(NpgsqlReadBuffer buf, int len, bool async, FieldDescription fieldDescription = null)
29+
protected override async ValueTask<T> Read<T>(NpgsqlReadBuffer buf, int len, bool async, FieldDescription? fieldDescription = null)
2830
{
2931
var s = await base.Read<string>(buf, len, async, fieldDescription);
3032
if (typeof(T) == typeof(string))
@@ -39,17 +41,17 @@ protected override async ValueTask<T> Read<T>(NpgsqlReadBuffer buf, int len, boo
3941
}
4042
}
4143

42-
protected override int ValidateAndGetLength<T2>(T2 value, ref NpgsqlLengthCache lengthCache, NpgsqlParameter parameter)
44+
protected override int ValidateAndGetLength<T2>(T2 value, ref NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter)
4345
=> typeof(T2) == typeof(string)
4446
? base.ValidateAndGetLength(value, ref lengthCache, parameter)
45-
: ValidateObjectAndGetLength(value, ref lengthCache, parameter);
46-
47-
protected override Task WriteWithLength<T2>(T2 value, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, NpgsqlParameter parameter, bool async)
47+
: ValidateObjectAndGetLength(value!, ref lengthCache, parameter);
48+
49+
protected override Task WriteWithLength<T2>(T2 value, NpgsqlWriteBuffer buf, NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter, bool async)
4850
=> typeof(T2) == typeof(string)
4951
? base.WriteWithLength(value, buf, lengthCache, parameter, async)
50-
: WriteObjectWithLength(value, buf, lengthCache, parameter, async);
52+
: WriteObjectWithLength(value!, buf, lengthCache, parameter, async);
5153

52-
protected override int ValidateObjectAndGetLength(object value, ref NpgsqlLengthCache lengthCache, NpgsqlParameter parameter)
54+
protected override int ValidateObjectAndGetLength(object value, ref NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter)
5355
{
5456
var s = value as string;
5557
if (s == null)
@@ -61,10 +63,10 @@ protected override int ValidateObjectAndGetLength(object value, ref NpgsqlLength
6163
return base.ValidateObjectAndGetLength(s, ref lengthCache, parameter);
6264
}
6365

64-
protected override Task WriteObjectWithLength(object value, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, NpgsqlParameter parameter, bool async)
66+
protected override Task WriteObjectWithLength(object value, NpgsqlWriteBuffer buf, NpgsqlLengthCache? lengthCache, NpgsqlParameter? parameter, bool async)
6567
{
6668
if (value == null || value is DBNull)
67-
return base.WriteObjectWithLength(value, buf, lengthCache, parameter, async);
69+
return base.WriteObjectWithLength(DBNull.Value, buf, lengthCache, parameter, async);
6870

6971
if (parameter?.ConvertedValue != null)
7072
value = parameter.ConvertedValue;

src/Npgsql.Json.NET/NpgsqlJsonNetExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ public static class NpgsqlJsonNetExtensions
2121
/// <param name="settings">Optional settings to customize JSON serialization</param>
2222
public static INpgsqlTypeMapper UseJsonNet(
2323
this INpgsqlTypeMapper mapper,
24-
Type[] jsonbClrTypes = null,
25-
Type[] jsonClrTypes = null,
26-
JsonSerializerSettings settings = null
24+
Type[]? jsonbClrTypes = null,
25+
Type[]? jsonClrTypes = null,
26+
JsonSerializerSettings? settings = null
2727
)
2828
{
2929
mapper.AddMapping(new NpgsqlTypeMappingBuilder

src/Npgsql.LegacyPostgis/CodeAnnotations.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ internal enum ImplicitUseTargetFlags
164164
sealed class PublicAPIAttribute : Attribute
165165
#pragma warning restore CA1018
166166
{
167-
public PublicAPIAttribute() { }
167+
public PublicAPIAttribute() : this("") { }
168168
public PublicAPIAttribute([NotNull] string comment)
169169
{
170170
Comment = comment;

0 commit comments

Comments
 (0)