Skip to content

Commit 4458655

Browse files
authored
Merge pull request #6797 from mavasani/Issue6164
Handle default expression value properly in Value content analysis
2 parents e809312 + cbc9012 commit 4458655

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

src/Utilities/Compiler/Extensions/ITypeSymbolExtensions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,13 @@ public static bool IsAttribute(this ITypeSymbol symbol)
294294
public static bool HasValueCopySemantics(this ITypeSymbol typeSymbol)
295295
=> typeSymbol.IsValueType || typeSymbol.SpecialType == SpecialType.System_String;
296296

297+
#if !MICROSOFT_CODEANALYSIS_PUBLIC_API_ANALYZERS
298+
public static bool CanHoldNullValue([NotNullWhen(returnValue: true)] this ITypeSymbol? typeSymbol)
299+
=> typeSymbol.IsReferenceTypeOrNullableValueType() ||
300+
typeSymbol?.IsRefLikeType == true ||
301+
typeSymbol is ITypeParameterSymbol typeParameter && !typeParameter.IsValueType;
302+
#endif
303+
297304
public static bool IsNonNullableValueType([NotNullWhen(returnValue: true)] this ITypeSymbol? typeSymbol)
298305
=> typeSymbol != null && typeSymbol.IsValueType && typeSymbol.OriginalDefinition.SpecialType != SpecialType.System_Nullable_T;
299306

src/Utilities/FlowAnalysis/FlowAnalysis/Analysis/PointsToAnalysis/PointsToAnalysis.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,7 @@ private PointsToAnalysis(PointsToAnalysisDomain analysisDomain, PointsToDataFlow
9090
}
9191

9292
internal static bool ShouldBeTracked([NotNullWhen(returnValue: true)] ITypeSymbol? typeSymbol, Func<ITypeSymbol?, bool> isDisposable)
93-
=> typeSymbol.IsReferenceTypeOrNullableValueType() ||
94-
typeSymbol?.IsRefLikeType == true ||
95-
typeSymbol is ITypeParameterSymbol typeParameter && !typeParameter.IsValueType ||
93+
=> typeSymbol.CanHoldNullValue() ||
9694
isDisposable(typeSymbol);
9795

9896
internal static bool ShouldBeTracked(AnalysisEntity analysisEntity, PointsToAnalysisKind pointsToAnalysisKind, Func<ITypeSymbol?, bool> isDisposable)

src/Utilities/FlowAnalysis/FlowAnalysis/Analysis/ValueContentAnalysis/ValueContentAnalysis.ValueContentDataFlowOperationVisitor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Collections.Generic;
55
using System.Collections.Immutable;
6+
using Analyzer.Utilities.Extensions;
67
using Microsoft.CodeAnalysis.FlowAnalysis.DataFlow.CopyAnalysis;
78
using Microsoft.CodeAnalysis.Operations;
89

@@ -62,7 +63,7 @@ protected override ValueContentAbstractValue GetAbstractValue(AnalysisEntity ana
6263
=> CurrentAnalysisData.TryGetValue(analysisEntity, out var value) ? value : ValueDomain.UnknownOrMayBeValue;
6364

6465
protected override ValueContentAbstractValue GetAbstractDefaultValue(ITypeSymbol? type)
65-
=> type != null ?
66+
=> type != null && !type.CanHoldNullValue() ?
6667
ValueContentAbstractValue.DoesNotContainLiteralOrNonLiteralState :
6768
ValueContentAbstractValue.ContainsNullLiteralState;
6869

0 commit comments

Comments
 (0)