From 4ded7f5caed181c59e9b5b8eaff1a81bceb524c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20Ron=C4=8Devi=C4=87?= Date: Thu, 13 Jun 2019 07:53:04 +0430 Subject: [PATCH] feat: Ignores declare field as nullable on structs --- ...AndDeclareReferenceIdentifierAsNullableAnalyzer.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Sharpen.Engine/SharpenSuggestions/CSharp80/NullableReferenceTypes/Analyzers/EnableNullableContextAndDeclareReferenceIdentifierAsNullableAnalyzer.cs b/src/Sharpen.Engine/SharpenSuggestions/CSharp80/NullableReferenceTypes/Analyzers/EnableNullableContextAndDeclareReferenceIdentifierAsNullableAnalyzer.cs index 56952a0..8f52df1 100644 --- a/src/Sharpen.Engine/SharpenSuggestions/CSharp80/NullableReferenceTypes/Analyzers/EnableNullableContextAndDeclareReferenceIdentifierAsNullableAnalyzer.cs +++ b/src/Sharpen.Engine/SharpenSuggestions/CSharp80/NullableReferenceTypes/Analyzers/EnableNullableContextAndDeclareReferenceIdentifierAsNullableAnalyzer.cs @@ -107,6 +107,8 @@ bool NullableContextCanBeEnabledForIdentifier((ISymbol symbol, SyntaxNode declar return false; var typeSymbol = GetTypeSymbol(); + // WARNING: A bit paranoid but still. Never remove this check because + // the GetSuggestion() later on assumes that the type symbol exists. if (typeSymbol == null) return false; // For this, we want to have a special suggestion @@ -198,9 +200,16 @@ variableDeclarator.Parent is VariableDeclarationSyntax variableDeclaration && ISharpenSuggestion GetSuggestion(ISymbol symbol) { + // The type surely exists at this point on all the symbols. switch (symbol) { - case IFieldSymbol _: return EnableNullableContextAndDeclareFieldAsNullable.Instance; + case IFieldSymbol fieldSymbol: + if (fieldSymbol.Type.IsReferenceType) + return EnableNullableContextAndDeclareFieldAsNullable.Instance; + // TODO-IG: Just ignoring structs so far. + // They are rarely used anyway compared to classes. + // Still, see what to do with structs. + return null; case IPropertySymbol _: return EnableNullableContextAndDeclareReferencePropertyAsNullable.Instance; case IParameterSymbol _: return EnableNullableContextAndDeclareReferenceParameterAsNullable.Instance; case ILocalSymbol _: return EnableNullableContextAndDeclareReferenceVariableAsNullable.Instance;