-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Fixes SonarQube's "Null checks should not be used with 'is'" / Code Smell #1059
Conversation
@@ -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) |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
/azp run |
Azure Pipelines successfully started running 1 pipeline(s). |
…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'"
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
PR Checklist