Hi
Having just upgraded to the latest version and cleaned up a large document I got a load of errors, I've tracked the error down to the following line of code:
C#string sortCodeFormat = $"{(sortCode.Substring(0, 2))}-{(sortCode.Substring(2, 2))}-{(sortCode.Substring(4, 2))}";
As you can see I use the $"xxxx" command to place code within the string rather than use the older string.format() command. However this has caused an issue.
I currently have C#>Formatting>Wrapping > Arguments turned on to wrap the lines if it goes over 120 characters, the above line was wrapped to look like this:
C#string sortCodeFormat = $"{(sortCode.Substring(0, 2))}-{(sortCode.Substring(2, 2))}-{(sortCode.Substring(4, 2))}
";
Due to the bad formatting of this line, the whole of the documents then threw various errors, took me a while to fine this issue in a c# file with over 7000 lines of code.
I think you need to remove the wrapping of arguments within the string $"" command.
Many thanks
Alan
Hello Alan,
Thank you for contacting us and providing code samples.
I tried to reproduce this issue on the clean machine with CodeRush for Roslyn 17.2.3, but without success.
To help in our research, would you provide the following information:
- CodeRush settings (they can be obtained at "%AppData%\CodeRush");
- a simple project where the issue is reproducible.
We are looking forward to your response.
Hi
I've had another look at the original code, and the actual code was using string.format, so it converted that to use $"…" then formatted the arguments.
here is the code:
namespace Testing0 { internal class Class2 { public string test() { string sortCode = string.Empty; string sortCodeFormat = string.Format("{0}-{1}-{2}", sortCode.Substring(0, 2), sortCode.Substring(2, 2), sortCode.Substring(4, 2)); return sortCodeFormat; } } }
and after cleanup:
namespace Testing0 { internal class Class2 { public string test() { string sortCode = string.Empty; string sortCodeFormat = $"{(sortCode.Substring(0, 2))}-{(sortCode.Substring(2, 2))}-{(sortCode.Substring(4, 2))}"; return sortCodeFormat; } } }
I've attached my settings file for you. Hopefully you can see what it's doing this time. Apologies, it was a 7000+ lines of code, so didn't know it wast string.Format before.
Just another quick update - I looked at the order of the code Clean up list, and I currently have "Format Document" at the top, and then several rows later I have the "Convert to String Interpolation". I've now moved the convert to above the format document row, and it works as expected.
So "Sort of fixed" if I put them in the correct order, however I guess others might not have them in the correct order and might run into this issue, so you might need to put some more checks in the code.
Hope this helps.
Hello Alan,
Thank you for providing the settings and your detailed explanation.
I have reproduced the same behavior. The code becomes uncompilable if the "Format Document" cleanup rule is above "Convert to string interpolation".
We will research this issue and notify you as soon as we make any progress.
Excellent, glad you could replicate it - Not sure what the default "order" is of the options, but I think I changed mine quite a bit to put them in a "logical" order (for me anyway). I understand it's important to have them in a certain order, but not sure if you need to put some rules around what needs to be above what, or if you need to add more rules to the code.
Hello Alan,
Thank you for the clarification.
We believe that code cleanup rules should work correctly regardless of their order.
So we are trying to fix this issue even if the "Format Document" cleanup rule is above "Convert to string interpolation".
Excellent! :)