提交 f67fb259 编写于 作者: M Martin Strecker 提交者: Sam Harwell

Removed generic constraint check. Changed C# null check from (v is null) to (v == null).

上级 2e2cbb6c
......@@ -36,7 +36,7 @@ class C
{
void M(string s)
{
if (s is null)
if (s == null)
return;
}
}");
......@@ -62,7 +62,7 @@ class C
{
void M(string s)
{
if (s is null)
if (s == null)
return;
}
}");
......@@ -88,7 +88,7 @@ class C
{
void M(string s)
{
if (s is null)
if (s == null)
return;
}
}");
......@@ -114,7 +114,7 @@ class C
{
void M(string s)
{
if (s is null)
if (s == null)
return;
}
}");
......@@ -140,7 +140,7 @@ class C
{
void M(string s)
{
if (!(s is null))
if (s != null)
return;
}
}");
......@@ -183,8 +183,8 @@ class C
{
void M(string s1, string s2)
{
if (s1 is null ||
s2 is null)
if (s1 == null ||
s2 == null)
return;
}
}");
......@@ -211,8 +211,8 @@ class C
{
void M(string s1, string s2)
{
if (s1 is null ||
s2 is null)
if (s1 == null ||
s2 == null)
return;
}
}");
......@@ -222,7 +222,7 @@ void M(string s1, string s2)
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseIsNullCheck)]
public async Task TestMissingIfValueParameterTypeIsUnconstraintGeneric()
{
await TestMissingAsync(
await TestInRegularAndScriptAsync(
@"
class C
{
......@@ -234,6 +234,17 @@ public static void NotNull<T>(T value, string parameterName)
}
}
}
", @"
class C
{
public static void NotNull<T>(T value, string parameterName)
{
if (value == null)
{
throw new System.ArgumentNullException(parameterName);
}
}
}
");
}
......@@ -259,7 +270,7 @@ class C
{
public static void NotNull<T>(T value, string parameterName) where T:class
{
if (value is null)
if (value == null)
{
throw new System.ArgumentNullException(parameterName);
}
......
......@@ -17,12 +17,16 @@ protected override string GetIsNullTitle()
protected override string GetIsNotNullTitle()
=> GetIsNullTitle();
protected override SyntaxNode CreateIsNullCheck(SyntaxNode argument)
=> SyntaxFactory.IsPatternExpression(
private static SyntaxNode CreateNullCheck(SyntaxNode argument, SyntaxKind comparisonOperator)
=> SyntaxFactory.BinaryExpression(
comparisonOperator,
(ExpressionSyntax)argument,
SyntaxFactory.ConstantPattern(SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression))).Parenthesize();
SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression)).Parenthesize();
protected override SyntaxNode CreateIsNullCheck(SyntaxNode argument)
=> CreateNullCheck(argument, SyntaxKind.EqualsExpression);
protected override SyntaxNode CreateIsNotNullCheck(SyntaxNode notExpression, SyntaxNode argument)
=> ((PrefixUnaryExpressionSyntax)notExpression).WithOperand((ExpressionSyntax)CreateIsNullCheck(argument));
=> CreateNullCheck(argument, SyntaxKind.NotEqualsExpression);
}
}
......@@ -109,11 +109,6 @@ private void AnalyzeSyntax(SyntaxNodeAnalysisContext context, IMethodSymbol refe
return;
}
if (HasUnconstraintGenericParameter(syntaxFacts, semanticModel, arguments[0], arguments[1], cancellationToken))
{
return;
}
var additionalLocations = ImmutableArray.Create(invocation.GetLocation());
var properties = ImmutableDictionary<string, string>.Empty;
......@@ -130,22 +125,6 @@ private void AnalyzeSyntax(SyntaxNodeAnalysisContext context, IMethodSymbol refe
additionalLocations, properties));
}
private static bool HasUnconstraintGenericParameter(ISyntaxFactsService syntaxFacts, SemanticModel semanticModel, SyntaxNode node1, SyntaxNode node2, CancellationToken cancellationToken)
{
var valueNode = syntaxFacts.IsNullLiteralExpression(node1) ? node2 : node1;
var argumentExpression = syntaxFacts.GetExpressionOfArgument(valueNode);
if (argumentExpression != null)
{
var parameterType = semanticModel.GetTypeInfo(argumentExpression, cancellationToken).Type;
if (parameterType is ITypeParameterSymbol typeParameter)
{
return !typeParameter.HasReferenceTypeConstraint;
}
}
return false;
}
private static bool MatchesPattern(ISyntaxFactsService syntaxFacts, SyntaxNode node1, SyntaxNode node2)
=> syntaxFacts.IsNullLiteralExpression(syntaxFacts.GetExpressionOfArgument(node1)) &&
!syntaxFacts.IsNullLiteralExpression(syntaxFacts.GetExpressionOfArgument(node2));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册