Skip to content
This repository has been archived by the owner. It is now read-only.

Commit

Permalink
Fixed #192: RECS0033 suggestion produces bad code fix at xor-assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
Rpinski committed Mar 22, 2016
1 parent 7be7784 commit b8f3f5b
Show file tree
Hide file tree
Showing 3 changed files with 279 additions and 248 deletions.
Expand Up @@ -114,7 +114,7 @@ internal static bool CheckForAssignmentOfLiteral(StatementSyntax statement, Synt
if (expressionStatement == null)
return false;
var assignmentExpression = expressionStatement.Expression as AssignmentExpressionSyntax;
if (assignmentExpression == null)
if ((assignmentExpression == null) || !assignmentExpression.IsKind(SyntaxKind.SimpleAssignmentExpression))
return false;
assignmentTarget = assignmentExpression.Left as IdentifierNameSyntax;
assignmentTrailingTriviaList = assignmentExpression.OperatorToken.TrailingTrivia;
Expand Down
246 changes: 131 additions & 115 deletions Tests/CSharp/Diagnostics/ConvertIfToAndExpressionTests.cs
Expand Up @@ -11,18 +11,18 @@ public void TestVariableDeclarationCase()
{
Analyze<ConvertIfToAndExpressionAnalyzer>(@"class Foo
{
int Bar(int o)
{
bool b = o > 10;
$if$ (o < 10)
b = false;
}
int Bar(int o)
{
bool b = o > 10;
$if$ (o < 10)
b = false;
}
}", @"class Foo
{
int Bar(int o)
{
bool b = o > 10 && o >= 10;
}
int Bar(int o)
{
bool b = o > 10 && o >= 10;
}
}");
}

Expand All @@ -31,20 +31,20 @@ public void TestVariableDeclarationCaseAndComment()
{
Analyze<ConvertIfToAndExpressionAnalyzer>(@"class Foo
{
int Bar(int o)
{
// Some comment
bool b = o > 10;
$if$ (o < 10)
b = false;
}
int Bar(int o)
{
// Some comment
bool b = o > 10;
$if$ (o < 10)
b = false;
}
}", @"class Foo
{
int Bar(int o)
{
// Some comment
bool b = o > 10 && o >= 10;
}
int Bar(int o)
{
// Some comment
bool b = o > 10 && o >= 10;
}
}");
}

Expand All @@ -53,20 +53,20 @@ public void TestVariableDeclarationCaseWithBlock()
{
Analyze<ConvertIfToAndExpressionAnalyzer>(@"class Foo
{
int Bar(int o)
{
bool b = o > 10;
$if$ (o < 10)
{
b = false;
}
}
int Bar(int o)
{
bool b = o > 10;
$if$ (o < 10)
{
b = false;
}
}
}", @"class Foo
{
int Bar(int o)
{
bool b = o > 10 && o >= 10;
}
int Bar(int o)
{
bool b = o > 10 && o >= 10;
}
}");
}

Expand All @@ -75,18 +75,18 @@ public void TestComplexVariableDeclarationCase()
{
Analyze<ConvertIfToAndExpressionAnalyzer>(@"class Foo
{
int Bar(int o)
{
bool b = o > 10 || o < 10;
$if$ (o < 10)
b = false;
}
int Bar(int o)
{
bool b = o > 10 || o < 10;
$if$ (o < 10)
b = false;
}
}", @"class Foo
{
int Bar(int o)
{
bool b = (o > 10 || o < 10) && o >= 10;
}
int Bar(int o)
{
bool b = (o > 10 || o < 10) && o >= 10;
}
}");
}

Expand All @@ -95,17 +95,17 @@ public void TestConversionBug()
{
Analyze<ConvertIfToAndExpressionAnalyzer>(@"class Foo
{
public override void VisitComposedType (ComposedType composedType)
{
$if$ (composedType.PointerRank > 0)
unsafeStateStack.Peek ().UseUnsafeConstructs = false;
}
public override void VisitComposedType (ComposedType composedType)
{
$if$ (composedType.PointerRank > 0)
unsafeStateStack.Peek ().UseUnsafeConstructs = false;
}
}", @"class Foo
{
public override void VisitComposedType (ComposedType composedType)
{
unsafeStateStack.Peek ().UseUnsafeConstructs &= composedType.PointerRank <= 0;
}
public override void VisitComposedType (ComposedType composedType)
{
unsafeStateStack.Peek ().UseUnsafeConstructs &= composedType.PointerRank <= 0;
}
}");
}

Expand All @@ -114,21 +114,21 @@ public void TestCommonCase()
{
Analyze<ConvertIfToAndExpressionAnalyzer>(@"class Foo
{
int Bar(int o)
{
bool b = o > 10;
Console.WriteLine ();
$if$ (o < 10)
b = false;
}
int Bar(int o)
{
bool b = o > 10;
Console.WriteLine ();
$if$ (o < 10)
b = false;
}
}", @"class Foo
{
int Bar(int o)
{
bool b = o > 10;
Console.WriteLine ();
b &= o >= 10;
}
int Bar(int o)
{
bool b = o > 10;
Console.WriteLine ();
b &= o >= 10;
}
}");
}

Expand All @@ -137,23 +137,23 @@ public void TestCommonCaseWithComment()
{
Analyze<ConvertIfToAndExpressionAnalyzer>(@"class Foo
{
int Bar(int o)
{
bool b = o > 10;
Console.WriteLine ();
// Some comment
$if$ (o < 10)
b = false;
}
int Bar(int o)
{
bool b = o > 10;
Console.WriteLine ();
// Some comment
$if$ (o < 10)
b = false;
}
}", @"class Foo
{
int Bar(int o)
{
bool b = o > 10;
Console.WriteLine ();
// Some comment
b &= o >= 10;
}
int Bar(int o)
{
bool b = o > 10;
Console.WriteLine ();
// Some comment
b &= o >= 10;
}
}");
}

Expand All @@ -162,23 +162,23 @@ public void TestCommonCaseWithBlock()
{
Analyze<ConvertIfToAndExpressionAnalyzer>(@"class Foo
{
int Bar(int o)
{
bool b = o > 10;
Console.WriteLine ();
$if$ (o < 10)
{
b = false;
}
}
int Bar(int o)
{
bool b = o > 10;
Console.WriteLine ();
$if$ (o < 10)
{
b = false;
}
}
}", @"class Foo
{
int Bar(int o)
{
bool b = o > 10;
Console.WriteLine ();
b &= o >= 10;
}
int Bar(int o)
{
bool b = o > 10;
Console.WriteLine ();
b &= o >= 10;
}
}");
}

Expand All @@ -187,19 +187,19 @@ public void TestCommonCaseWithElse()
{
Analyze<ConvertIfToAndExpressionAnalyzer>(@"class Foo
{
int Bar(int o)
{
bool b = o > 10;
Console.WriteLine ();
if (o < 10)
{
b = false;
}
else
{
return 42;
}
}
int Bar(int o)
{
bool b = o > 10;
Console.WriteLine ();
if (o < 10)
{
b = false;
}
else
{
return 42;
}
}
}");
}

Expand All @@ -208,13 +208,29 @@ public void TestNullCheckBug()
{
Analyze<ConvertIfToAndExpressionAnalyzer>(@"class Foo
{
public bool Enabled { get; set; }
public bool Enabled { get; set; }
int Bar(Foo fileChangeWatcher)
{
if (fileChangeWatcher != null)
fileChangeWatcher.Enabled = true;
}
int Bar(Foo fileChangeWatcher)
{
if (fileChangeWatcher != null)
fileChangeWatcher.Enabled = true;
}
}");
}

[Test]
public void TestComplexAssignmentCase()
{
Analyze<ConvertIfToOrExpressionAnalyzer>(@"class Foo
{
int Bar(bool foo)
{
bool bar = true;
if (foo)
{
bar ^= false;
}
}
}");
}
}
Expand Down

0 comments on commit b8f3f5b

Please sign in to comment.