-
Notifications
You must be signed in to change notification settings - Fork 256
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
Add validation for additional unnecessary locations #1181
Conversation
@@ -10,8 +10,13 @@ namespace Microsoft.CodeAnalysis.Testing.TestAnalyzers | |||
public abstract class AbstractHighlightTokensAnalyzer : DiagnosticAnalyzer | |||
{ | |||
protected AbstractHighlightTokensAnalyzer(string id, params int[] tokenKinds) | |||
: this(id, customTags: new string[0], tokenKinds) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
➡️ Not available in all target frameworks.
int[] unnecessaryIndices = { }; | ||
if (actual.diagnostic.Properties.TryGetValue(WellKnownDiagnosticTags.Unnecessary, out var encodedUnnecessaryLocations)) | ||
{ | ||
verifier.True(actual.diagnostic.Descriptor.CustomTags.Contains(WellKnownDiagnosticTags.Unnecessary), "Diagnostic reported extended unnecessary locations, but the descriptor is not marked as unnecessary code."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
➡️ No, a primary use case for faded diagnostics is when the primary diagnostic location is faded. There would be no additional property in this case.
var match = EncodedIndicesSyntax.Match(encodedUnnecessaryLocations); | ||
verifier.True(match.Success, $"Expected encoded unnecessary locations to be a valid JSON array of non-negative integers: {encodedUnnecessaryLocations}"); | ||
unnecessaryIndices = match.Groups["Index"].Captures.OfType<Capture>().Select(capture => int.Parse(capture.Value)).ToArray(); | ||
verifier.NotEmpty(nameof(unnecessaryIndices), unnecessaryIndices); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
➡️ I felt it would be harder to understand why the regex failed
AppendLocation(diagnostics[i].Location); | ||
foreach (var additionalLocation in diagnostics[i].AdditionalLocations) | ||
// The unnecessary code designator is ignored for the primary diagnostic location. | ||
AppendLocation(diagnostics[i].Location, isUnnecessary: false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
➡️ Simplified
var match = EncodedIndicesSyntax.Match(encodedUnnecessaryLocations); | ||
if (match.Success) | ||
{ | ||
unnecessaryIndices = match.Groups["Index"].Captures.OfType<Capture>().Select(capture => int.Parse(capture.Value)).ToArray(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
➡️ Will keep this form, since the collection is typically small, and if we ever want to add assertions about uniqueness or order it will be clearer what we are starting with.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No description provided.