Apply IDE0150 (Prefer 'null' check over type check) on Microsoft.CodeAnalysis.Testing

This commit is contained in:
Sam Harwell 2023-02-13 12:42:00 -06:00
Родитель 2fe5f22ffd
Коммит a5327b7bce
7 изменённых файлов: 25 добавлений и 25 удалений

Просмотреть файл

@ -536,11 +536,11 @@ namespace Microsoft.CodeAnalysis.Testing
private void VerifyDiagnosticResults(IEnumerable<(Project project, Diagnostic diagnostic)> actualResults, ImmutableArray<DiagnosticAnalyzer> analyzers, DiagnosticResult[] expectedResults, IVerifier verifier)
{
var matchedDiagnostics = MatchDiagnostics(actualResults.ToArray(), expectedResults);
verifier.Equal(actualResults.Count(), matchedDiagnostics.Count(x => x.actual is object), $"{nameof(MatchDiagnostics)} failed to include all actual diagnostics in the result");
verifier.Equal(expectedResults.Length, matchedDiagnostics.Count(x => x.expected is object), $"{nameof(MatchDiagnostics)} failed to include all expected diagnostics in the result");
verifier.Equal(actualResults.Count(), matchedDiagnostics.Count(x => x.actual is not null), $"{nameof(MatchDiagnostics)} failed to include all actual diagnostics in the result");
verifier.Equal(expectedResults.Length, matchedDiagnostics.Count(x => x.expected is not null), $"{nameof(MatchDiagnostics)} failed to include all expected diagnostics in the result");
actualResults = matchedDiagnostics.Select(x => x.actual).Where(x => x is { }).Select(x => x!.Value);
expectedResults = matchedDiagnostics.Where(x => x.expected is object).Select(x => x.expected.GetValueOrDefault()).ToArray();
expectedResults = matchedDiagnostics.Where(x => x.expected is not null).Select(x => x.expected.GetValueOrDefault()).ToArray();
var expectedCount = expectedResults.Length;
var actualCount = actualResults.Count();

Просмотреть файл

@ -147,7 +147,7 @@ namespace Microsoft.CodeAnalysis.Testing
return null;
}
if (result is object)
if (result is not null)
{
codeActionVerifier?.Invoke(result, verifier);
}

Просмотреть файл

