diff --git a/docs/Rules/MA0109.md b/docs/Rules/MA0109.md index c454a985..2b02e359 100644 --- a/docs/Rules/MA0109.md +++ b/docs/Rules/MA0109.md @@ -11,3 +11,5 @@ void A(System.ReadOnlySpan a) { } ````c# void A(string[] a) { } // report diagnostic ```` + +The rule does not report a diagnostic for the program entry point (`Main(string[] args)`), as its signature is mandated by the runtime. diff --git a/src/Meziantou.Analyzer/Rules/AddOverloadWithSpanOrMemoryAnalyzer.cs b/src/Meziantou.Analyzer/Rules/AddOverloadWithSpanOrMemoryAnalyzer.cs index b99a1ded..094ee4c1 100644 --- a/src/Meziantou.Analyzer/Rules/AddOverloadWithSpanOrMemoryAnalyzer.cs +++ b/src/Meziantou.Analyzer/Rules/AddOverloadWithSpanOrMemoryAnalyzer.cs @@ -40,6 +40,10 @@ private static void AnalyzeMethod(SymbolAnalysisContext context) if (method.MethodKind is not MethodKind.Ordinary and not MethodKind.Constructor) return; + // Skip the program entry point (e.g., Main(string[] args)) as the signature is mandated by the runtime + if (method.IsEqualTo(context.Compilation.GetEntryPoint(context.CancellationToken))) + return; + if (!method.Parameters.Any(IsCandidateForSpanOrMemory)) return; diff --git a/tests/Meziantou.Analyzer.Test/Rules/AddOverloadWithSpanOrMemoryAnalyzerTests.cs b/tests/Meziantou.Analyzer.Test/Rules/AddOverloadWithSpanOrMemoryAnalyzerTests.cs index 199dadec..ff7b5029 100755 --- a/tests/Meziantou.Analyzer.Test/Rules/AddOverloadWithSpanOrMemoryAnalyzerTests.cs +++ b/tests/Meziantou.Analyzer.Test/Rules/AddOverloadWithSpanOrMemoryAnalyzerTests.cs @@ -13,6 +13,37 @@ private static ProjectBuilder CreateProjectBuilder() .WithAnalyzer(); } + [Fact] + public async Task EntryPoint_Main_ShouldNotTrigger() + { + const string SourceCode = """ + public class Program + { + public static void Main(string[] args) { } + } + """; + await CreateProjectBuilder() + .WithOutputKind(Microsoft.CodeAnalysis.OutputKind.ConsoleApplication) + .WithSourceCode(SourceCode) + .ValidateAsync(); + } + + [Fact] + public async Task EntryPoint_NonMainMethod_ShouldTrigger() + { + const string SourceCode = """ + public class Program + { + public static void Main(string[] args) { } + public static void [|DoWork|](string[] data) { } + } + """; + await CreateProjectBuilder() + .WithOutputKind(Microsoft.CodeAnalysis.OutputKind.ConsoleApplication) + .WithSourceCode(SourceCode) + .ValidateAsync(); + } + [Fact] public async Task StringArrayWithoutSpanOverload_Params() {