-
Notifications
You must be signed in to change notification settings - Fork 511
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
Fix #1878 (SA1133CodeFixProvider crash with System.InvalidCastException) #1880
Conversation
Current coverage is
|
@@ -51,8 +51,18 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context) | |||
private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken) | |||
{ | |||
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); | |||
var violatingAttribute = (AttributeSyntax)syntaxRoot.FindNode(diagnostic.Location.SourceSpan).Parent; | |||
var attributeList = (AttributeListSyntax)violatingAttribute.Parent; | |||
var nodeInSourceSpan = syntaxRoot.FindNode(diagnostic.Location.SourceSpan); |
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.
We've run into problems in the past where error nodes in the tree caused FindNode
to return unexpected nodes. Normally this is solved by setting getInnermostNodeForTie: true
, but I'm not sure that would behave properly here. We probably want to have a third case handled where nodeInSourceSpan.Parent
is neither an AttributeListSyntax
nor an AttributeSyntax
, and just return.
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.
❗ A better solution would be to use getInnermostNodeForTie: true
and then call FirstAncestorOrSelf<AttributeListSyntax>()
on the found node. That call should never fail, as the analyzer works on AttributeSyntaxList
.
@sharwell added default case which just returns the document object |
Could you add these tests?
|
@Noryoko unit tests added. |
@Noryoko, @vweijsters, do you think the following tests should also pass?
With the current code they are failing with:
Here you can find the test code: https://github.com/NikolayIT/StyleCopAnalyzers/commit/74bef9273a0a148b7a2e1a8fb32db9e5529b2ed9#diff-9e4ac25db4b34d81902b3ff7fedf0e6cR176 |
I think so. The fix is already splitting the attributes onto their own lines. Would make the end result look consistent. |
I don't think they should pass, as they are partly in the domain of SA1134. The SA1133 code fix adds the new lines to make sure that it doesn't immediately introduces a SA1134 after code fixing, but it will not / should not attempt to fix any preexisting SA1134 cases. |
OK, then this pull request is ready for approval. |
I'll change my vote to agree with @vweijsters. |
@NikolayIT I would like to see my remark on |
@vweijsters is that what you meant? |
No, I meant:
|
…storOrSelf() on the found node
Sorry, didn't see it. Is now OK? @vweijsters |
Totally 😉 👍 Looks good to me |
👍 Looks good to me |
Fix #1878 (SA1133CodeFixProvider crash with System.InvalidCastException)
@sharwell Could you merge this into master as well? |
@vweijsters yes I'll merge it tonight |
Fix #1878