-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[wildcards] report a warning on final
wildcard variables
#55920
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
I don't think it makes less sense than It just doesn't make sense to have a local variable declaration named An "unnecessary variable" warning would apply no matter how you declare it, Using (I guess I can see someone doing var a = e1, _ = for effect(), b = e3; but they still shouldn't.) |
Good points! Following your logic, it's probably sufficient to treat them both as dead and just issue a Thank you! |
Purely philosophically it's not really dead code. It's not code that could be run, but isn't reachable on any path. In practice, I'm fine with labeling it dead code. |
Should the full expression be marked dead code or only the assignment to the variable? |
I appreciate this, thanks. You're right.
Absolutely and if the initializer is side-effecting...
If we issue a dead code warning, I'd say we should limit it to the variable (and not the initializer). /fyi @bwilkerson for 2 cents |
I agree, we should only mark the variable, not the initializer. I believe that in the past we've made a distinction between 'dead code', code that can't be reached, and 'unnecessary' code, code that doesn't need to be there because it has no semantic value. I'd opt for using the latter terminology in this case. |
Thanks! It sounds like we want to ensure that |
One thing might come up: String? get maybeHello => null;
void main() {
// A switch statement isn't checked for exhaustiveness, unless
// the scrutinee has a type which is always-exhaustive.
switch (maybeHello) {
case String():
print('Got a String');
// Oops, we forgot the null case, no warning.
}
// We can use a switch expression, they are always checked for exhaustiveness.
// But they can't be used as statements, so we wrap it in `(...)`.
(switch (maybeHello) {
String() => print('Got a String'),
null => print('Got null'),
});
// Alternative way to allow for a switch expression. Perhaps some
// people would prefer this style?
var _ = switch (maybeHello) {
String() => print('Got a String'),
null => print('Got null'),
};
} A couple of expressions (switch expressions and map literals, at least) cannot be used as the expression of an expression statement (that is, it is an error to turn them into a statement simply by adding a semicolon at the end). We may need a workaround for those cases, and I don't know if it actually matters, but it wouldn't be too hard to omit the lint when the initializing expression is a switch expression. |
With wildcard variables, this doesn't make much sense:
I'm thinking we should produce a warning?
/cc @eernstg @lrhn @kallentu
The text was updated successfully, but these errors were encountered: