Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes SonarQube's "Null checks should not be used with 'is'" / Code Smell #1059

Merged
merged 2 commits into from
May 27, 2021

Conversation

marodev
Copy link
Contributor

@marodev marodev commented May 20, 2021

Hello,

I am aware that there is no open issue for this kind of contribution. However, this pull request is very simple and aims to reduce the technical debt. It specifically targets the "Null checks should not be used with 'is'" rule reported by SonarQube.

Description of Change

  • Fixes SonarQube's "Null checks should not be used with 'is'" / Code Smell / Minor

PR Checklist

  • Targets the correct branch
  • Tests are passing (or failures are unrelated)

@@ -18,7 +18,7 @@ public static class TabIndexExtensions
countChildrenWithTabStopWithoutThis = 0;

Element parentPage = (element as Element)?.Parent;
while (parentPage != null && !(parentPage is Page))
while (parentPage is not Page)
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this actually do the same thing as before?

parentPage != null && !(parentPage is Page)

would be false when parentPage is null

parentPage is not Page

Would this still be false is parentPage is null?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi Oliver, thanks for the question.

It would still be false if parentPage is null.

A simple example:

string s = null;
if (s is not string)
{
    Console.WriteLine("It's NOT a string.");
}
else
{
    Console.WriteLine("It's a string.");
}

prints It's NOT a string. to the console.

Copy link
Contributor

Choose a reason for hiding this comment

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

I understand what the is not is doing, but the code you're replacing is not quite doing that. It's checking if it is not a Page and is also not null:

		object myString = "Hello";
		var result1 = (myString != null && !(myString is string));
		
		myString = null;
		var result2 = (myString is not string);	
		
		System.Console.WriteLine(result1);
		System.Console.WriteLine(result2);

result1 is false
result2 is true

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for this! I reverted to the previous != null && !(...) pattern

@rmarinho
Copy link
Member

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@rmarinho rmarinho merged commit 8a91f40 into dotnet:main May 27, 2021
lytico pushed a commit to lytico/maui that referenced this pull request Jun 8, 2021
…mell (dotnet#1059)

* Fix SonarQube's "Null checks should not be used with 'is'" / Code Smell

* Revert wrongly refactored "Null checks should not be used with 'is'"
@github-actions github-actions bot locked and limited conversation to collaborators Dec 26, 2023
@samhouts samhouts added the fixed-in-6.0.100-preview.5 Look for this fix in 6.0.100-preview.5! label Aug 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
fixed-in-6.0.100-preview.5 Look for this fix in 6.0.100-preview.5! layout-grid
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants