Skip to content

Commit

Permalink
Fix S3237: Rule should not throw NullReferenceException when using ex…
Browse files Browse the repository at this point in the history
…pression body accessor (#1022)
  • Loading branch information
Amaury Levé authored Dec 18, 2017
1 parent f2f5f17 commit a9477c7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/

using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

0 comments on commit a9477c7

Please sign in to comment.