Skip to content

Commit 921c627

Browse files
committed
Enable null-state analysis
Annotate nullable reference types to resolve warnings. Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
1 parent ab5ebd0 commit 921c627

File tree

6 files changed

+22
-26
lines changed

6 files changed

+22
-26
lines changed

Directory.Build.props

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
https://github.com/dotnet/roslyn/issues/37995#issuecomment-531549082),
3333
and/or using the Nullable NuGet package.
3434
-->
35-
<Nullable>disable</Nullable>
35+
<Nullable>enable</Nullable>
3636

3737
<!--
3838
Include full paths in compiler output for Omnisharp.

KevinLocke.DataSetChecker.UnitTests/XPathFinderTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static class XPathFinderTests
1515
public static void FindXPath_ThrowsOnNull()
1616
{
1717
XPathFinder finder = new();
18-
Assert.Throws<ArgumentNullException>(() => finder.FindXPath(null));
18+
Assert.Throws<ArgumentNullException>(() => finder.FindXPath(null!));
1919
}
2020

2121
[Fact]

KevinLocke.DataSetChecker/DataSetChecker.cs

+12-12
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class DataSetChecker : IDisposable
5252
new(@"^[\p{L}\p{Nd}@#$_]+$", RegexOptions.Compiled | RegexOptions.CultureInvariant);
5353

5454
private readonly SqlConnection sqlConnectionFmtOnly;
55-
private readonly SqlConnection sqlConnectionSpDescribe;
55+
private readonly SqlConnection? sqlConnectionSpDescribe;
5656
private bool disposedValue;
5757

5858
/// <summary>
@@ -99,7 +99,7 @@ public DataSetChecker(string connectionString)
9999
}
100100
}
101101

102-
public event EventHandler<DataSetCheckerEventArgs> DataSetCheckerEventHandler;
102+
public event EventHandler<DataSetCheckerEventArgs>? DataSetCheckerEventHandler;
103103

104104
/// <summary>
105105
/// Entry-point to check a named XSD against a given collection.
@@ -136,7 +136,7 @@ public static int Main(string[] args)
136136
}
137137

