From f67fb25978eebcff0f355d6f612ea4d7e2189c7b Mon Sep 17 00:00:00 2001 From: Martin Strecker Date: Mon, 15 Jan 2018 10:19:13 +0100 Subject: [PATCH] Removed generic constraint check. Changed C# null check from (v is null) to (v == null). --- .../UseIsNullCheck/UseIsNullCheckTests.cs | 33 ++++++++++++------- .../CSharpUseIsNullCheckCodeFixProvider.cs | 12 ++++--- .../AbstractUseIsNullDiagnosticAnalyzer.cs | 21 ------------ 3 files changed, 30 insertions(+), 36 deletions(-) diff --git a/src/EditorFeatures/CSharpTest/UseIsNullCheck/UseIsNullCheckTests.cs b/src/EditorFeatures/CSharpTest/UseIsNullCheck/UseIsNullCheckTests.cs index 7b05727d9fc..65ce66c37e5 100644 --- a/src/EditorFeatures/CSharpTest/UseIsNullCheck/UseIsNullCheckTests.cs +++ b/src/EditorFeatures/CSharpTest/UseIsNullCheck/UseIsNullCheckTests.cs @@ -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 value, string parameterName) } } } +", @" +class C +{ + public static void NotNull(T value, string parameterName) + { + if (value == null) + { + throw new System.ArgumentNullException(parameterName); + } + } +} "); } @@ -259,7 +270,7 @@ class C { public static void NotNull(T value, string parameterName) where T:class { - if (value is null) + if (value == null) { throw new System.ArgumentNullException(parameterName); } diff --git a/src/Features/CSharp/Portable/UseIsNullCheck/CSharpUseIsNullCheckCodeFixProvider.cs b/src/Features/CSharp/Portable/UseIsNullCheck/CSharpUseIsNullCheckCodeFixProvider.cs index dda13baacae..767d58e0795 100644 --- a/src/Features/CSharp/Portable/UseIsNullCheck/CSharpUseIsNullCheckCodeFixProvider.cs +++ b/src/Features/CSharp/Portable/UseIsNullCheck/CSharpUseIsNullCheckCodeFixProvider.cs @@ -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); } } diff --git a/src/Features/Core/Portable/UseIsNullCheck/AbstractUseIsNullDiagnosticAnalyzer.cs b/src/Features/Core/Portable/UseIsNullCheck/AbstractUseIsNullDiagnosticAnalyzer.cs index e0464a9faef..cebf9a6ddc8 100644 --- a/src/Features/Core/Portable/UseIsNullCheck/AbstractUseIsNullDiagnosticAnalyzer.cs +++ b/src/Features/Core/Portable/UseIsNullCheck/AbstractUseIsNullDiagnosticAnalyzer.cs @@ -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.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)); -- GitLab