You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Analyzer proposal: Consider using String.Equals instead of String.Compare #45552
I am not sure why we need this? can you explain more why we need that? both are valid options and both are preferable as long as providing the StringComparison options. I don't think we need that in the analyzers.
I am not sure why we need this? can you explain more why we need that? both are valid options and both are preferable as long as providing the StringComparison options. I don't think we need that in the analyzers.
Sure, both are valid options, as @Therzok mentioned above we expect it will give a bit of performance benefit. From that point isn't string.Equals(x, y, StringComparison.CurrentCulture) is preferable than string.Compare(x, y, StringComparison.CurrentCulture) == 0? What you think @tarekgh
I think it would be worthwhile to have a separate analyzer to flag redundant assignment, like RCS1212.
If we had such an analyzer, rules like CA2249 and the rule proposed in this issue could use the rule as a pre-processor to improve detection of violations.
Activity
Evangelink commentedon Dec 2, 2020
@mavasani not sure who could confirm there is indeed a performance issue.
xtqqczze commentedon Dec 2, 2020
Perhaps this a more of a code style issue?
Therzok commentedon Dec 3, 2020
Compare and Equals
From the looks of it, between the two,
Equalsfast-paths on a.Lengthcheck,Comparedoes a full string comparison.mavasani commentedon Dec 3, 2020
This is a .NET API usage analyzer suggestion. @jeffhandley @carlossanlop @buyaa-n can this please be ported to dotnet/runtime repo and triaged?
tarekgh commentedon Dec 3, 2020
I am not sure why we need this? can you explain more why we need that? both are valid options and both are preferable as long as providing the StringComparison options. I don't think we need that in the analyzers.
buyaa-n commentedon Dec 7, 2020
Sure, both are valid options, as @Therzok mentioned above we expect it will give a bit of performance benefit. From that point isn't
string.Equals(x, y, StringComparison.CurrentCulture)is preferable thanstring.Compare(x, y, StringComparison.CurrentCulture) == 0? What you think @tarekgh38 remaining items
mavasani commentedon Dec 10, 2020
We can do either of the following:
==check is done in the same expression asCompareORresin above case only has single reference which compares it with 0@pgovind did an implementation that matches (2) for CA2249: https://github.com/dotnet/roslyn-analyzers/blob/6dea9b867e32607bc02f958efe46d4b2d1220e45/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/PreferStringContainsOverIndexOfAnalyzer.cs#L19
tarekgh commentedon Dec 10, 2020
@mavasani your proposal is reasonable. if yo go with #1 make sure this will not include.
buyaa-n commentedon Dec 10, 2020
Sure @tarekgh those scenarios shouldn't be flagged, i would vote for the #1 from @mavasani's proposal
xtqqczze commentedon Dec 13, 2020
I think it would be worthwhile to have a separate analyzer to flag redundant assignment, like RCS1212.
If we had such an analyzer, rules like
CA2249and the rule proposed in this issue could use the rule as a pre-processor to improve detection of violations.mavasani commentedon Dec 13, 2020
@xtqqczze The assignment above is not redundant,
RCS1212seems to be worded incorrectly, it should probably say inline variable (which we already provide as a refactoring in the VS IDE). Also note that we do have https://docs.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0059 for real redundant assignments where the value written is never read on any control flow path.mavasani commentedon Dec 13, 2020
@buyaa-n Sounds good to me. I think it will cover most realistic user code.
NewellClark commentedon May 17, 2021
I would like to be assigned to this, please.
Youssef1313 commentedon May 31, 2021
@buyaa-n This was already implemented. Should it be closed now?