138138
int exitCode = 0;
139-
using (DataSetChecker checker = new(options.ConnectionString))
139+
using (DataSetChecker checker = new(options.ConnectionString!))
140140
{
141141
checker.DataSetCheckerEventHandler += (object sender, DataSetCheckerEventArgs eventArgs) =>
142142
{
@@ -247,8 +247,8 @@ protected void CheckDbCommand(XmlNode dbCommand)
247247
throw new ArgumentNullException(nameof(dbCommand));
248248
}
249249

250-
string commandText = null;
251-
List<SqlParameter> sqlParameters = null;
250+
string? commandText = null;
251+
List<SqlParameter>? sqlParameters = null;
252252
foreach (XmlNode childNode in dbCommand.ChildNodes)
253253
{
254254
if (childNode.NamespaceURI != DataSetConstants.MsDsNamespace)
@@ -391,7 +391,7 @@ protected void CheckDbCommand(XmlNode dbCommand)
391391
protected void CheckCommand(
392392
string commandText,
393393
CommandType commandType,
394-
ICollection<SqlParameter> sqlParameters)
394+
ICollection<SqlParameter>? sqlParameters)
395395
{
396396
if (this.sqlConnectionSpDescribe != null)
397397
{
@@ -400,7 +400,7 @@ protected void CheckCommand(
400400

401401
if (this.sqlConnectionFmtOnly != null)
402402
{
403-
SqlParameter[] sqlParametersArray = null;
403+
SqlParameter[]? sqlParametersArray = null;
404404
if (sqlParameters != null)
405405
{
406406
sqlParametersArray = new SqlParameter[sqlParameters.Count];
@@ -418,7 +418,7 @@ protected void CheckCommand(
418418
protected void CheckCommandFormatOnly(
419419
string commandText,
420420
CommandType commandType,
421-
SqlParameter[] sqlParameters)
421+
SqlParameter[]? sqlParameters)
422422
{
423423
using SqlCommand sqlCommand = new(commandText, this.sqlConnectionFmtOnly);
424424
sqlCommand.CommandType = commandType;
@@ -432,7 +432,7 @@ protected void CheckCommandFormatOnly(
432432

433433
protected void CheckCommandSPDescribe(
434434
string commandText,
435-
IEnumerable<SqlParameter> sqlParameters)
435+
IEnumerable<SqlParameter>? sqlParameters)
436436
{
437437
string paramsDecl = this.GetSqlDeclaration(sqlParameters);
438438
using SqlCommand sqlCommand = new("sp_describe_first_result_set", this.sqlConnectionSpDescribe);
@@ -572,7 +572,7 @@ private static XmlNamespaceManager CreateNamespaceManager()
572572
}
573573

574574
// FIXME: Is this really not implemented somewhere in ADO.NET?
575-
private string GetSqlDeclaration(IEnumerable<SqlParameter> parameters)
575+
private string GetSqlDeclaration(IEnumerable<SqlParameter>? parameters)
576576
{
577577
if (parameters == null)
578578
{
@@ -612,7 +612,7 @@ private string GetSqlDeclaration(IEnumerable<SqlParameter> parameters)
612612
return declaration.ToString(0, declaration.Length - 2);
613613
}
614614

615-
private void LogError(string message, XmlNode node, Exception exception = null)
615+
private void LogError(string message, XmlNode? node, Exception? exception = null)
616616
{
617617
// FIXME: Throw if no EventHandler?
618618
DataSetCheckerEventArgs eventArgs = new(
@@ -623,7 +623,7 @@ private void LogError(string message, XmlNode node, Exception exception = null)
623623
this.OnDataSetCheckerEventHandler(eventArgs);
624624
}
625625

626-
private void LogWarning(string message, XmlNode node, Exception exception = null)
626+
private void LogWarning(string message, XmlNode? node, Exception? exception = null)
627627
{
628628
// FIXME: Throw if no EventHandler?
629629
DataSetCheckerEventArgs eventArgs = new(

KevinLocke.DataSetChecker/DataSetCheckerEventArgs.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ public class DataSetCheckerEventArgs : EventArgs
1515
{
1616
private static readonly DataSetXPathFinder XPathFinder = new();
1717

18-
internal DataSetCheckerEventArgs(XmlSeverityType severity, string message, XmlNode node, Exception exception)
18+
internal DataSetCheckerEventArgs(XmlSeverityType severity, string message, XmlNode? node, Exception? exception)
1919
{
2020
this.Message = message ?? throw new ArgumentNullException(nameof(message));
2121
this.Severity = severity;
2222
this.Node = node;
2323
this.Exception = exception;
2424
}
2525

26-
public Exception Exception { get; }
26+
public Exception? Exception { get; }
2727

2828
public string Message { get; }
2929

30-
public XmlNode Node { get; }
30+
public XmlNode? Node { get; }
3131

3232
public XmlSeverityType Severity { get; }
3333

KevinLocke.DataSetChecker/DataSetCheckerSettings.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace KevinLocke.DataSetChecker
1919
/// </remarks>
2020
public class DataSetCheckerSettings
2121
{
22-
public string ConnectionString { get; set; }
22+
public string? ConnectionString { get; set; }
2323

2424
public bool NoWarnings { get; set; }
2525
}

KevinLocke.DataSetChecker/XPathFinder.cs

+4-8
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,14 @@ public string FindXPath(XmlNode node)
3636
/// Finds an XPath expression to a given <see cref="XmlNode"/> from a
3737
/// given context.
3838
/// </summary>
39-
/// <param name="node">Node which the returned XPath will select.</param>
39+
/// <param name="target">Node which the returned XPath will select.</param>
4040
/// <param name="context">Node from which the XPath will be run.
4141
/// If <c>null</c>, an absolute XPath is returned.</param>
42-
/// <returns>An XPath expression which selects <paramref name="node"/>
42+
/// <returns>An XPath expression which selects <paramref name="target"/>
4343
/// when run from <paramref name="context"/>.</returns>
44-
public virtual string FindXPath(XmlNode node, XmlNode context)
44+
public virtual string FindXPath(XmlNode target, XmlNode? context)
4545
{
46-
if (node == null)
47-
{
48-
throw new ArgumentNullException(nameof(node));
49-
}
50-
46+
XmlNode? node = target ?? throw new ArgumentNullException(nameof(target));
5147
List<string> steps = [];
5248
while (node != null && node != context)
5349
{

0 commit comments

Comments
 (0)