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
Culture sensitivity #266
Culture sensitivity #266
Conversation
dotnet_diagnostic.CA1304.severity = none # Method behaviour depends upon user's locale | ||
dotnet_diagnostic.CA1305.severity = none # Method behaviour depends upon user's locale | ||
dotnet_diagnostic.CA1307.severity = none # Method behaviour depends upon user's locale |
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.
Is the default "warning" here?
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.
I got warnings. The docs categorizes the rules as globalization warnings.
WriteVerbose($"Extracting metadata from file: {FilePath}"); | ||
WriteVerbose(string.Format(CultureInfo.InvariantCulture, "Extracting metadata from file: {0}", FilePath)); |
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.
Were these transformations performed manually or via automation?
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.
Hand made with ♥. I don't know any tool for that.
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.
I thought perhaps the analyzer came with a codefix.
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.
Great idea!
WriteVerbose($"Extracting metadata from file: {FilePath}"); | ||
WriteVerbose(string.Format(CultureInfo.InvariantCulture, "Extracting metadata from file: {0}", FilePath)); |
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.
Pardon my ignorance, but how can culture influence string concatenation? For example, other cases were previously $"Foo {nameof(Bar)}"
, which I believe the compiler is within rights to optimise to "Foo Bar"
at compile time.
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.
String interpolation uses the current culture. There are issues for cases like
var bar = 1.23;
$"Foo {bar}"
but (and you are right) not for cases like:
var bar = 1.23;
$"Foo {nameof(bar)}"
If you like I revert the changes for these cases with nameof
.
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.
Finally also cases like
var bar = 1.23;
string.Concat("Foo ", bar);
"Foo " + bar;
are not taking culture into account. These cases have not been translated as mentioned in the PR description.
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.
Did you only fix warnings produced by the analyzer, or did you search for all string interpolation?
I don't see warnings when reverting some of these changes. Where formattable types are involved, fair enough, but many of these changes involve only strings and the result is a reduction in both readability and performance.
I should point out that I'm also trying to work out whether the analyzer can be improved in order to provide feedback to that team.
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.
Visual studio helped me to find all places. I didn't get warnings. Knowing that interpolated strings use the current culture I tried to find them all. Validation was done with git grep
.
I found another notation the keep the readability.
FormattableString.Invariant($"ABC {i} DEF");
Do you like that?
@patricksadowski thanks for taking the initiative on this. Looks good. Just a few questions to help me understand. |
My original review was focussed on how the analyzer works and whether it could be improved. Thinking about this some more I realise something deeper. #138 is about a bug in parsing where the input is known to be invariant, but parsing code relies upon the current culture. This PR fixes that, however it also forces all formatted output to use invariant culture, which is not correct. The library should produce output strings that match the user's culture. If this was merged as-is, all strings would appear in invariant culture (which is essentially en-US). |
Because of your #138 (comment) I tried to fix the whole issue. I think I have another unterstanding of culture sensitivity. To get this done I propose to fix only the main bug of the issue. Your #138 (comment) should be done in a second PR oder issue. Is it OK for you? |
Yeah sorry, I hadn't thought the implications through when I wrote that. It wasn't intended as a strong statement. It may be possible to configure the analyzer to do what we want (via attributes or patterns in the
This is debatable. I'm pretty sure if this were changed then we'd see bug reports. Ideally all output strings would be localised.
That's certainly the safest way forward here. Perhaps we add a bit more context to the TODO in the |
4fc0e85
to
db0225b
Compare
/azp run |
Azure Pipelines successfully started running 1 pipeline(s). |
Thanks very much Patrick. |
Closes #138
There are still some string concatenation that do not use an invariant output format. But the main points of the issue are fixed.