diff options
author | Wei Chen <weichch87@gmail.com> | 2022-09-27 16:53:10 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-27 16:53:10 +0300 |
commit | b2af65a406bc28c25f0a25ed87b4e48c98f52fef (patch) | |
tree | 2ad4a8978d8004743bcb85b05ae55a344ab164e8 /src/libraries | |
parent | 91706f19f469587937ab300dbb97c913983577cf (diff) |
Fix LoggerMessageGenerator parser to check for expression bodied method (#75601)
* Check for expression bodied method
Fixed LoggerMessageGenerator parser to check for expression bodied
method and generate a proper diagnostic message.
Fix #66080
* Fix expression body in test
Fixed invalid expression body in test
Diffstat (limited to 'src/libraries')
2 files changed, 20 insertions, 2 deletions
diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Parser.cs b/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Parser.cs index cc8dc0b5aae..0a6eadbbfee 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Parser.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Parser.cs @@ -248,9 +248,10 @@ namespace Microsoft.Extensions.Logging.Generators keepMethod = false; } - if (method.Body != null) + CSharpSyntaxNode? methodBody = method.Body as CSharpSyntaxNode ?? method.ExpressionBody; + if (methodBody != null) { - Diag(DiagnosticDescriptors.LoggingMethodHasBody, method.Body.GetLocation()); + Diag(DiagnosticDescriptors.LoggingMethodHasBody, methodBody.GetLocation()); keepMethod = false; } diff --git a/src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/LoggerMessageGeneratorParserTests.cs b/src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/LoggerMessageGeneratorParserTests.cs index 4bb4271e265..b09409a76b6 100644 --- a/src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/LoggerMessageGeneratorParserTests.cs +++ b/src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/LoggerMessageGeneratorParserTests.cs @@ -80,6 +80,23 @@ namespace Microsoft.Extensions.Logging.Generators.Tests Assert.Equal(DiagnosticDescriptors.LoggingMethodHasBody.Id, diagnostics[0].Id); } + [Fact] + public async Task InvalidMethodExpressionBody() + { + IReadOnlyList<Diagnostic> diagnostics = await RunGenerator(@" + partial class C + { + static partial void M1(ILogger logger); + + [LoggerMessage(EventId = 0, Level = LogLevel.Debug, Message = ""M1"")] + static partial void M1(ILogger logger) => throw new Exception(); + } + "); + + Assert.Single(diagnostics); + Assert.Equal(DiagnosticDescriptors.LoggingMethodHasBody.Id, diagnostics[0].Id); + } + [Theory] [InlineData("EventId = 0, Level = null, Message = \"This is a message with {foo}\"")] [InlineData("eventId: 0, level: null, message: \"This is a message with {foo}\"")] |