linter icon indicating copy to clipboard operation
linter copied to clipboard

The `prefer_interpolation_to_compose_strings` bad example isn't even valid.

Open lrhn opened this issue 4 years ago • 2 comments

The description of the prefer_interpolation_to_compose_strings lint is that:

PREFER using interpolation to compose strings and values.

Using interpolation when composing strings and values is usually easier to write and read than concatenation.

BAD:

'Hello, ' + name + '! You are ' + (year - birth) + ' years old.';

GOOD:

'Hello, $name! You are ${year - birth} years old.';

The "bad" example isn't valid Dart. The year-birth is presumably a number, and you can't add a number to a string. it would at least need an added .toString().

The description says "usually", but doesn't say whether there are exceptions to the lint (are all String + String expressions lint violations? Only some?). The lint should specify precisely which cases are covered and which are not.

(Just today I wrote something like return firstChar.toUpperCase() + string.substring(firstChar.length);, which would not be improved by using an interpolation. I'd say that anything with precisely two parts and no literal string part or part ending in .toString() should be excluded from the lint.).

lrhn avatar May 25 '21 12:05 lrhn

I don't disagree, but I'll note that the docs for many of the lints were taken from the style guide at the time the lint was first written and are generally not updated when the style guide is updated. In particular the guide entry corresponding to this lint (https://dart.dev/guides/language/effective-dart/usage#prefer-using-interpolation-to-compose-strings-and-values) has been updated to have a corrected "bad" example, but the corresponding change wasn't made here.

In particular, I agree with the observation that the lint documentation sometimes needs to say more than the style guide about when the lint will and won't produce a diagnostic.

And excluding concatenation expressions in which there are no string literals seems reasonable to me.

bwilkerson avatar May 25 '21 14:05 bwilkerson

This particularly bad example was fixed w/ #3294.

pq avatar Apr 08 '22 21:04 pq