Skip to content

Commit

Permalink
Fix NullReferenceException in CodeContractFromBaseAnalyzer. Add more …
Browse files Browse the repository at this point in the history
…patterns to SmartNotExpression method
  • Loading branch information
ikopylov committed Oct 28, 2019
1 parent 447c466 commit 8cf136e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="ContractFix.ca38bdda-3bf6-437e-823a-d9214656f65f" Version="1.0" Language="en-US" Publisher="Qoollo"/>
<Identity Id="ContractFix.ca38bdda-3bf6-437e-823a-d9214656f65f" Version="1.0.1" Language="en-US" Publisher="Qoollo"/>
<DisplayName>ContractFix</DisplayName>
<Description xml:space="preserve">CodeContracts remover</Description>
</Metadata>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public override void Initialize(AnalysisContext context)
private static IEnumerable<IMethodSymbol> GetInterfaceImplementation(IMethodSymbol method)
{
return method.ContainingType.AllInterfaces.SelectMany(@interface => @interface.GetMembers().OfType<IMethodSymbol>()).
Where(interfaceMethod => method.ContainingType.FindImplementationForInterfaceMember(interfaceMethod).Equals(method));
Where(interfaceMethod => method.ContainingType.FindImplementationForInterfaceMember(interfaceMethod)?.Equals(method) ?? false);
}
private static IEnumerable<IMethodSymbol> GetOverridenMethods(IMethodSymbol method)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
context.Diagnostics);
}

private static bool IsComparisonExpr(ExpressionSyntax expr)
{
var exprKind = expr.Kind();
return exprKind == SyntaxKind.NotEqualsExpression ||
exprKind == SyntaxKind.EqualsExpression ||
exprKind == SyntaxKind.GreaterThanExpression ||
exprKind == SyntaxKind.GreaterThanOrEqualExpression ||
exprKind == SyntaxKind.LessThanExpression ||
exprKind == SyntaxKind.LessThanOrEqualExpression;
}

private static ExpressionSyntax SmartNotExpression(ExpressionSyntax expr, SyntaxGenerator generator)
{
if (expr is BinaryExpressionSyntax binary)
Expand All @@ -69,8 +80,17 @@ private static ExpressionSyntax SmartNotExpression(ExpressionSyntax expr, Syntax
return (ExpressionSyntax)generator.GreaterThanOrEqualExpression(binary.Left, binary.Right);
case SyntaxKind.LessThanOrEqualExpression:
return (ExpressionSyntax)generator.GreaterThanExpression(binary.Left, binary.Right);
case SyntaxKind.LogicalOrExpression when IsComparisonExpr(binary.Left) && IsComparisonExpr(binary.Right):
return (ExpressionSyntax)generator.LogicalAndExpression(SmartNotExpression(binary.Left, generator), SmartNotExpression(binary.Right, generator));
case SyntaxKind.LogicalAndExpression when IsComparisonExpr(binary.Left) && IsComparisonExpr(binary.Right):
return (ExpressionSyntax)generator.LogicalOrExpression(SmartNotExpression(binary.Left, generator), SmartNotExpression(binary.Right, generator));
}
}
else if (expr is PrefixUnaryExpressionSyntax prefixUnary)
{
if (prefixUnary.IsKind(SyntaxKind.LogicalNotExpression))
return prefixUnary.Operand;
}

return (ExpressionSyntax)generator.LogicalNotExpression(expr);
}
Expand Down
1 change: 1 addition & 0 deletions src/TestSolution/TestProject/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ static void Test(string val)
static void ContractTest(string val, int data)
{
Contract.Requires<ArgumentNullException>(val != null, "aaa" + "ab");
Contract.Requires<ArgumentNullException>(!string.IsNullOrEmpty(val));
Contract.Requires<ArgumentOutOfRangeException>(data >= 0);
Contract.Requires<ArgumentException>(data != 0 && val != "fff");
Contract.Requires<InvalidOperationException>(data < 100);
Expand Down

0 comments on commit 8cf136e

Please sign in to comment.