From 9b6cc3a90d13e63c28f218812628cbd63daf7e2f Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Sun, 30 Aug 2015 00:47:37 -0500 Subject: [PATCH 1/2] Add regression tests for #1328 --- .../NamingRules/SA1306UnitTests.cs | 30 ++++++++++++++++++ .../NamingRules/SA1312UnitTests.cs | 27 ++++++++++++++++ .../NamingRules/SA1313UnitTests.cs | 31 +++++++++++++++++++ 3 files changed, 88 insertions(+) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1306UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1306UnitTests.cs index eb7f4393c..be3b66aa8 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1306UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1306UnitTests.cs @@ -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); + } + /// /// This test ensures the implementation of is /// correct with respect to the documented behavior for parameters and local variables (including local diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1312UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1312UnitTests.cs index 307d6b36f..e267bf8d1 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1312UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1312UnitTests.cs @@ -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() { diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1313UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1313UnitTests.cs index 3b7ee85a9..6b5369740 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1313UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/NamingRules/SA1313UnitTests.cs @@ -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() { From 53481452cf77421a74be78d20da40e1289a56c82 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Sun, 30 Aug 2015 00:54:02 -0500 Subject: [PATCH 2/2] Fix SA1312 treating fields as local variables Fixes #1328 --- .../SA1312VariableNamesMustBeginWithLowerCaseLetter.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/NamingRules/SA1312VariableNamesMustBeginWithLowerCaseLetter.cs b/StyleCop.Analyzers/StyleCop.Analyzers/NamingRules/SA1312VariableNamesMustBeginWithLowerCaseLetter.cs index adf593bfb..9ce05241f 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/NamingRules/SA1312VariableNamesMustBeginWithLowerCaseLetter.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/NamingRules/SA1312VariableNamesMustBeginWithLowerCaseLetter.cs @@ -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;