Skip to content

Commit a75155f

Browse files
Prefer 'is null' over '== null'
1 parent 41a45a9 commit a75155f

File tree

86 files changed

+337
-322
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+337
-322
lines changed

.editorconfig

+15-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,18 @@ indent_size = 4
1313

1414
[*.{proj,csproj,vcxproj,xproj,json,config,nuspec,xml,yml}]
1515
indent_style = space
16-
indent_size = 2
16+
indent_size = 2
17+
18+
##########################################################################################
19+
# Analysis settings
20+
21+
[*.cs]
22+
# CSharpIsNull Analyzers
23+
24+
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:warning
25+
26+
# CSIsNull001: Use `is null` for null checks
27+
dotnet_diagnostic.CSIsNull001.severity = warning
28+
29+
# CSIsNull002: Use `is object` for non-null checks
30+
dotnet_diagnostic.CSIsNull002.severity = warning

src/Directory.Build.props

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<ItemGroup>
4444
<PackageReference Include="StyleCop.Analyzers.Unstable" Version="1.2.0.556" PrivateAssets="all" />
4545
<GlobalAnalyzerConfigFiles Include="$(MSBuildThisFileDirectory)/StyleCop.Analyzers.globalconfig" />
46+
<PackageReference Include="CSharpIsNullAnalyzer" Version="0.1.593" PrivateAssets="all" />
4647
</ItemGroup>
4748

4849
</Project>

src/NUnitCommon/nunit.agent.core.tests/Drivers/NotRunnableFrameworkDriverTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ private IFrameworkDriver GetDriver(string filePath)
100100
private static string? GetSkipReason(XmlNode result)
101101
{
102102
var propNode = result.SelectSingleNode(string.Format("properties/property[@name='{0}']", PropertyNames.SkipReason));
103-
return propNode == null ? null : propNode.GetAttribute("value");
103+
return propNode is null ? null : propNode.GetAttribute("value");
104104
}
105105

106106
private class NullListener : ITestEventListener

src/NUnitCommon/nunit.agent.core.tests/Runners/DomainManagerStaticTests.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public static void ApplicationBaseTests(string filePath, string? appBase, string
9393
expected = TestPath(expected);
9494

9595
var package = new TestPackage(filePath);
96-
if (appBase != null)
96+
if (appBase is not null)
9797
package.Settings["BasePath"] = appBase;
9898

9999
Assert.That(DomainManager.GetApplicationBase(package), Is.SamePath(expected));
@@ -124,7 +124,7 @@ public static void ConfigFileTests(string filePath, string appBase, string? conf
124124
expected = TestPath(expected);
125125

126126
var package = new TestPackage(filePath);
127-
if (configSetting != null)
127+
if (configSetting is not null)
128128
package.Settings["ConfigurationFile"] = configSetting;
129129

130130
Assert.That(DomainManager.GetConfigFile(appBase, package), Is.EqualTo(expected));
@@ -138,7 +138,7 @@ public static void ConfigFileTests(string filePath, string appBase, string? conf
138138
[return: NotNullIfNotNull(nameof(path))]
139139
private static string? TestPath(string? path)
140140
{
141-
if (path != null && Path.DirectorySeparatorChar != '/')
141+
if (path is not null && Path.DirectorySeparatorChar != '/')
142142
{
143143
path = path.Replace('/', Path.DirectorySeparatorChar);
144144
if (path[0] == Path.DirectorySeparatorChar)

src/NUnitCommon/nunit.agent.core.tests/Runners/TestAgentRunnerTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void Initialize()
3535
[TearDown]
3636
public void Cleanup()
3737
{
38-
if (_runner != null)
38+
if (_runner is not null)
3939
_runner.Dispose();
4040
}
4141

src/NUnitCommon/nunit.agent.core/AgentOptions.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ public AgentOptions(params string[] args)
5555

5656
if (optionTakesValue)
5757
{
58-
if (val == null && index + 1 < args.Length)
58+
if (val is null && index + 1 < args.Length)
5959
val = args[++index];
6060

61-
if (val == null)
61+
if (val is null)
6262
throw new Exception($"Option requires a value: {arg}");
6363
}
6464
else if (delim > 0)

src/NUnitCommon/nunit.agent.core/Agents/RemoteTestAgent.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ public RemoteTestAgent(Guid agentId) : base(agentId)
3232

3333
public override bool Start()
3434
{
35-
Guard.OperationValid(Transport != null, "Transport must be set before calling Start().");
35+
Guard.OperationValid(Transport is not null, "Transport must be set before calling Start().");
3636
return Transport.Start();
3737
}
3838

3939
public override void Stop()
4040
{
41-
Guard.OperationValid(Transport != null, "Transport must be set before calling Stop().");
41+
Guard.OperationValid(Transport is not null, "Transport must be set before calling Stop().");
4242
Transport.Stop();
4343
}
4444

src/NUnitCommon/nunit.agent.core/Communication/Transports/Remoting/TestAgentRemotingTransport.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public bool Start()
7070

7171
public void Stop()
7272
{
73-
Guard.OperationValid(_channel != null, "Channel is not open");
73+
Guard.OperationValid(_channel is not null, "Channel is not open");
7474

7575
log.Info("Stopping");
7676

src/NUnitCommon/nunit.agent.core/Drivers/DriverService.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public IFrameworkDriver GetDriver(AppDomain domain, TestPackage package, string
5656
if (!PathUtils.IsAssemblyFileType(assemblyPath))
5757
return new InvalidAssemblyFrameworkDriver(assemblyPath, package.ID, "File type is not supported");
5858

59-
if (targetFramework != null)
59+
if (targetFramework is not null)
6060
{
6161
// This takes care of an issue with Roslyn. It may get fixed, but we still
6262
// have to deal with assemblies having this setting. I'm assuming that

src/NUnitCommon/nunit.agent.core/Drivers/NUnitFrameworkApi2009.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public string Load(string testAssemblyPath, IDictionary<string, object> settings
7272
null,
7373
null).ShouldNotBeNull();
7474
}
75-
catch (BadImageFormatException ex) when (requestedRuntime != null)
75+
catch (BadImageFormatException ex) when (requestedRuntime is not null)
7676
{
7777
throw new NUnitEngineException($"Requested runtime {requestedRuntime} is not suitable for use with test assembly {_testAssemblyPath}", ex);
7878
}
@@ -113,7 +113,7 @@ public string Explore(string filter)
113113

114114
private void CheckLoadWasCalled()
115115
{
116-
if (_frameworkController == null)
116+
if (_frameworkController is null)
117117
throw new InvalidOperationException(LOAD_MESSAGE);
118118
}
119119

src/NUnitCommon/nunit.agent.core/Drivers/NUnitFrameworkApi2018.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public string Load(string testAssemblyPath, IDictionary<string, object> settings
9696
_frameworkAssembly = LoadAssembly(_nunitRef);
9797

9898
_frameworkController = CreateInstance(CONTROLLER_TYPE, testAssembly, idPrefix, settings);
99-
if (_frameworkController == null)
99+
if (_frameworkController is null)
100100
{
101101
log.Error(INVALID_FRAMEWORK_MESSAGE);
102102
throw new NUnitEngineException(INVALID_FRAMEWORK_MESSAGE);
@@ -114,14 +114,14 @@ public int CountTestCases(string filter)
114114
{
115115
CheckLoadWasCalled();
116116
object? count = ExecuteMethod(COUNT_METHOD, filter);
117-
return count != null ? (int)count : 0;
117+
return count is not null ? (int)count : 0;
118118
}
119119

120120
public string Run(ITestEventListener? listener, string filter)
121121
{
122122
CheckLoadWasCalled();
123123
log.Info("Running {0} - see separate log file", Path.GetFileName(_testAssemblyPath.ShouldNotBeNull()));
124-
Action<string>? callback = listener != null ? listener.OnTestEvent : null;
124+
Action<string>? callback = listener is not null ? listener.OnTestEvent : null;
125125
return (string)ExecuteMethod(RUN_METHOD, [typeof(Action<string>), typeof(string)], callback, filter);
126126
}
127127

@@ -146,7 +146,7 @@ public string Explore(string filter)
146146

147147
private void CheckLoadWasCalled()
148148
{
149-
if (_frameworkController == null)
149+
if (_frameworkController is null)
150150
throw new InvalidOperationException(LOAD_MESSAGE);
151151
}
152152

@@ -164,7 +164,7 @@ private Assembly LoadAssembly(string assemblyPath)
164164
try
165165
{
166166
assembly = _assemblyLoadContext?.LoadFromAssemblyPath(assemblyPath)!;
167-
if (assembly == null)
167+
if (assembly is null)
168168
throw new Exception("LoadFromAssemblyPath returned null");
169169
}
170170
catch (Exception e)
@@ -185,7 +185,7 @@ private Assembly LoadAssembly(AssemblyName assemblyName)
185185
try
186186
{
187187
assembly = _assemblyLoadContext?.LoadFromAssemblyName(assemblyName)!;
188-
if (assembly == null)
188+
if (assembly is null)
189189
throw new Exception("LoadFromAssemblyName returned null");
190190
}
191191
catch (Exception e)
@@ -227,7 +227,7 @@ private object ExecuteMethod(string methodName, Type[] ptypes, params object?[]
227227

228228
private object ExecuteMethod(MethodInfo? method, params object?[] args)
229229
{
230-
if (method == null)
230+
if (method is null)
231231
throw new NUnitEngineException(INVALID_FRAMEWORK_MESSAGE);
232232

233233
log.Debug($"Executing {method.DeclaringType}.{method.Name}");

src/NUnitCommon/nunit.agent.core/Drivers/NUnitFrameworkDriver.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public string Load(string testAssemblyPath, IDictionary<string, object> settings
133133
/// <returns>An Xml string representing the result</returns>
134134
public string Run(ITestEventListener? listener, string filter)
135135
{
136-
return _api.Run(listener != null ? new EventInterceptor(listener) : null, filter);
136+
return _api.Run(listener is not null ? new EventInterceptor(listener) : null, filter);
137137
}
138138

139139
/// <summary>

src/NUnitCommon/nunit.agent.core/Runners/DomainDetailsBuilder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ internal static class DomainDetailsBuilder
2323
public static string DetailsFor(AppDomain domain, string? errMsg = null)
2424
{
2525
var sb = new StringBuilder();
26-
if (errMsg != null)
26+
if (errMsg is not null)
2727
sb.AppendLine(errMsg);
2828

2929
try

src/NUnitCommon/nunit.agent.core/Runners/DomainManager.cs

+9-9
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public AppDomain CreateDomain(TestPackage package)
3434
AppDomainSetup setup = CreateAppDomainSetup(package);
3535

3636
string hashCode = string.Empty;
37-
if (package.Name != null)
37+
if (package.Name is not null)
3838
{
3939
hashCode = package.Name.GetHashCode().ToString("x") + "-";
4040
}
@@ -80,7 +80,7 @@ private AppDomainSetup CreateAppDomainSetup(TestPackage package)
8080
// .NET versions greater than v4.0 report as v4.0, so look at
8181
// the TargetFrameworkAttribute on the assembly if it exists
8282
// If property is null, .NET 4.5+ is not installed, so there is no need
83-
if (TargetFrameworkNameProperty != null)
83+
if (TargetFrameworkNameProperty is not null)
8484
{
8585
var frameworkName = package.GetSetting(EnginePackageSettings.ImageTargetFrameworkName, string.Empty);
8686
if (frameworkName != string.Empty)
@@ -133,7 +133,7 @@ public void Unload()
133133
throw new NUnitEngineUnloadException(msg);
134134
}
135135

136-
if (_unloadException != null)
136+
if (_unloadException is not null)
137137
throw new NUnitEngineUnloadException("Exception encountered unloading application domain", _unloadException);
138138
}
139139

@@ -231,7 +231,7 @@ private static bool IsExecutable(string? fileName)
231231
var assemblies = new List<string>();
232232

233233
// All subpackages have full names, but this is a public method in a public class so we have no control.
234-
foreach (var package in packages.Where(p => p.FullName != null))
234+
foreach (var package in packages.Where(p => p.FullName is not null))
235235
assemblies.Add(package.FullName!);
236236

237237
return GetCommonAppBase(assemblies);
@@ -244,10 +244,10 @@ private static bool IsExecutable(string? fileName)
244244
foreach (string assembly in assemblies)
245245
{
246246
string? dir = Path.GetDirectoryName(Path.GetFullPath(assembly))!;
247-
if (commonBase == null)
247+
if (commonBase is null)
248248
commonBase = dir;
249249
else
250-
while (commonBase != null && !PathUtils.SamePathOrUnder(commonBase, dir))
250+
while (commonBase is not null && !PathUtils.SamePathOrUnder(commonBase, dir))
251251
commonBase = Path.GetDirectoryName(commonBase)!;
252252
}
253253

@@ -266,7 +266,7 @@ private static bool IsExecutable(string? fileName)
266266
if (package.GetSetting(EnginePackageSettings.AutoBinPath, binPath == string.Empty))
267267
binPath = package.SubPackages.Count > 0
268268
? GetPrivateBinPath(appBase, package.SubPackages)
269-
: package.FullName != null
269+
: package.FullName is not null
270270
? GetPrivateBinPath(appBase, package.FullName)
271271
: null;
272272

@@ -276,7 +276,7 @@ private static bool IsExecutable(string? fileName)
276276
public static string? GetPrivateBinPath(string basePath, IList<TestPackage> packages)
277277
{
278278
var assemblies = new List<string>();
279-
foreach (var package in packages.Where(p => p.FullName != null))
279+
foreach (var package in packages.Where(p => p.FullName is not null))
280280
assemblies.Add(package.FullName!);
281281

282282
return GetPrivateBinPath(basePath, assemblies);
@@ -292,7 +292,7 @@ private static bool IsExecutable(string? fileName)
292292
string? dir = PathUtils.RelativePath(
293293
Path.GetFullPath(basePath),
294294
Path.GetDirectoryName(Path.GetFullPath(assembly))!);
295-
if (dir != null && dir != string.Empty && dir != "." && !dirList.Contains(dir))
295+
if (dir is not null && dir != string.Empty && dir != "." && !dirList.Contains(dir))
296296
{
297297
dirList.Add(dir);
298298
if (sb.Length > 0)

src/NUnitCommon/nunit.agent.core/Runners/TestAgentRunner.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public abstract class TestAgentRunner : ITestEngineRunner
4242
/// </summary>
4343
public bool IsPackageLoaded
4444
{
45-
get { return LoadResult != null; }
45+
get { return LoadResult is not null; }
4646
}
4747

4848
public TestAgentRunner(TestPackage package)
@@ -89,23 +89,23 @@ public TestEngineResult Explore(TestFilter filter)
8989
/// <returns>A TestEngineResult.</returns>
9090
public virtual TestEngineResult Load()
9191
{
92-
Guard.OperationValid(TestDomain != null, "TestDomain is not set");
92+
Guard.OperationValid(TestDomain is not null, "TestDomain is not set");
9393

9494
var result = new TestEngineResult();
9595

9696
// The TestAgentRunner constructor guarantees that TestPackage has
9797
// only a single assembly.
9898
var assemblyPackage = TestPackage.Select(p => !p.HasSubPackages()).First();
9999

100-
if (DriverService == null)
100+
if (DriverService is null)
101101
DriverService = new DriverService();
102102

103103
var testFile = assemblyPackage.FullName!; // We know it's an assembly
104104

105105
string? targetFramework = assemblyPackage.GetSetting(EnginePackageSettings.ImageTargetFrameworkName, (string?)null);
106106
bool skipNonTestAssemblies = assemblyPackage.GetSetting(EnginePackageSettings.SkipNonTestAssemblies, false);
107107

108-
if (_assemblyResolver != null && !TestDomain.IsDefaultAppDomain()
108+
if (_assemblyResolver is not null && !TestDomain.IsDefaultAppDomain()
109109
&& assemblyPackage.GetSetting(EnginePackageSettings.ImageRequiresDefaultAppDomainAssemblyResolver, false))
110110
{
111111
// It's OK to do this in the loop because the Add method

src/NUnitCommon/nunit.agent.core/Runners/TestDomainRunner.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public override TestEngineResult Load()
2828
/// </summary>
2929
public override void Unload()
3030
{
31-
if (this.TestDomain != null)
31+
if (this.TestDomain is not null)
3232
{
3333
_domainManager.Unload(this.TestDomain);
3434
this.TestDomain = null;

src/NUnitCommon/nunit.agent.core/TestAssemblyLoadContext.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public TestAssemblyLoadContext(string testAssemblyPath)
3434
log.Debug("Loading {0} assembly", name);
3535

3636
var loadedAssembly = base.Load(name);
37-
if (loadedAssembly != null)
37+
if (loadedAssembly is not null)
3838
{
3939
log.Info("Assembly {0} ({1}) is loaded using default base.Load()", name, GetAssemblyLocationInfo(loadedAssembly));
4040
return loadedAssembly;
@@ -47,14 +47,14 @@ public TestAssemblyLoadContext(string testAssemblyPath)
4747
loadedAssembly = LoadFromAssemblyPath(runtimeResolverPath);
4848
}
4949

50-
if (loadedAssembly != null)
50+
if (loadedAssembly is not null)
5151
{
5252
log.Info("Assembly {0} ({1}) is loaded using the deps.json info", name, GetAssemblyLocationInfo(loadedAssembly));
5353
return loadedAssembly;
5454
}
5555

5656
loadedAssembly = _resolver.Resolve(this, name);
57-
if (loadedAssembly != null)
57+
if (loadedAssembly is not null)
5858
{
5959
log.Info("Assembly {0} ({1}) is loaded using the TestAssembliesResolver", name, GetAssemblyLocationInfo(loadedAssembly));
6060

@@ -71,7 +71,7 @@ public TestAssemblyLoadContext(string testAssemblyPath)
7171
loadedAssembly = LoadFromAssemblyPath(assemblyPath);
7272
}
7373

74-
if (loadedAssembly != null)
74+
if (loadedAssembly is not null)
7575
{
7676
log.Info("Assembly {0} ({1}) is loaded using base path", name, GetAssemblyLocationInfo(loadedAssembly));
7777
return loadedAssembly;

0 commit comments

Comments
 (0)