diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpecialRules/SA0002UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpecialRules/SA0002UnitTests.cs index 83413683d..1957ea201 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpecialRules/SA0002UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/SpecialRules/SA0002UnitTests.cs @@ -52,7 +52,8 @@ public async Task TestInvalidSettingsAsync() } "; - DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(null, 0, 0); + // This diagnostic is reported without a location + DiagnosticResult expected = this.CSharpDiagnostic(); await this.VerifyCSharpDiagnosticAsync(TestCode, expected, CancellationToken.None).ConfigureAwait(false); } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test/Verifiers/DiagnosticVerifier.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test/Verifiers/DiagnosticVerifier.cs index b5cabc506..92076218c 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test/Verifiers/DiagnosticVerifier.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test/Verifiers/DiagnosticVerifier.cs @@ -326,6 +326,26 @@ private static string FormatDiagnostics(ImmutableArray analy return builder.ToString(); } + private static bool IsSubjectToExclusion(DiagnosticResult result) + { + if (result.Id.StartsWith("CS", StringComparison.Ordinal)) + { + return false; + } + + if (result.Id.StartsWith("AD", StringComparison.Ordinal)) + { + return false; + } + + if (result.Locations.Length == 0) + { + return false; + } + + return true; + } + /// /// General method that gets a collection of actual s found in the source after the /// analyzer is run, then verifies each of them. @@ -346,12 +366,13 @@ private async Task VerifyDiagnosticsAsync(string[] sources, string language, Imm if (filenames == null) { // Also check if the analyzer honors exclusions - if (expected.Any(x => x.Id.StartsWith("SA", StringComparison.Ordinal) || x.Id.StartsWith("SX", StringComparison.Ordinal))) + if (expected.Any(IsSubjectToExclusion)) { - // We want to look at non-stylecop diagnostics only. We also insert a new line at the beginning - // so we have to move all diagnostic location down by one line + // Diagnostics reported by the compiler and analyzer diagnostics which don't have a location will + // still be reported. We also insert a new line at the beginning so we have to move all diagnostic + // locations which have a specific position down by one line. var expectedResults = expected - .Where(x => !x.Id.StartsWith("SA", StringComparison.Ordinal) && !x.Id.StartsWith("SX", StringComparison.Ordinal)) + .Where(x => !IsSubjectToExclusion(x)) .Select(x => x.WithLineOffset(1)) .ToArray();