Skip to content

Commit

Permalink
Merge pull request #1148 from Noryoko/fix-SA1215
Browse files Browse the repository at this point in the history
Fix for SA1215 reported for const followed by readonly
  • Loading branch information
sharwell committed Aug 8, 2015
2 parents cedc7e8 + 9acff65 commit 1bd787c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,23 @@ public async Task TestNonStaticFollowedByReadOnlyAtDifferentAccessLevelAsync()
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

/// <summary>
/// Verifies that the analyzer will properly handle const before readonly fields.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task TestConstBeforeReadonlyAsync()
{
var testCode = @"class TestClass
{
private const int TestField1 = 1;
private readonly int TestField2 = 2;
}
";

await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

/// <inheritdoc/>
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private static void HandleTypeDeclaration(SyntaxNodeAnalysisContext context)

var previousFieldReadonly = true;
var previousAccessLevel = AccessLevel.NotSpecified;
var previousMemberStatic = true;
var previousMemberStaticOrConst = true;
foreach (var member in typeDeclaration.Members)
{
var field = member as FieldDeclarationSyntax;
Expand All @@ -66,10 +66,10 @@ private static void HandleTypeDeclaration(SyntaxNodeAnalysisContext context)
var currentFieldReadonly = field.Modifiers.Any(SyntaxKind.ReadOnlyKeyword);
var currentAccessLevel = AccessLevelHelper.GetAccessLevel(field.Modifiers);
currentAccessLevel = currentAccessLevel == AccessLevel.NotSpecified ? AccessLevel.Private : currentAccessLevel;
var currentMemberStatic = field.Modifiers.Any(SyntaxKind.StaticKeyword);
var currentMemberStaticOrConst = field.Modifiers.Any(SyntaxKind.StaticKeyword) || field.Modifiers.Any(SyntaxKind.ConstKeyword);
if (currentAccessLevel == previousAccessLevel
&& !currentMemberStatic
&& !previousMemberStatic
&& !currentMemberStaticOrConst
&& !previousMemberStaticOrConst
&& currentFieldReadonly
&& !previousFieldReadonly)
{
Expand All @@ -78,7 +78,7 @@ private static void HandleTypeDeclaration(SyntaxNodeAnalysisContext context)

previousFieldReadonly = currentFieldReadonly;
previousAccessLevel = currentAccessLevel;
previousMemberStatic = currentMemberStatic;
previousMemberStaticOrConst = currentMemberStaticOrConst;
}
}
}
Expand Down

0 comments on commit 1bd787c

Please sign in to comment.