Skip to content

Commit 8a968da

Browse files
committed
C#: Enable nullability in Semmle.Util
1 parent 3767794 commit 8a968da

13 files changed

+31
-32
lines changed

csharp/extractor/Semmle.Util/ActionMap.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,19 @@ namespace Semmle.Util
99
/// </summary>
1010
/// <typeparam name="Key"></typeparam>
1111
/// <typeparam name="Value"></typeparam>
12-
public class ActionMap<Key, Value>
12+
public class ActionMap<Key, Value> where Key : notnull
1313
{
1414
public void Add(Key key, Value value)
1515
{
16-
Action<Value> a;
17-
if (actions.TryGetValue(key, out a))
16+
17+
if (actions.TryGetValue(key, out var a))
1818
a(value);
1919
values[key] = value;
2020
}
2121

2222
public void OnAdd(Key key, Action<Value> action)
2323
{
24-
Action<Value> a;
25-
if (actions.TryGetValue(key, out a))
24+
if (actions.TryGetValue(key, out var a))
2625
{
2726
actions[key] = a + action;
2827
}

csharp/extractor/Semmle.Util/CanonicalPathCache.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ public override string GetCanonicalPath(string path, IPathCache cache)
127127

128128
if (parent != null)
129129
{
130-
string name = Path.GetFileName(path);
131-
string parentPath = cache.GetCanonicalPath(parent.FullName);
130+
string? name = Path.GetFileName(path);
131+
string? parentPath = cache.GetCanonicalPath(parent.FullName);
132132
try
133133
{
134134
string[] entries = Directory.GetFileSystemEntries(parentPath, name);
@@ -313,14 +313,15 @@ void AddToCache(string path, string canonical)
313313
/// <returns>The canonical path.</returns>
314314
public string GetCanonicalPath(string path)
315315
{
316-
string canonicalPath;
317316
lock (cache)
318-
if (!cache.TryGetValue(path, out canonicalPath))
317+
{
318+
if (!cache.TryGetValue(path, out var canonicalPath))
319319
{
320320
canonicalPath = pathStrategy.GetCanonicalPath(path, this);
321321
AddToCache(path, canonicalPath);
322322
}
323-
return canonicalPath;
323+
return canonicalPath;
324+
}
324325
}
325326
}
326327
}

csharp/extractor/Semmle.Util/CommandLineExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static bool WriteCommandLine(this IEnumerable<string> commandLineArgument
1818
var found = false;
1919
foreach (var arg in commandLineArguments.Where(arg => arg.StartsWith('@')).Select(arg => arg.Substring(1)))
2020
{
21-
string line;
21+
string? line;
2222
using (StreamReader file = new StreamReader(arg))
2323
while ((line = file.ReadLine()) != null)
2424
textWriter.WriteLine(line);

csharp/extractor/Semmle.Util/DictionaryExtensions.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ public static class DictionaryExtensions
99
/// dictionary. If a list does not already exist, a new list is
1010
/// created.
1111
/// </summary>
12-
public static void AddAnother<T1, T2>(this Dictionary<T1, List<T2>> dict, T1 key, T2 element)
12+
public static void AddAnother<T1, T2>(this Dictionary<T1, List<T2>> dict, T1 key, T2 element) where T1:notnull
1313
{
14-
List<T2> list;
15-
if (!dict.TryGetValue(key, out list))
14+
if (!dict.TryGetValue(key, out var list))
1615
{
1716
list = new List<T2>();
1817
dict[key] = list;

csharp/extractor/Semmle.Util/FileUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public static void TryDelete(string file)
6262
///
6363
/// Returns <code>null</code> of no path can be found.
6464
/// </summary>
65-
public static string FindProgramOnPath(string prog)
65+
public static string? FindProgramOnPath(string prog)
6666
{
6767
var paths = Environment.GetEnvironmentVariable("PATH")?.Split(Path.PathSeparator);
6868
string[] exes;

csharp/extractor/Semmle.Util/FuzzyDictionary.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace Semmle.Util
3737
/// </remarks>
3838
///
3939
/// <typeparam name="T">The value type.</typeparam>
40-
public class FuzzyDictionary<T>
40+
public class FuzzyDictionary<T> where T:class
4141
{
4242
// All data items indexed by the "base string" (stripped of numbers)
4343
readonly Dictionary<string, List<KeyValuePair<string, T>>> index = new Dictionary<string, List<KeyValuePair<string, T>>>();
@@ -61,7 +61,7 @@ public void Add(string k, T v)
6161
/// <param name="v1">Vector 1</param>
6262
/// <param name="v2">Vector 2</param>
6363
/// <returns>The Hamming Distance.</returns>
64-
static int HammingDistance<U>(IEnumerable<U> v1, IEnumerable<U> v2)
64+
static int HammingDistance<U>(IEnumerable<U> v1, IEnumerable<U> v2) where U: notnull
6565
{
6666
return v1.Zip(v2, (x, y) => x.Equals(y) ? 0 : 1).Sum();
6767
}
@@ -72,11 +72,10 @@ static int HammingDistance<U>(IEnumerable<U> v1, IEnumerable<U> v2)
7272
/// <param name="query">The query string.</param>
7373
/// <param name="distance">The distance between the query string and the stored string.</param>
7474
/// <returns>The best match, or null (default).</returns>
75-
public T FindMatch(string query, out int distance)
75+
public T? FindMatch(string query, out int distance)
7676
{
7777
string root = StripDigits(query);
78-
List<KeyValuePair<string, T>> list;
79-
if (!index.TryGetValue(root, out list))
78+
if (!index.TryGetValue(root, out var list))
8079
{
8180
distance = 0;
8281
return default(T);
@@ -93,9 +92,9 @@ public T FindMatch(string query, out int distance)
9392
/// <param name="distance">The distance function.</param>
9493
/// <param name="bestDistance">The distance between the query and the stored string.</param>
9594
/// <returns>The stored value.</returns>
96-
static T BestMatch(string query, IEnumerable<KeyValuePair<string, T>> candidates, Func<string, string, int> distance, out int bestDistance)
95+
static T? BestMatch(string query, IEnumerable<KeyValuePair<string, T>> candidates, Func<string, string, int> distance, out int bestDistance)
9796
{
98-
T bestMatch = default(T);
97+
T? bestMatch = default(T);
9998
bestDistance = 0;
10099
bool first = true;
101100

csharp/extractor/Semmle.Util/IEnumerableExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public static void Enumerate<T>(this IEnumerable<T> items)
9393
/// <typeparam name="T">The type of the item.</typeparam>
9494
/// <param name="items">The list of items to hash.</param>
9595
/// <returns>The hash code.</returns>
96-
public static int SequenceHash<T>(this IEnumerable<T> items)
96+
public static int SequenceHash<T>(this IEnumerable<T> items) where T: notnull
9797
{
9898
int h = 0;
9999
foreach (var i in items)

csharp/extractor/Semmle.Util/LineCounter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ public sealed class LineCounts
3131
//#################### PUBLIC METHODS ####################
3232
#region
3333

34-
public override bool Equals(Object other)
34+
public override bool Equals(object? other)
3535
{
36-
LineCounts rhs = other as LineCounts;
36+
var rhs = other as LineCounts;
3737
return rhs != null && Total == rhs.Total && Code == rhs.Code && Comment == rhs.Comment;
3838
}
3939

csharp/extractor/Semmle.Util/Logger.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ public FileLogger(Verbosity verbosity, string outputFile)
6767

6868
try
6969
{
70-
var dir = Path.GetDirectoryName(outputFile);
71-
if (dir.Length > 0 && !System.IO.Directory.Exists(dir))
70+
string? dir = Path.GetDirectoryName(outputFile);
71+
if (!string.IsNullOrEmpty(dir) && !System.IO.Directory.Exists(dir))
7272
Directory.CreateDirectory(dir);
7373
writer = new PidStreamWriter(new FileStream(outputFile, FileMode.Append, FileAccess.Write,
7474
FileShare.ReadWrite, 8192));

csharp/extractor/Semmle.Util/LoggerUtils.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ public PidStreamWriter(Stream stream) : base(stream) { }
2121

2222
private readonly string prefix = "[" + Process.GetCurrentProcess().Id + "] ";
2323

24-
public override void WriteLine(string value)
24+
public override void WriteLine(string? value)
2525
{
2626
lock (mutex)
2727
{
2828
base.WriteLine(prefix + value);
2929
}
3030
}
3131

32-
public override void WriteLine(string value, object[] args)
32+
public override void WriteLine(string? value, object?[] args)
3333
{
34-
WriteLine(String.Format(value, args));
34+
WriteLine(value is null ? value : String.Format(value, args));
3535
}
3636

3737
readonly object mutex = new object();

0 commit comments

Comments
 (0)