Skip to content

Commit 5febfe1

Browse files
authored
Merge pull request #16 from delegateas/copilot/fix-ct0004-issue
Fix CT0004 to only apply to standalone if statements
2 parents 8ecabdc + 91662f2 commit 5febfe1

2 files changed

Lines changed: 40 additions & 13 deletions

File tree

src/DataverseAnalyzer/NoBracesForControlFlowAnalyzer.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,22 @@ public override void Initialize(AnalysisContext context)
3232
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
3333
context.EnableConcurrentExecution();
3434
context.RegisterSyntaxNodeAction(AnalyzeIfStatement, SyntaxKind.IfStatement);
35-
context.RegisterSyntaxNodeAction(AnalyzeElseClause, SyntaxKind.ElseClause);
3635
}
3736

3837
private static void AnalyzeIfStatement(SyntaxNodeAnalysisContext context)
3938
{
4039
var ifStatement = (IfStatementSyntax)context.Node;
41-
AnalyzeEmbeddedStatement(context, ifStatement.Statement);
42-
}
40+
if (ifStatement.Else is not null)
41+
{
42+
return;
43+
}
4344

44-
private static void AnalyzeElseClause(SyntaxNodeAnalysisContext context)
45-
{
46-
var elseClause = (ElseClauseSyntax)context.Node;
47-
if (elseClause.Statement is not IfStatementSyntax)
45+
if (ifStatement.Parent is ElseClauseSyntax)
4846
{
49-
AnalyzeEmbeddedStatement(context, elseClause.Statement);
47+
return;
5048
}
49+
50+
AnalyzeEmbeddedStatement(context, ifStatement.Statement);
5151
}
5252

5353
private static void AnalyzeEmbeddedStatement(SyntaxNodeAnalysisContext context, StatementSyntax statement)

tests/DataverseAnalyzer.Tests/NoBracesForControlFlowAnalyzerTests.cs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public void TestMethod()
205205
}
206206

207207
[Fact]
208-
public async Task ElseClauseWithOnlyReturnShouldTrigger()
208+
public async Task ElseClauseWithOnlyReturnShouldNotTrigger()
209209
{
210210
var source = """
211211
class TestClass
@@ -223,12 +223,11 @@ public void TestMethod(bool condition)
223223
""";
224224

225225
var diagnostics = await GetDiagnosticsAsync(source);
226-
Assert.Single(diagnostics);
227-
Assert.Equal("CT0004", diagnostics[0].Id);
226+
Assert.Empty(diagnostics);
228227
}
229228

230229
[Fact]
231-
public async Task ElseIfShouldNotTrigger()
230+
public async Task IfWithElseIfShouldNotTrigger()
232231
{
233232
var source = """
234233
class TestClass
@@ -248,7 +247,7 @@ public void TestMethod(bool condition1, bool condition2)
248247
""";
249248

250249
var diagnostics = await GetDiagnosticsAsync(source);
251-
Assert.Equal(2, diagnostics.Length);
250+
Assert.Empty(diagnostics);
252251
}
253252

254253
[Fact]
@@ -291,6 +290,34 @@ public void TestMethod(bool condition)
291290
Assert.Empty(diagnostics);
292291
}
293292

293+
[Fact]
294+
public async Task IfElseWithMultipleStatementsInIfAndSingleControlFlowInElseShouldNotTrigger()
295+
{
296+
var source = """
297+
class TestClass
298+
{
299+
public void TestMethod(bool condition)
300+
{
301+
if (condition)
302+
{
303+
DoSomething();
304+
DoSomethingElse();
305+
}
306+
else
307+
{
308+
throw new System.Exception();
309+
}
310+
}
311+
312+
private void DoSomething() { }
313+
private void DoSomethingElse() { }
314+
}
315+
""";
316+
317+
var diagnostics = await GetDiagnosticsAsync(source);
318+
Assert.Empty(diagnostics);
319+
}
320+
294321
private static async Task<Diagnostic[]> GetDiagnosticsAsync(string source)
295322
{
296323
var syntaxTree = CSharpSyntaxTree.ParseText(source);

0 commit comments

Comments
 (0)