WiseTechGlobal / WTG.Analyzers

Analyzers from WiseTech Global to enforce our styles, behaviours, and prevent common mistakes.
Other
16 stars 3 forks source link

Analyzer to warn against pointless uses of interpolated strings #143

Closed yaakov-h closed 1 year ago

yaakov-h commented 3 years ago

i.e. $"this string has no format specifiers", including $@/@$ variations.

brian-reichle commented 3 years ago

On the one hand, I agree with the general idea of this rule, on the other hand I have used this very technique to avoid creating an overload of a method that takes a string (because the string overload would break the usage of the FormattableString overload.)

I would probably also classify $"{foo}" as pointless when the overall expression result will be a string (instead of FormattableString).

yaakov-h commented 3 years ago

That's a good point, we would only want to flag it when the receiver is of type string - particularly as the C# team are also looking into other types of formattable string builders for high-perf.

We could also be fancy and try detect overloads but that might be a bit much.

I agree on $"{foo}".

brian-reichle commented 2 years ago

Occasionally I come across people using interpolated strings as a way of concatenating strings.

eg. $"{foo}{bar}" where both foo and bar are strings, should be foo + bar instead.

though this could lead down a rabbit warren and I'm not sure where to draw the line.

$"{foo}|{bar}" feels like it should be foo + "|" + bar, but $"{foo} not found in {bar}" feels like it should remain an interpolated string.

brian-reichle commented 2 years ago

It looks like C# is already making that conversion for us, so it's really just down to which conveys semantics better.

yaakov-h commented 2 years ago

C# is also getting constant interpolated strings and other significant improvements to interpolated strings next month, so I'm tempted to leave them as they are if they are actually doing some interpolation.