Skip to content

Commit

Permalink
Merge pull request #4979 from drewnoakes/nullable-annotations
Browse files Browse the repository at this point in the history
Nullable annotations
  • Loading branch information
drewnoakes authored Jul 3, 2019
2 parents d98f426 + 0ad8423 commit c70a477
Show file tree
Hide file tree
Showing 53 changed files with 166 additions and 212 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ private void Search(IGraphContext graphContext)
cachedDependencyToMatchingResultsMap);
}

cachedDependencyToMatchingResultsMap[topLevelDependency.Id] = topLevelDependencyMatches;
cachedDependencyToMatchingResultsMap[topLevelDependency.Id] = topLevelDependencyMatches!;
}

if (topLevelDependencyMatches.Count == 0)
if (topLevelDependencyMatches!.Count == 0)
{
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
using Microsoft.Build.Construction;
using Microsoft.Build.Evaluation;

#nullable disable

namespace Microsoft.VisualStudio.Build
{
internal static class BuildExtensions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
using Microsoft.VisualStudio.Buffers.PooledObjects;
using Microsoft.VisualStudio.Text;

#nullable disable

namespace Microsoft.VisualStudio.Build
{
/// <summary>
Expand Down Expand Up @@ -54,7 +52,7 @@ public static bool HasWellKnownConditionsThatAlwaysEvaluateToTrue(ProjectPropert
/// <param name="project">Xml representation of the MsBuild project.</param>
/// <param name="propertyName">Name of the property.</param>
/// <returns>Requested project property. Null if the property is not present.</returns>
public static ProjectPropertyElement GetProperty(ProjectRootElement project, string propertyName)
public static ProjectPropertyElement? GetProperty(ProjectRootElement project, string propertyName)
{
Requires.NotNull(project, "project");

Expand All @@ -70,7 +68,7 @@ public static ProjectPropertyElement GetProperty(ProjectRootElement project, str
/// <returns>Collection of individual values in the property.</returns>
public static IEnumerable<string> GetPropertyValues(string propertyValue, char delimiter = ';')
{
HashSet<string> seen = null;
HashSet<string>? seen = null;

// We need to ensure that we return values in the specified order.
foreach (string value in new LazyStringSplit(propertyValue, delimiter))
Expand Down Expand Up @@ -223,7 +221,7 @@ public static ProjectPropertyElement GetProperty(ProjectRootElement project, str
public static ProjectPropertyElement GetOrAddProperty(ProjectRootElement project, string propertyName)
{
Requires.NotNull(project, "project");
ProjectPropertyElement property = GetProperty(project, propertyName);
ProjectPropertyElement? property = GetProperty(project, propertyName);

if (property != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
using System.Collections.Generic;
using System.Collections.Immutable;

#nullable disable

namespace Microsoft.VisualStudio.Collections
{
/// <summary>
Expand Down Expand Up @@ -49,9 +47,10 @@ public int GetHashCode(IImmutableDictionary<TKey, TValue> obj)
{
int hashCode = 0;

var concreteDictionary1 = obj as ImmutableDictionary<TKey, TValue>;
ImmutableDictionary<TKey, TValue>? concreteDictionary1 = obj as ImmutableDictionary<TKey, TValue>;
IEqualityComparer<TKey> keyComparer = concreteDictionary1 != null ? concreteDictionary1.KeyComparer : EqualityComparer<TKey>.Default;
IEqualityComparer<TValue> valueComparer = concreteDictionary1 != null ? concreteDictionary1.ValueComparer : EqualityComparer<TValue>.Default;

if (obj != null)
{
foreach (KeyValuePair<TKey, TValue> pair in obj)
Expand All @@ -68,6 +67,7 @@ public int GetHashCode(IImmutableDictionary<TKey, TValue> obj)
private static bool AreEquivalent(IImmutableDictionary<TKey, TValue> dictionary1, IImmutableDictionary<TKey, TValue> dictionary2)
{
Requires.NotNull(dictionary1, "dictionary1");

if (dictionary1 == dictionary2)
{
return true;
Expand All @@ -80,7 +80,7 @@ private static bool AreEquivalent(IImmutableDictionary<TKey, TValue> dictionary1
/// <summary>
/// Tests two dictionaries to see if their contents are identical.
/// </summary>
private static bool AreEquivalent(IReadOnlyDictionary<TKey, TValue> dictionary1, IReadOnlyDictionary<TKey, TValue> dictionary2, IEqualityComparer<TValue> valueComparer)
private static bool AreEquivalent(IReadOnlyDictionary<TKey, TValue>? dictionary1, IReadOnlyDictionary<TKey, TValue>? dictionary2, IEqualityComparer<TValue> valueComparer)
{
Requires.NotNull(valueComparer, "valueComparer");

Expand All @@ -89,7 +89,7 @@ private static bool AreEquivalent(IReadOnlyDictionary<TKey, TValue> dictionary1,
return true;
}

if ((dictionary1 == null) ^ (dictionary2 == null)) // XOR
if (dictionary1 == null || dictionary2 == null)
{
return false;
}
Expand All @@ -116,6 +116,5 @@ private static bool AreEquivalent(IReadOnlyDictionary<TKey, TValue> dictionary1,

return true;
}

}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

#nullable disable

namespace System.Collections.Immutable
{
internal static class ImmutableStringDictionary<TValue>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

#nullable disable

namespace System.Collections.Immutable
{
internal static class ImmutableStringHashSet
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

#nullable disable

namespace System.Collections.Generic
{
internal static class DeconstructionExtensions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
using System.Collections.Generic;
using System.IO;
using System.Text;

using Microsoft.VisualStudio.Composition;
using Microsoft.VisualStudio.ProjectSystem;

#nullable disable

namespace Microsoft.VisualStudio.IO
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,26 @@
using System;
using System.IO;

#nullable disable

namespace Microsoft.VisualStudio.IO
{
/// <summary>
/// Simple wrapper around the FileSystemWatcher.
/// </summary>
internal sealed class SimpleFileWatcher : IDisposable
{
private FileSystemWatcher FileWatcher { get; set; }
private FileSystemEventHandler _handler;
private RenamedEventHandler _renameHandler;
private FileSystemWatcher? _fileWatcher;
private FileSystemEventHandler? _handler;
private RenamedEventHandler? _renameHandler;

// For unit tests
public SimpleFileWatcher()
{
}

public SimpleFileWatcher(string dirToWatch, bool includeSubDirs, NotifyFilters notifyFilters, string fileFilter,
FileSystemEventHandler handler, RenamedEventHandler renameHandler)
FileSystemEventHandler? handler, RenamedEventHandler? renameHandler)
{
FileWatcher = new FileSystemWatcher(dirToWatch)
_fileWatcher = new FileSystemWatcher(dirToWatch)
{
IncludeSubdirectories = includeSubDirs,
NotifyFilter = notifyFilters,
Expand All @@ -33,42 +31,40 @@ public SimpleFileWatcher(string dirToWatch, bool includeSubDirs, NotifyFilters n

if (handler != null)
{
FileWatcher.Created += handler;
FileWatcher.Deleted += handler;
FileWatcher.Changed += handler;
_fileWatcher.Created += handler;
_fileWatcher.Deleted += handler;
_fileWatcher.Changed += handler;
}

if (renameHandler != null)
{
FileWatcher.Renamed += renameHandler;
_fileWatcher.Renamed += renameHandler;
}
FileWatcher.EnableRaisingEvents = true;

_fileWatcher.EnableRaisingEvents = true;
_handler = handler;
_renameHandler = renameHandler;
}

/// <summary>
/// Cleans up our watcher on the Project.Json file
/// </summary>
public void Dispose()
{
if (FileWatcher != null)
if (_fileWatcher != null)
{
FileWatcher.EnableRaisingEvents = false;
_fileWatcher.EnableRaisingEvents = false;
if (_handler != null)
{
FileWatcher.Created += _handler;
FileWatcher.Deleted += _handler;
FileWatcher.Changed += _handler;
_fileWatcher.Created -= _handler;
_fileWatcher.Deleted -= _handler;
_fileWatcher.Changed -= _handler;
_handler = null;
}
if (_renameHandler != null)
{
FileWatcher.Renamed += _renameHandler;
_fileWatcher.Renamed -= _renameHandler;
_renameHandler = null;
}
FileWatcher.Dispose();
FileWatcher = null;
_fileWatcher.Dispose();
_fileWatcher = null;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
using System.IO;
using System.Text;

#nullable disable

namespace Microsoft.VisualStudio.IO
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
using System.Diagnostics;
using System.Threading;

#nullable disable

namespace Microsoft.VisualStudio.Buffers.PooledObjects
{
/// <summary>
Expand All @@ -30,12 +28,12 @@ internal class ObjectPool<T> where T : class
[DebuggerDisplay("{Value,nq}")]
private struct Element
{
internal T Value;
internal T? Value;
}

// Storage for the pool objects. The first item is stored in a dedicated field because we
// expect to be able to satisfy most requests from it.
private T _firstItem;
private T? _firstItem;
private readonly Element[] _items;

// factory is stored for the lifetime of the pool. We will call this only when pool needs to
Expand Down Expand Up @@ -74,7 +72,7 @@ internal T Allocate()
// Note that the initial read is optimistically not synchronized. That is intentional.
// We will interlock only when we have a candidate. in a worst case we may miss some
// recently returned objects. Not a big deal.
T inst = _firstItem;
T? inst = _firstItem;
if (inst == null || inst != Interlocked.CompareExchange(ref _firstItem, null, inst))
{
inst = AllocateSlow();
Expand All @@ -92,7 +90,7 @@ private T AllocateSlow()
// Note that the initial read is optimistically not synchronized. That is intentional.
// We will interlock only when we have a candidate. in a worst case we may miss some
// recently returned objects. Not a big deal.
T inst = items[i].Value;
T? inst = items[i].Value;
if (inst != null)
{
if (inst == Interlocked.CompareExchange(ref items[i].Value, null, inst))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
using System.Collections.Generic;
using System.Collections.Immutable;

#nullable disable

namespace Microsoft.VisualStudio.Buffers.PooledObjects
{
// Dictionary that can be recycled via an object pool
Expand Down Expand Up @@ -37,8 +35,8 @@ public void Free()
// if someone needs to create a pool;
public static ObjectPool<PooledDictionary<K, V>> CreatePool()
{
ObjectPool<PooledDictionary<K, V>> pool = null;
pool = new ObjectPool<PooledDictionary<K, V>>(() => new PooledDictionary<K, V>(pool), 128);
ObjectPool<PooledDictionary<K, V>>? pool = null;
pool = new ObjectPool<PooledDictionary<K, V>>(() => new PooledDictionary<K, V>(pool!), 128);
return pool;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

using System.Collections.Generic;

#nullable disable

namespace Microsoft.VisualStudio.Buffers.PooledObjects
{
// HashSet that can be recycled via an object pool
Expand All @@ -29,8 +27,8 @@ public void Free()
// if someone needs to create a pool;
public static ObjectPool<PooledHashSet<T>> CreatePool()
{
ObjectPool<PooledHashSet<T>> pool = null;
pool = new ObjectPool<PooledHashSet<T>>(() => new PooledHashSet<T>(pool), 128);
ObjectPool<PooledHashSet<T>>? pool = null;
pool = new ObjectPool<PooledHashSet<T>>(() => new PooledHashSet<T>(pool!), 128);
return pool;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
using System;
using System.Text;

#nullable disable

namespace Microsoft.VisualStudio.Buffers.PooledObjects
{
/// <summary>
Expand Down Expand Up @@ -57,16 +55,15 @@ public string ToStringAndFree(int startIndex, int length)
// global pool
private static readonly ObjectPool<PooledStringBuilder> s_poolInstance = CreatePool();

// if someone needs to create a private pool;
/// <summary>
/// If someone need to create a private pool
/// </summary>
/// <param name="size">The size of the pool.</param>
/// <returns></returns>
public static ObjectPool<PooledStringBuilder> CreatePool(int size = 32)
{
ObjectPool<PooledStringBuilder> pool = null;
pool = new ObjectPool<PooledStringBuilder>(() => new PooledStringBuilder(pool), size);
ObjectPool<PooledStringBuilder>? pool = null;
pool = new ObjectPool<PooledStringBuilder>(() => new PooledStringBuilder(pool!), size);
return pool;
}

Expand Down
Loading

0 comments on commit c70a477

Please sign in to comment.