-
Notifications
You must be signed in to change notification settings - Fork 1.7k
proposal: no_nullable_values_in_interpolated_strings
#58615
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
Comments
The examples here suggests that the underlying rules would be:
Correct? |
Given the discussion at dart-lang/language#2053, I'd say having a string that possibly contains the word null being printed on-screen should still be linted. Additionally, via any way strings can make it to the user, which infamously includes So, you'd end up with: String? nullable;
String nonNull = "Hello, World!";
final a = "Welcome, $nonNull"; // ok, non-null value
final b = "Welcome, $nullable"; // lint: nullable expression in string interpolation
final c = "Welcome, ${nullable ?? 'user'}"; // ok, non-null value
final d = "Welcome, ${nullable!}"; // ok, assumed to be non-null
final e = "name=$?nullable"; // ok, value is allowed to be null, see dart-lang/language#2053
final f = "$a $b" // ok, b is unsafe, but using its value is fine
print(b); // ok, b is unsafe, but using its value is fine |
The problem is that In general, If we disable this lint only for string literals that are the argument expression of a The alternative is to do flow analysis on the values (which is definitely approximative at best) and consider any string containing a nullable interpolation as "tainted". If it flows anywhere except into If we actually introduce a I'd be fine with only allowing |
What if I want to print such values for logging purposes? Either using a package called logger or debugLog. Restricting null acceptance only to |
@lrhn I suppose If user deliberately refuses interpolation-soundness, they should process 'null-case' themselves (or leave it as is). |
I was bit by this recently in dartdoc. Very surprising to see "foo/bar/null" show up as a URI. And hard to debug. |
For what it's worth regarding the earlier concerns about debug printing, if this is kept as a lint rather than a warning, it will be at the same level as |
Instead of being clever, we could also just say "no nullable values in interpolations" and require you to do The |
no_nullable_values_in_interpolated_strings
Description
Do not use nullable values in interpolated strings.
Details
TODO:
See: dart-lang/language#2053.
Kind
Error
Good Examples
Bad Examples
Discussion
See: dart-lang/language#2053
The text was updated successfully, but these errors were encountered: