Skip to content

Consistently use 'is null' and 'is not null' iso '== null' and '!= null'#4379

Merged
manfred-brands merged 2 commits into
nunit:masterfrom
manfred-brands:UseIsNull
May 6, 2023
Merged

Consistently use 'is null' and 'is not null' iso '== null' and '!= null'#4379
manfred-brands merged 2 commits into
nunit:masterfrom
manfred-brands:UseIsNull

Conversation

@manfred-brands

Copy link
Copy Markdown
Member

Fixes #4378

@mikkelbu mikkelbu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for doing this @manfred-brands. I only have some nit-picks (mostly relating to the formatting of ternary expressions after the update) - otherwise it looks good.

Comment thread src/NUnitFramework/Directory.Build.props
return 0;

if (x == null || y == null)
if (x is null || y is null)

@mikkelbu mikkelbu May 5, 2023

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to your change, but this is an odd (read: wrong) IComparer

static readonly RuntimeType currentRuntime =
Type.GetType("Mono.Runtime", false) != null
? RuntimeType.Mono
Type.GetType("Mono.Runtime", false) is not null ? RuntimeType.Mono

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Type.GetType("Mono.Runtime", false) is not null ? RuntimeType.Mono
Type.GetType("Mono.Runtime", false) is not null
? RuntimeType.Mono


var testFile = _testAssembly != null
? AssemblyHelper.GetAssemblyPath(_testAssembly)
var testFile = _testAssembly is not null ? AssemblyHelper.GetAssemblyPath(_testAssembly)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var testFile = _testAssembly is not null ? AssemblyHelper.GetAssemblyPath(_testAssembly)
var testFile = _testAssembly is not null
? AssemblyHelper.GetAssemblyPath(_testAssembly)

? _testAssembly.GetName().Name
: _options.InputFile != null
? Path.GetFileNameWithoutExtension(_options.InputFile)
var baseName = _testAssembly is not null ? _testAssembly.GetName().Name

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should keep the existing formatting

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I certainly agree, it seems the CodeFix from the Analyzer looses trailing trivia. I will create an issue (and PR) for it: AArnott/CSharpIsNull#45

{
string display = arg == null
? "null"
string display = arg is null ? "null"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
string display = arg is null ? "null"
string display = arg is null
? "null"

{
return Assembly != null
? Assembly.GetAttributes<TAttr>()
return Assembly is not null ? Assembly.GetAttributes<TAttr>()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return Assembly is not null ? Assembly.GetAttributes<TAttr>()
return Assembly is not null
? Assembly.GetAttributes<TAttr>()

{
Event? e = _events.Dequeue( PumpState == EventPumpState.Pumping );
if ( e == null )
if ( e is null)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if ( e is null)
if (e is null)


return tail != null
? ApplySelection(resultNodes, tail)
return tail is not null ? ApplySelection(resultNodes, tail)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return tail is not null ? ApplySelection(resultNodes, tail)
return tail is not null
? ApplySelection(resultNodes, tail)

// 2. User provided an ITestCaseData and we just use it.
ITestCaseData? parms = item == null
? new TestCaseParameters(new object?[] { null })
ITestCaseData? parms = item is null ? new TestCaseParameters(new object?[] { null })

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ITestCaseData? parms = item is null ? new TestCaseParameters(new object?[] { null })
ITestCaseData? parms = item is null
? new TestCaseParameters(new object?[] { null })

@manfred-brands

Copy link
Copy Markdown
Member Author

@mikkelbu I have fixed the CSharpIsNullAnalyzer CodeFix (locally) and re-applied the codefix. Now the existing formatting has been kept.
Once the fix is officially released we can update our reference.

@manfred-brands manfred-brands merged commit 8a56c85 into nunit:master May 6, 2023
@manfred-brands manfred-brands deleted the UseIsNull branch May 6, 2023 09:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Update code base to use 'is (not) null' consistently

2 participants