Skip to content

Commit

Permalink
Issues/754 (#756)
Browse files Browse the repository at this point in the history
* MA0007 reports in switch expressions

* Add with expressions
  • Loading branch information
meziantou authored Sep 21, 2024
1 parent 780afd1 commit 70714c2
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/Meziantou.Analyzer/Rules/CommaAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public override void Initialize(AnalysisContext context)
context.RegisterSyntaxNodeAction(HandleObjectInitializer, ObjectInitializerKinds);
context.RegisterSyntaxNodeAction(HandleAnonymousObjectInitializer, SyntaxKind.AnonymousObjectCreationExpression);
context.RegisterSyntaxNodeAction(HandleEnumDeclaration, SyntaxKind.EnumDeclaration);
context.RegisterSyntaxNodeAction(HandleWithExpression, SyntaxKind.WithExpression);
context.RegisterSyntaxNodeAction(HandleSwitchExpression, SyntaxKind.SwitchExpression);
#if CSHARP12_OR_GREATER
context.RegisterSyntaxNodeAction(HandleCollectionExpression, SyntaxKind.CollectionExpression);
#endif
Expand All @@ -58,6 +60,21 @@ private void HandleCollectionExpression(SyntaxNodeAnalysisContext context)
}
#endif

private void HandleSwitchExpression(SyntaxNodeAnalysisContext context)
{
var node = (SwitchExpressionSyntax)context.Node;
HandleSeparatedList(context, node, node.Arms);
}

private void HandleWithExpression(SyntaxNodeAnalysisContext context)
{
var node = (WithExpressionSyntax)context.Node;
if (node.Initializer is null)
return;

HandleSeparatedList(context, node, node.Initializer.Expressions);
}

private static void HandleEnumDeclaration(SyntaxNodeAnalysisContext context)
{
var node = (EnumDeclarationSyntax)context.Node;
Expand Down
2 changes: 1 addition & 1 deletion tests/Meziantou.Analyzer.Test/Helpers/ProjectBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ public ProjectBuilder ShouldReportDiagnosticWithMessage(string message)
return this;
}

public ProjectBuilder ShouldFixCodeWith(string codeFix) =>
public ProjectBuilder ShouldFixCodeWith([StringSyntax("C#-test")] string codeFix) =>
ShouldFixCodeWith(index: null, codeFix);

public ProjectBuilder ShouldFixCodeWith(int? index, [StringSyntax("C#-test")] string codeFix)
Expand Down
111 changes: 111 additions & 0 deletions tests/Meziantou.Analyzer.Test/Rules/CommaAnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,115 @@ await CreateProjectBuilder()
.ValidateAsync();
}
#endif

[Fact]
public Task SwitchExpressionWithoutLeadingComma()
=> CreateProjectBuilder()
.WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp8)
.WithSourceCode("""
class TypeName
{
public void Test()
{
_ = 0 switch
{
1 => 1,
[||]2 => 2
};
}
}
""")
.ShouldFixCodeWith("""
class TypeName
{
public void Test()
{
_ = 0 switch
{
1 => 1,
2 => 2,
};
}
}
""")
.ValidateAsync();

[Fact]
public Task SwitchExpressionWithLeadingComma()
=> CreateProjectBuilder()
.WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp8)
.WithSourceCode("""
class TypeName
{
public void Test()
{
_ = 0 switch
{
1 => 1,
2 => 2,
};
}
}
""")
.ValidateAsync();

[Fact]
public Task WithExpressionWithoutLeadingComma()
=> CreateProjectBuilder()
.WithOutputKind(Microsoft.CodeAnalysis.OutputKind.ConsoleApplication)
.WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.Latest)
.WithTargetFramework(TargetFramework.Net8_0)
.WithSourceCode("""
var a = new Sample(1, 2);
_ = a with
{
A = 3,
[||]B = 4
};

record Sample(int A, int B);
""")
.ShouldFixCodeWith("""
var a = new Sample(1, 2);
_ = a with
{
A = 3,
B = 4,
};

record Sample(int A, int B);
""")
.ValidateAsync();

[Fact]
public Task WithExpressionWithLeadingComma()
=> CreateProjectBuilder()
.WithOutputKind(Microsoft.CodeAnalysis.OutputKind.ConsoleApplication)
.WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.Latest)
.WithTargetFramework(TargetFramework.Net8_0)
.WithSourceCode("""
var a = new Sample(1, 2);
_ = a with
{
A = 3,
B = 4,
};

record Sample(int A, int B);
""")
.ValidateAsync();

[Fact]
public Task WithExpressionWithoutLeadingCommaSingleLine()
=> CreateProjectBuilder()
.WithOutputKind(Microsoft.CodeAnalysis.OutputKind.ConsoleApplication)
.WithTargetFramework(TargetFramework.Net8_0)
.WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.Latest)
.WithSourceCode("""
var a = new Sample(1, 2);
_ = a with { A = 3, B = 4 };

record Sample(int A, int B);
""")
.ValidateAsync();
}

0 comments on commit 70714c2

Please sign in to comment.