C#: Enable nullability in Semmle.Util

This commit is contained in:
Calum Grant
2020-03-26 20:10:21 +00:00
parent 376779421d
commit 8a968dac81
13 changed files with 31 additions and 32 deletions

View File

@@ -9,20 +9,19 @@ namespace Semmle.Util
/// </summary>
/// <typeparam name="Key"></typeparam>
/// <typeparam name="Value"></typeparam>
public class ActionMap<Key, Value>
public class ActionMap<Key, Value> where Key : notnull
{
public void Add(Key key, Value value)
{
Action<Value> a;
if (actions.TryGetValue(key, out a))
if (actions.TryGetValue(key, out var a))
a(value);
values[key] = value;
}
public void OnAdd(Key key, Action<Value> action)
{
Action<Value> a;
if (actions.TryGetValue(key, out a))
if (actions.TryGetValue(key, out var a))
{
actions[key] = a + action;
}

View File

@@ -127,8 +127,8 @@ namespace Semmle.Util
if (parent != null)
{
string name = Path.GetFileName(path);
string parentPath = cache.GetCanonicalPath(parent.FullName);
string? name = Path.GetFileName(path);
string? parentPath = cache.GetCanonicalPath(parent.FullName);
try
{
string[] entries = Directory.GetFileSystemEntries(parentPath, name);
@@ -313,14 +313,15 @@ namespace Semmle.Util
/// <returns>The canonical path.</returns>
public string GetCanonicalPath(string path)
{
string canonicalPath;
lock (cache)
if (!cache.TryGetValue(path, out canonicalPath))
{
if (!cache.TryGetValue(path, out var canonicalPath))
{
canonicalPath = pathStrategy.GetCanonicalPath(path, this);
AddToCache(path, canonicalPath);
}
return canonicalPath;
return canonicalPath;
}
}
}
}

View File

@@ -18,7 +18,7 @@ namespace Semmle.Util
var found = false;
foreach (var arg in commandLineArguments.Where(arg => arg.StartsWith('@')).Select(arg => arg.Substring(1)))
{
string line;
string? line;
using (StreamReader file = new StreamReader(arg))
while ((line = file.ReadLine()) != null)
textWriter.WriteLine(line);

View File

@@ -9,10 +9,9 @@ namespace Semmle.Util
/// dictionary. If a list does not already exist, a new list is
/// created.
/// </summary>
public static void AddAnother<T1, T2>(this Dictionary<T1, List<T2>> dict, T1 key, T2 element)
public static void AddAnother<T1, T2>(this Dictionary<T1, List<T2>> dict, T1 key, T2 element) where T1:notnull
{
List<T2> list;
if (!dict.TryGetValue(key, out list))
if (!dict.TryGetValue(key, out var list))
{
list = new List<T2>();
dict[key] = list;

View File

@@ -62,7 +62,7 @@ namespace Semmle.Util
///
/// Returns <code>null</code> of no path can be found.
/// </summary>
public static string FindProgramOnPath(string prog)
public static string? FindProgramOnPath(string prog)
{
var paths = Environment.GetEnvironmentVariable("PATH")?.Split(Path.PathSeparator);
string[] exes;

View File

@@ -37,7 +37,7 @@ namespace Semmle.Util
/// </remarks>
///
/// <typeparam name="T">The value type.</typeparam>
public class FuzzyDictionary<T>
public class FuzzyDictionary<T> where T:class
{
// All data items indexed by the "base string" (stripped of numbers)
readonly Dictionary<string, List<KeyValuePair<string, T>>> index = new Dictionary<string, List<KeyValuePair<string, T>>>();
@@ -61,7 +61,7 @@ namespace Semmle.Util
/// <param name="v1">Vector 1</param>
/// <param name="v2">Vector 2</param>
/// <returns>The Hamming Distance.</returns>
static int HammingDistance<U>(IEnumerable<U> v1, IEnumerable<U> v2)
static int HammingDistance<U>(IEnumerable<U> v1, IEnumerable<U> v2) where U: notnull
{
return v1.Zip(v2, (x, y) => x.Equals(y) ? 0 : 1).Sum();
}
@@ -72,11 +72,10 @@ namespace Semmle.Util
/// <param name="query">The query string.</param>
/// <param name="distance">The distance between the query string and the stored string.</param>
/// <returns>The best match, or null (default).</returns>
public T FindMatch(string query, out int distance)
public T? FindMatch(string query, out int distance)
{
string root = StripDigits(query);
List<KeyValuePair<string, T>> list;
if (!index.TryGetValue(root, out list))
if (!index.TryGetValue(root, out var list))
{
distance = 0;
return default(T);
@@ -93,9 +92,9 @@ namespace Semmle.Util
/// <param name="distance">The distance function.</param>
/// <param name="bestDistance">The distance between the query and the stored string.</param>
/// <returns>The stored value.</returns>
static T BestMatch(string query, IEnumerable<KeyValuePair<string, T>> candidates, Func<string, string, int> distance, out int bestDistance)
static T? BestMatch(string query, IEnumerable<KeyValuePair<string, T>> candidates, Func<string, string, int> distance, out int bestDistance)
{
T bestMatch = default(T);
T? bestMatch = default(T);
bestDistance = 0;
bool first = true;

View File

@@ -93,7 +93,7 @@ namespace Semmle.Util
/// <typeparam name="T">The type of the item.</typeparam>
/// <param name="items">The list of items to hash.</param>
/// <returns>The hash code.</returns>
public static int SequenceHash<T>(this IEnumerable<T> items)
public static int SequenceHash<T>(this IEnumerable<T> items) where T: notnull
{
int h = 0;
foreach (var i in items)

View File

@@ -31,9 +31,9 @@ namespace Semmle.Util
//#################### PUBLIC METHODS ####################
#region
public override bool Equals(Object other)
public override bool Equals(object? other)
{
LineCounts rhs = other as LineCounts;
var rhs = other as LineCounts;
return rhs != null && Total == rhs.Total && Code == rhs.Code && Comment == rhs.Comment;
}

View File

@@ -67,8 +67,8 @@ namespace Semmle.Util.Logging
try
{
var dir = Path.GetDirectoryName(outputFile);
if (dir.Length > 0 && !System.IO.Directory.Exists(dir))
string? dir = Path.GetDirectoryName(outputFile);
if (!string.IsNullOrEmpty(dir) && !System.IO.Directory.Exists(dir))
Directory.CreateDirectory(dir);
writer = new PidStreamWriter(new FileStream(outputFile, FileMode.Append, FileAccess.Write,
FileShare.ReadWrite, 8192));

View File

@@ -21,7 +21,7 @@ namespace Semmle.Util
private readonly string prefix = "[" + Process.GetCurrentProcess().Id + "] ";
public override void WriteLine(string value)
public override void WriteLine(string? value)
{
lock (mutex)
{
@@ -29,9 +29,9 @@ namespace Semmle.Util
}
}
public override void WriteLine(string value, object[] args)
public override void WriteLine(string? value, object?[] args)
{
WriteLine(String.Format(value, args));
WriteLine(value is null ? value : String.Format(value, args));
}
readonly object mutex = new object();

View File

@@ -14,7 +14,7 @@ namespace Semmle.Util
stdout = new List<string>();
using (var process = Process.Start(pi))
{
string s;
string? s;
do
{
s = process.StandardOutput.ReadLine();

View File

@@ -6,6 +6,7 @@
<RootNamespace>Semmle.Util</RootNamespace>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<RuntimeIdentifiers>win-x64;linux-x64;osx-x64</RuntimeIdentifiers>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>

View File

@@ -13,6 +13,6 @@ namespace Semmle.Util
/// <summary>
/// The shared object to which different parts of the code want to refer.
/// </summary>
public T Obj { get; set; }
public T? Obj { get; set; }
}
}