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

Enable nullable reference types for the repo #3021

Merged
merged 30 commits into from Nov 26, 2019

Conversation

mavasani
Copy link
Member

@mavasani mavasani commented Nov 8, 2019

  1. I have broken down the changes to each analyzer package in different commits.
  2. I haven't enable nullable for test projects in this PR
  3. I have added few suppressions or nullable disable at source file level for bunch of security analyzers. @dotpaul will address these in a follow-up PR. You can search for #pramga warning disable CS or comments with TODO(dotpaul): Enable nullable analysis.

NOTE: PR targets 2.9.x branch. Once it flows into master, I will add remaining annotations in new code in master.

@@ -61,6 +61,7 @@ public override void Initialize(AnalysisContext analysisContext)
var lastField = fieldInitializer?.InitializedFields.LastOrDefault();
var fieldInitializerValue = fieldInitializer?.Value;
if (fieldInitializerValue == null ||
lastField == null ||
Copy link
Member

Choose a reason for hiding this comment

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

💭 Could this one have been a bug? Maybe try to add a test to hit this case?

Copy link
Member Author

Choose a reason for hiding this comment

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

Avoiding adding unit tests in this PR. I will leave this comment unresolved and file a followup bug to track any such cases.

@dotpaul
Copy link
Contributor

dotpaul commented Nov 8, 2019

                                    && !sslProtocolsSymbol.Equals(variableInitializerOperation.Value.Type))

Change this to:

if (variableInitializerOperation.Value == null
    || !sslProtocolsSymbol.Equals(variableInitializerOperation.Value.Type))

and then can get rid of the ? for valueOperation.

(I think that makes more sense anyway. Thanks nullable reference!)


Refers to: src/Microsoft.NetCore.Analyzers/Core/Security/SslProtocolsAnalyzer.cs:127 in 64aa452. [](commit_id = 64aa452, deletion_comment = False)

Copy link
Contributor

@dotpaul dotpaul left a comment

Choose a reason for hiding this comment

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

🚢 security analyzers look fine to me

Copy link
Member

@sharwell sharwell left a comment

Choose a reason for hiding this comment

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

📝 Now at 212/322 files viewed

src/Utilities/Compiler/RoslynString.cs Show resolved Hide resolved
src/Utilities/Compiler/Extensions/ISymbolExtensions.cs Outdated Show resolved Hide resolved
internal static SyntaxNode CreateOrdinalMemberAccess(SyntaxGenerator generator, SemanticModel model)
{
INamedTypeSymbol stringComparisonType = model.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemStringComparison);
INamedTypeSymbol stringComparisonType = model.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemStringComparison)!;
Copy link
Member

Choose a reason for hiding this comment

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

💭 This is a fascinating case. We have several situations where the code fix relies on the analyzer behavior to ensure null will not be encountered. No request for a change here but eventually interested in seeing if the situation could be improved.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, totally agree. We need a follow-up PR to clean this up across the repo.

Copy link
Member

@sharwell sharwell left a comment

Choose a reason for hiding this comment

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

Now up to 248/322.

@@ -144,7 +144,7 @@ void AnalyzeArgument(IParameterSymbol parameter, IPropertySymbol containingPrope
}

// FxCop compat: Filter out xml string literals.
var filteredStrings = stringLiteralValues.Where(literal => !LooksLikeXmlTag(literal));
IEnumerable<string> filteredStrings = stringLiteralValues.Where(literal => literal != null && !LooksLikeXmlTag(literal))!;
Copy link
Member

Choose a reason for hiding this comment

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

💭 Was the lack of a null check here a bug in the analyzer? Any idea how we might have hit that case?

Not a big fan of the suppression here, but WhereNotNull would be an additional allocation. Perhaps we're on a code path where it doesn't matter anyway so I'll leave the suggestion here:

Suggested change
IEnumerable<string> filteredStrings = stringLiteralValues.Where(literal => literal != null && !LooksLikeXmlTag(literal))!;
IEnumerable<string> filteredStrings = stringLiteralValues.WhereNotNull().Where(literal => !LooksLikeXmlTag(literal));

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep, it was a bug. I will keep this comment unresolved so we can track adding a regression test for this.

if (variableInitializerOperation.Value != null
&& !sslProtocolsSymbol.Equals(variableInitializerOperation.Value.Type))
if (variableInitializerOperation.Value == null
|| !sslProtocolsSymbol.Equals(variableInitializerOperation.Value.Type))
Copy link
Member

Choose a reason for hiding this comment

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

😕 Was this a bug? Should we have a test covering this condition?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, will add this comment to set of items that needs a regression test.

src/Utilities/Compiler/WellKnownTypeProvider.cs Outdated Show resolved Hide resolved
src/Utilities/Compiler/PooledObjects/ArrayBuilder.cs Outdated Show resolved Hide resolved
src/Utilities/Compiler/PooledObjects/ArrayBuilder.cs Outdated Show resolved Hide resolved
src/Utilities/Compiler/PooledObjects/ArrayBuilder.cs Outdated Show resolved Hide resolved
src/Utilities/Compiler/PooledObjects/ArrayBuilder.cs Outdated Show resolved Hide resolved
@mavasani mavasani requested a review from a team as a code owner November 16, 2019 00:45
@mavasani
Copy link
Member Author

Ah, I accidentally fetched in sources from master :(. I will clean this up and update the PR over the weekend.

@mavasani
Copy link
Member Author

The branch is now back to sane state. @sharwell Please let me know if you have any more feedback before I merge the PR. I will file a separate work item for adding tests for unresolved comments.

@mavasani
Copy link
Member Author

@sharwell Any concerns in me merging the PR?

@dotpaul I presume you want me to wait for #3050 to go in before this PR is merged, so that 2.9.8 release is unaffected by this change?

@dotpaul
Copy link
Contributor

dotpaul commented Nov 18, 2019

@dotpaul I presume you want me to wait for #3050 to go in before this PR is merged, so that 2.9.8 release is unaffected by this change?

Yes please.

{
SemanticModel model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
if (model.GetSymbolInfo((IdentifierNameSyntax)identifier, cancellationToken).Symbol is IMethodSymbol methodSymbol && CanAddStringComparison(methodSymbol, model))
if (model.GetSymbolInfo((IdentifierNameSyntax)identifier!, cancellationToken).Symbol is IMethodSymbol methodSymbol && CanAddStringComparison(methodSymbol, model))
Copy link
Member

Choose a reason for hiding this comment

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

📝 This suppression is not supposed to be needed. If it's not compiling for you with this removed, a compiler bug should be filed.

@mavasani
Copy link
Member Author

@dotnet-bot retest this please

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.

None yet

3 participants