diff --git a/sonaranalyzer-dotnet/src/SonarAnalyzer.CSharp/Rules/UseValueParameter.cs b/sonaranalyzer-dotnet/src/SonarAnalyzer.CSharp/Rules/UseValueParameter.cs index 88a318a7689..d996061b214 100644 --- a/sonaranalyzer-dotnet/src/SonarAnalyzer.CSharp/Rules/UseValueParameter.cs +++ b/sonaranalyzer-dotnet/src/SonarAnalyzer.CSharp/Rules/UseValueParameter.cs @@ -19,7 +19,6 @@ */ using System.Collections.Immutable; -using System.Linq; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; @@ -53,13 +52,14 @@ protected sealed override void Initialize(SonarAnalysisContext context) var accessorDeclaration = cbc.CodeBlock as AccessorDeclarationSyntax; if (accessorDeclaration == null || - accessorDeclaration.IsKind(SyntaxKind.GetAccessorDeclaration)) + accessorDeclaration.IsKind(SyntaxKind.GetAccessorDeclaration) || + accessorDeclaration.Body == null) { return; } if (accessorDeclaration.Body.Statements.Count == 1 && - accessorDeclaration.Body.Statements.Single() is ThrowStatementSyntax) + accessorDeclaration.Body.Statements[0] is ThrowStatementSyntax) { return; } diff --git a/sonaranalyzer-dotnet/src/Tests/SonarAnalyzer.UnitTest/TestCases/UseValueParameter.cs b/sonaranalyzer-dotnet/src/Tests/SonarAnalyzer.UnitTest/TestCases/UseValueParameter.cs index b32133e55a7..a94522e8605 100644 --- a/sonaranalyzer-dotnet/src/Tests/SonarAnalyzer.UnitTest/TestCases/UseValueParameter.cs +++ b/sonaranalyzer-dotnet/src/Tests/SonarAnalyzer.UnitTest/TestCases/UseValueParameter.cs @@ -115,4 +115,16 @@ public override event EventHandler Bar remove { } // Noncompliant } } + + class Program + { + + private string id; + + public string Id + { + get => null; + set => id = 10; // Compliant (should not be, see https://github.com/SonarSource/sonar-csharp/issues/1021) + } + } }