Skip to content
Permalink
Browse files

Don't show nullability analysis for value types or constant values (#…

  • Loading branch information...
ryzngard committed Aug 14, 2019
1 parent 0f2224a commit 1cc7305dab3d72f5091494d34c27dc895315bbe7
@@ -6611,5 +6611,45 @@ void N()
MainDescription($"({FeaturesResources.local_variable}) string s"),
NullabilityAnalysis(""));
}

[Fact, Trait(Traits.Feature, Traits.Features.QuickInfo)]
public async Task NullableNotShownForValueType()
{
await TestWithOptionsAsync(TestOptions.Regular8,
@"#nullable enable
using System.Collections.Generic;
class X
{
void N()
{
int a = 0;
int b = $$a;
}
}",
MainDescription($"({FeaturesResources.local_variable}) int a"),
NullabilityAnalysis(""));
}

[Fact, Trait(Traits.Feature, Traits.Features.QuickInfo)]
public async Task NullableNotShownForConst()
{
await TestWithOptionsAsync(TestOptions.Regular8,
@"#nullable enable
using System.Collections.Generic;
class X
{
void N()
{
const string? s = null;
string? s2 = $$s;
}
}",
MainDescription($"({FeaturesResources.local_constant}) string? s = null"),
NullabilityAnalysis(""));
}
}
}
@@ -3,10 +3,10 @@
using System.Collections.Immutable;
using System.Composition;
using System.Threading;
using System.Linq;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.QuickInfo;
using Microsoft.CodeAnalysis.Utilities;

namespace Microsoft.CodeAnalysis.CSharp.QuickInfo
{
@@ -76,18 +76,35 @@ protected override ImmutableArray<TaggedText> TryGetNullabilityAnalysis(Workspac
// Although GetTypeInfo can return nullability for uses of all sorts of things, it's not always useful for quick info.
// For example, if you have a call to a method with a nullable return, the fact it can be null is already captured
// in the return type shown -- there's no flow analysis information there.
if (symbolInfo.Symbol.Kind != SymbolKind.Event &&
symbolInfo.Symbol.Kind != SymbolKind.Field &&
symbolInfo.Symbol.Kind != SymbolKind.Local &&
symbolInfo.Symbol.Kind != SymbolKind.Parameter &&
symbolInfo.Symbol.Kind != SymbolKind.Property &&
symbolInfo.Symbol.Kind != SymbolKind.RangeVariable)
switch (symbolInfo.Symbol)
{
return default;
// Ignore constant values for nullability flow state
case IFieldSymbol { HasConstantValue: true }: return default;
case ILocalSymbol { HasConstantValue: true }: return default;

// Symbols with useful quick info
case IFieldSymbol _:
case ILocalSymbol _:
case IParameterSymbol _:
case IPropertySymbol _:
case IRangeVariableSymbol _:
break;

default:
return default;
}

var typeInfo = semanticModel.GetTypeInfo(bindableParent, cancellationToken);

// Nullability is a reference type only feature, value types can use
// something like "int?" to be nullable but that ends up encasing as
// Nullable<int>, which isn't exactly the same. To avoid confusion and
// extra noise, we won't show nullable flow state for value types
if (typeInfo.Type?.IsValueType == true)
{
return default;
}

string messageTemplate = null;

if (typeInfo.Nullability.FlowState == NullableFlowState.NotNull)

0 comments on commit 1cc7305

Please sign in to comment.
You can’t perform that action at this time.