@ -10,7 +10,7 @@ namespace Microsoft.CodeAnalysis.Testing
{
internal static class IEnumerableExtensions
{
private static readonly Func<object?, bool> s_notNullTest = x => x is object;
private static readonly Func<object?, bool> s_notNullTest = x => x is not null;
public static DiagnosticResult[] ToOrderedArray(this IEnumerable<DiagnosticResult> diagnosticResults)
{

Просмотреть файл

@ -22,10 +22,10 @@ namespace Microsoft.CodeAnalysis.Testing
Func<string, DocumentationProvider?> createDocumentationProvider = _ => null;
var xmlDocumentationProvider = typeof(Workspace).GetTypeInfo().Assembly.GetType("Microsoft.CodeAnalysis.XmlDocumentationProvider");
if (xmlDocumentationProvider is object)
if (xmlDocumentationProvider is not null)
{
var createFromFile = xmlDocumentationProvider.GetTypeInfo().GetMethod("CreateFromFile", new[] { typeof(string) });
if (createFromFile is object)
if (createFromFile is not null)
{
var xmlDocCommentFilePath = Expression.Parameter(typeof(string), "xmlDocCommentFilePath");
var body = Expression.Convert(

Просмотреть файл

@ -165,7 +165,7 @@ namespace Microsoft.CodeAnalysis.Testing
public async Task<ImmutableArray<MetadataReference>> ResolveAsync(string? language, CancellationToken cancellationToken)
{
if (language is object)
if (language is not null)
{
if (LanguageSpecificAssemblies.IsEmpty
|| !LanguageSpecificAssemblies.TryGetValue(language, out var languageSpecificAssemblies)
@ -222,7 +222,7 @@ namespace Microsoft.CodeAnalysis.Testing
repositories = repositories.Insert(0, sourceRepositoryProvider.CreateRepository(new PackageSource(new Uri(SettingsUtility.GetGlobalPackagesFolder(settings)).AbsoluteUri, "global"), FeedType.FileSystemV3));
var dependencies = ImmutableDictionary.CreateBuilder<NuGet.Packaging.Core.PackageIdentity, SourcePackageDependencyInfo>(PackageIdentityComparer.Default);
if (ReferenceAssemblyPackage is object)
if (ReferenceAssemblyPackage is not null)
{
await GetPackageDependenciesAsync(ReferenceAssemblyPackage.ToNuGetIdentity(), targetFramework, repositories, cacheContext, logger, dependencies, cancellationToken);
}
@ -235,7 +235,7 @@ namespace Microsoft.CodeAnalysis.Testing
var availablePackages = dependencies.ToImmutable();
var packagesToInstall = new List<NuGet.Packaging.Core.PackageIdentity>();
if (ReferenceAssemblyPackage is object)
if (ReferenceAssemblyPackage is not null)
{
packagesToInstall.Add(ReferenceAssemblyPackage.ToNuGetIdentity()!);
}
@ -244,7 +244,7 @@ namespace Microsoft.CodeAnalysis.Testing
{
var targetIds = new List<string>(Packages.Select(package => package.Id));
var preferredVersions = new List<NuGet.Packaging.Core.PackageIdentity>(Packages.Select(package => package.ToNuGetIdentity()));
if (ReferenceAssemblyPackage is object)
if (ReferenceAssemblyPackage is not null)
{
// Make sure to include the implicit reference assembly package
if (!targetIds.Contains(ReferenceAssemblyPackage.Id))
@ -394,7 +394,7 @@ namespace Microsoft.CodeAnalysis.Testing
var nearestFramework = frameworkReducer.GetNearest(targetFramework, frameworkItems.Select(x => x.TargetFramework));
var refItems = await packageReader.GetItemsAsync(PackagingConstants.Folders.Ref, cancellationToken);
var nearestRef = frameworkReducer.GetNearest(targetFramework, refItems.Select(x => x.TargetFramework));
if (nearestRef is object)
if (nearestRef is not null)
{
var nearestRefItems = refItems.Single(x => x.TargetFramework == nearestRef);
foreach (var item in nearestRefItems.Items)
@ -409,7 +409,7 @@ namespace Microsoft.CodeAnalysis.Testing
resolvedAssemblies.Add(Path.Combine(installedPath, item));
}
}
else if (nearestLib is object)
else if (nearestLib is not null)
{
var nearestLibItems = libItems.Single(x => x.TargetFramework == nearestLib);
foreach (var item in nearestLibItems.Items)
@ -426,18 +426,18 @@ namespace Microsoft.CodeAnalysis.Testing
}
// Include framework references except for package based frameworks
if (!targetFramework.IsPackageBased && nearestFramework is object)
if (!targetFramework.IsPackageBased && nearestFramework is not null)
{
var nearestFrameworkItems = frameworkItems.Single(x => x.TargetFramework == nearestFramework);
frameworkAssemblies.UnionWith(nearestFrameworkItems.Items);
}
}
var referenceAssemblyInstalledPath = ReferenceAssemblyPackage is object
var referenceAssemblyInstalledPath = ReferenceAssemblyPackage is not null
? GetInstalledPath(localPathResolver, globalPathResolver, ReferenceAssemblyPackage.ToNuGetIdentity())
: null;
Debug.Assert(ReferenceAssemblyPackage is null || referenceAssemblyInstalledPath is object, $"Assertion failed: {nameof(ReferenceAssemblyPackage)} is null || {nameof(referenceAssemblyInstalledPath)} is object");
Debug.Assert(ReferenceAssemblyPackage is null || ReferenceAssemblyPath is object, $"Assertion failed: {nameof(ReferenceAssemblyPackage)} is null || {nameof(ReferenceAssemblyPath)} is object");
Debug.Assert(ReferenceAssemblyPackage is null || referenceAssemblyInstalledPath is not null, $"Assertion failed: {nameof(ReferenceAssemblyPackage)} is null || {nameof(referenceAssemblyInstalledPath)} is object");
Debug.Assert(ReferenceAssemblyPackage is null || ReferenceAssemblyPath is not null, $"Assertion failed: {nameof(ReferenceAssemblyPackage)} is null || {nameof(ReferenceAssemblyPath)} is object");
foreach (var assembly in frameworkAssemblies)
{
@ -461,7 +461,7 @@ namespace Microsoft.CodeAnalysis.Testing
}
// Prefer assemblies from the reference assembly package to ones otherwise provided
if (ReferenceAssemblyPackage is object)
if (ReferenceAssemblyPackage is not null)
{
var referenceAssemblies = new HashSet<string>(resolvedAssemblies.Where(resolved => resolved.StartsWith(referenceAssemblyInstalledPath!)));
@ -471,7 +471,7 @@ namespace Microsoft.CodeAnalysis.Testing
}
// Add the facade assemblies
if (ReferenceAssemblyPackage is object)
if (ReferenceAssemblyPackage is not null)
{
var facadesPath = Path.Combine(referenceAssemblyInstalledPath!, ReferenceAssemblyPath!, "Facades");
if (Directory.Exists(facadesPath))
@ -519,7 +519,7 @@ namespace Microsoft.CodeAnalysis.Testing
{
installedPath = GetInstalledPath(localPathResolver, packageIdentity)
?? GetInstalledPath(globalPathResolver, packageIdentity);
if (installedPath is object)
if (installedPath is not null)
{
installedPath = ImmutableInterlocked.GetOrAdd(ref s_packageToInstalledLocation, packageIdentity, installedPath);
}

Просмотреть файл

@ -423,7 +423,7 @@ namespace Microsoft.CodeAnalysis.Testing
public static string CreateTestFile(string code, int? position, ImmutableDictionary<string, ImmutableArray<TextSpan>> spans)
{
var positions = position is object ? ImmutableArray.Create(position.Value) : ImmutableArray<int>.Empty;
var positions = position is not null ? ImmutableArray.Create(position.Value) : ImmutableArray<int>.Empty;
return CreateTestFile(code, positions, spans.ToImmutableDictionary(pair => pair.Key, pair => pair.Value.ToImmutableArray()));
}

Просмотреть файл

@ -615,13 +615,13 @@ namespace Microsoft.CodeAnalysis.Testing
var fixableDiagnostics = analyzerDiagnostics
.Where(diagnostic => codeFixProviders.Any(provider => provider.FixableDiagnosticIds.Contains(diagnostic.diagnostic.Id)))
.Where(diagnostic => project.Solution.GetDocument(diagnostic.diagnostic.Location.SourceTree) is object)
.Where(diagnostic => project.Solution.GetDocument(diagnostic.diagnostic.Location.SourceTree) is not null)
.ToImmutableArray();
if (CodeFixTestBehaviors.HasFlag(CodeFixTestBehaviors.FixOne))
{
var diagnosticToFix = TrySelectDiagnosticToFix(fixableDiagnostics.Select(x => x.diagnostic).ToImmutableArray());
fixableDiagnostics = diagnosticToFix is object ? ImmutableArray.Create(fixableDiagnostics.Single(x => x.diagnostic == diagnosticToFix)) : ImmutableArray<(Project project, Diagnostic diagnostic)>.Empty;
fixableDiagnostics = diagnosticToFix is not null ? ImmutableArray.Create(fixableDiagnostics.Single(x => x.diagnostic == diagnosticToFix)) : ImmutableArray<(Project project, Diagnostic diagnostic)>.Empty;
}
done = true;
@ -756,13 +756,13 @@ namespace Microsoft.CodeAnalysis.Testing
var fixableDiagnostics = analyzerDiagnostics
.Where(diagnostic => codeFixProviders.Any(provider => provider.FixableDiagnosticIds.Contains(diagnostic.diagnostic.Id)))
.Where(diagnostic => project.Solution.GetDocument(diagnostic.diagnostic.Location.SourceTree) is object)
.Where(diagnostic => project.Solution.GetDocument(diagnostic.diagnostic.Location.SourceTree) is not null)
.ToImmutableArray();
if (CodeFixTestBehaviors.HasFlag(CodeFixTestBehaviors.FixOne))
{
var diagnosticToFix = TrySelectDiagnosticToFix(fixableDiagnostics.Select(x => x.diagnostic).ToImmutableArray());
fixableDiagnostics = diagnosticToFix is object ? ImmutableArray.Create(fixableDiagnostics.Single(x => x.diagnostic == diagnosticToFix)) : ImmutableArray<(Project project, Diagnostic diagnostic)>.Empty;
fixableDiagnostics = diagnosticToFix is not null ? ImmutableArray.Create(fixableDiagnostics.Single(x => x.diagnostic == diagnosticToFix)) : ImmutableArray<(Project project, Diagnostic diagnostic)>.Empty;
}
Diagnostic? firstDiagnostic = null;