Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for SA1215 reported for const followed by readonly #1148

Merged
merged 2 commits into from
Aug 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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