Skip to content

Commit

Permalink
Merge pull request #1329 from sharwell/fix-1328
Browse files Browse the repository at this point in the history
Fix SA1312 treating fields as variables
  • Loading branch information
sharwell committed Aug 30, 2015
2 parents 70534fa + 5348145 commit 2a0671d
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,36 @@ public async Task TestThatDiagnosticIsNotReportedAsync(string modifiers)
await this.VerifyCSharpDiagnosticAsync(string.Format(testCode, modifiers), EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
public async Task TestThatDiagnosticIsNotReportedForParametersAsync()
{
var testCode = @"public class TypeName
{
public void MethodName(string bar, string Car)
{
}
}";

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

[Fact]
public async Task TestThatDiagnosticIsNotReportedForVariablesAsync()
{
var testCode = @"public class TypeName
{
public void MethodName()
{
const string bar = nameof(bar);
const string Bar = nameof(Bar);
string car = nameof(car);
string Car = nameof(Car);
}
}";

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

/// <summary>
/// This test ensures the implementation of <see cref="SA1306FieldNamesMustBeginWithLowerCaseLetter"/> is
/// correct with respect to the documented behavior for parameters and local variables (including local
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,33 @@ public void MethodName()
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
public async Task TestThatDiagnosticIsNotReportedForFieldsAsync()
{
var testCode = @"public class TypeName
{
const string bar = nameof(bar);
const string Bar = nameof(Bar);
string car = nameof(car);
string Car = nameof(Car);
}";

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

[Fact]
public async Task TestThatDiagnosticIsNotReportedForParametersAsync()
{
var testCode = @"public class TypeName
{
public void MethodName(string bar, string Car)
{
}
}";

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

[Fact]
public async Task TestThatDiagnosticIsReported_SingleVariableAsync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,37 @@

public class SA1313UnitTests : CodeFixVerifier
{
[Fact]
public async Task TestThatDiagnosticIsNotReportedForFieldsAsync()
{
var testCode = @"public class TypeName
{
const string bar = nameof(bar);
const string Bar = nameof(Bar);
string car = nameof(car);
string Car = nameof(Car);
}";

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

[Fact]
public async Task TestThatDiagnosticIsNotReportedForVariablesAsync()
{
var testCode = @"public class TypeName
{
public void MethodName()
{
const string bar = nameof(bar);
const string Bar = nameof(Bar);
string car = nameof(car);
string Car = nameof(Car);
}
}";

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

[Fact]
public async Task TestThatDiagnosticIsReported_SingleParameterAsync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ private static void HandleCompilationStart(CompilationStartAnalysisContext conte
private static void HandleVariableDeclarationSyntax(SyntaxNodeAnalysisContext context)
{
VariableDeclarationSyntax syntax = (VariableDeclarationSyntax)context.Node;
if (syntax.Parent.IsKind(SyntaxKind.FieldDeclaration))
{
// This diagnostic is only for local variables.
return;
}

if (NamedTypeHelpers.IsContainedInNativeMethodsClass(syntax))
{
return;
Expand Down

0 comments on commit 2a0671d

Please sign in to comment.