Tweak UTF-8 string literal analyzer and code fix after language change#61650
Tweak UTF-8 string literal analyzer and code fix after language change#61650davidwengier merged 10 commits intodotnet:mainfrom
Conversation
src/Analyzers/CSharp/Analyzers/UseUTF8StringLiteral/UseUTF8StringLiteralDiagnosticAnalyzer.cs
Outdated
Show resolved
Hide resolved
...o/CSharp/Impl/EditorConfigSettings/DataProvider/CodeStyle/CSharpCodeStyleSettingsProvider.cs
Show resolved
Hide resolved
Youssef1313
left a comment
There was a problem hiding this comment.
An array is implicitly convertible to a ReadOnlySpan<T>
The .ToArray call may be unnecessary if the variable, for example, is passed to a method that accepts ReadOnlySpan<T> and the implicit conversion is used, or if the method has both overloads.
This is out of scope for this PR, but it might be beneficial to consider. I'm not sure what would be the best design for this. A separate simplifier that's always applied after code fixes are invoked? An annotation (similar to AddImportsAnnotation)? A separate analyzer/codefix?
| private static LiteralExpressionSyntax CreateUTF8String(SyntaxNode nodeToTakeTriviaFrom, string stringValue) | ||
| private static InvocationExpressionSyntax CreateUTF8String(SyntaxNode nodeToTakeTriviaFrom, string stringValue) | ||
| { | ||
| return CreateUTF8String(nodeToTakeTriviaFrom.GetLeadingTrivia(), stringValue, nodeToTakeTriviaFrom.GetTrailingTrivia()); |
There was a problem hiding this comment.
| return CreateUTF8String(nodeToTakeTriviaFrom.GetLeadingTrivia(), stringValue, nodeToTakeTriviaFrom.GetTrailingTrivia()); | |
| return CreateUTF8String(stringValue).WithTriviaFrom(nodeToTakeTrivia); |
There was a problem hiding this comment.
As per below, switching this to use WithTriviaFrom would work, but the method would have to pass two empty trivia lists for the leading and trailing trivia arguments, so not worth it.
| } | ||
|
|
||
| private static LiteralExpressionSyntax CreateUTF8String(SyntaxTriviaList leadingTrivia, string stringValue, SyntaxTriviaList trailingTrivia) | ||
| private static InvocationExpressionSyntax CreateUTF8String(SyntaxTriviaList leadingTrivia, string stringValue, SyntaxTriviaList trailingTrivia) |
There was a problem hiding this comment.
you can remove all trivia logic from this.
There was a problem hiding this comment.
Unfortunately, not. This is called for the param array case which needs to pass in no leading trivia, and trailing trivia from the last argument.
Fixes #61517
In response to the language spec change to make the type of
"ABC"u8be aReadOnlySpan<byte>rather than abyte[], this updates the analyzer and code fix I wrote previously to:.ToArray()on the end of the string literals it creates