Skip to content

Commit 55a0441

Browse files
authored
Merge pull request #133 from bkoelman/fix-with-expr
Fixed AV1522: Skip assignments in 'with' expressions
2 parents 26d5209 + e044e52 commit 55a0441

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/CSharpGuidelinesAnalyzer/CSharpGuidelinesAnalyzer.Test/Specs/Maintainability/AssignEachVariableInASeparateStatementSpecs.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,6 +1408,30 @@ void M(object o)
14081408
await VerifyGuidelineDiagnosticAsync(source);
14091409
}
14101410

1411+
[Fact]
1412+
internal async Task When_two_variables_are_declared_and_assigned_in_with_expression_it_must_be_skipped()
1413+
{
1414+
// Arrange
1415+
ParsedSourceCode source = new MemberSourceCodeBuilder()
1416+
.InDefaultClass(@"
1417+
void M(object o)
1418+
{
1419+
X x1 = new X();
1420+
X x2 = x1 with { P = 1 };
1421+
}
1422+
1423+
private struct X
1424+
{
1425+
public int P { get; set; }
1426+
}
1427+
")
1428+
.Build();
1429+
1430+
// Act and assert
1431+
await VerifyGuidelineDiagnosticAsync(source);
1432+
}
1433+
1434+
14111435
protected override DiagnosticAnalyzer CreateAnalyzer()
14121436
{
14131437
return new AssignEachVariableInASeparateStatementAnalyzer();

src/CSharpGuidelinesAnalyzer/CSharpGuidelinesAnalyzer/Rules/Maintainability/AssignEachVariableInASeparateStatementAnalyzer.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,16 @@ public StatementWalker(ForLoopSection section)
152152
this.section = section;
153153
}
154154

155+
public override void DefaultVisit([NotNull] IOperation operation)
156+
{
157+
// Check should be replaced with empty override of VisitWith(IWithOperation operation),
158+
// after upgrade to recent version of Microsoft.CodeAnalysis.
159+
if (operation.GetType().Name != "WithOperation")
160+
{
161+
base.DefaultVisit(operation);
162+
}
163+
}
164+
155165
public override void VisitVariableDeclarator([NotNull] IVariableDeclaratorOperation operation)
156166
{
157167
IVariableInitializerOperation initializer = operation.GetVariableInitializer();

0 commit comments

Comments
 (0)