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

Add validation for additional unnecessary locations #1181

Merged
merged 2 commits into from
Aug 30, 2024

Conversation

sharwell
Copy link
Member

No description provided.

@sharwell sharwell requested a review from a team as a code owner August 22, 2024 14:57
@@ -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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new string[0]

Array.Empty?

Copy link
Member Author

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.");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actual.diagnostic.Descriptor.CustomTags.Contains(WellKnownDiagnosticTags.Unnecessary)

Could this be verified as false in WellKnownDiagnosticTags.Unnecessary isn't in the Properties?

Copy link
Member Author

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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

verifier.NotEmpty(nameof(unnecessaryIndices), unnecessaryIndices);

nit: not sure which I like better, just wanted to point out that the regex could have been written to not accept an empty number of indices

Copy link
Member Author

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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

diagnostics[i]

nit: feels like this code would be a bit simpler if diagnostics[i] was just in a local

Copy link
Member Author

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();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ToArray

perf doesn't matter here, but this is used more as a set than an array.

Copy link
Member Author

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.

Copy link

@ToddGrun ToddGrun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@sharwell sharwell merged commit 69e2fa7 into dotnet:main Aug 30, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants