0

I have developed my first diagnostic analyzer and installed it into a test project via NuGet. Everything works find, except that no red line is shown under the issue. I am using context.Node.GetLocation() where context is SyntaxNodeAnalysisContext.

I updated the diagnostic to write the location in the message so I could try to determine the location using context.ReportDiagnostic(Diagnostic.Create(DD_INVALID_PARAM_TYPE, location, methodSymbol.Name, location.ToString(), propertyType)); and I see in the errors window something that location is SourceFile(C:\..\..\..\Program.cs[275..276)) but the Program.cs file is only 27 lines long.

This is the full method in question at the moment:

        private static void AnalyzeParameters(SyntaxNodeAnalysisContext context, IMethodSymbol methodSymbol)
        {
            var parameters = methodSymbol.Parameters;
            if (parameters.Length != 1)
            {
                context.ReportDiagnostic(Diagnostic.Create(DD_INVALID_PARAM_COUNT, context.Node.GetLocation(), methodSymbol.Name, parameters.Length));
                return;
            }

            var propertyType = ((PropertyDeclarationSyntax)context.Node).Type.ToString();
            var methodInputType = parameters.First().Type.ToString();

            if (propertyType != methodInputType)
            {
                context.ReportDiagnostic(Diagnostic.Create(DD_INVALID_PARAM_TYPE, context.Node.GetLocation(), methodSymbol.Name, methodInputType, propertyType));
            }
        }

where I am analyzing a property declaration and its associated attributes.

1
  • 1
    Please don't edit the solution into your question, or edit the title to say "Solved". If you have an answer, post it as an answer and accept it. Commented Jan 17 at 13:26

1 Answer 1

1

That location is "span based", i.e, it's at character index 275 from the start of the file.

Sign up to request clarification or add additional context in comments.

3 Comments

Assuming this is the correct character span, any ideas on why it would show the error in the Errors tool window, but not putting the lines under the actually property. A couple of times I have seen it appears under the property (when the loading is lagging) and then it disappears, but still remains in the Errors window.
That's a pretty broad question with lots of potential things in play; if you can isolate it a bit more creating a new question with more details might be helpful. I'm not trying to be dismissive, it's just the space is really complicated and has lots of moving parts. So you could be describing a bug we've fixed, or some complicated interaction with an analyzer, etc....
I was able to resolve the issue; turns out it was a ReSharper setting. I have updated the original post with the solution. Thank you for your feedback.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.