-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Redeclaring function parameters gives error on usage not on redeclaration #43467
Comments
The reason we don't simply flag the variable declaration is because it's actually valid in Dart. Dart is a block structured language, which means that variables are defined in scopes, and scopes can be nested in other scopes. As with all block structured languages, it's valid to declare a variable in a nested scope that's also declared in an outer scope; the variable from the nested scope hides the one from the outer scope. The Dart language specification says that the parameters for a function (or method) are declared in a "parameter" scope and that the body of the function is a nested scope. So by definition it is valid to hide parameters by declaring a variable with the same name. We could, of course, add a lint to prevent hiding parameter names, but I suspect that there would be too many false positives for it to be useful to very many people. |
When redeclaring variables is allowed, why does this cause errors? Could this be solved by detecting if a variable is declared twice in the same scope? Edit:
Probably because inside a given scope there can only be a single declaration for a variable that can be used.
Well I think I overread this part:
|
The local variable Apart from the fact that this is just the way the scopes are structured, you could also argue that it would be confusing to allow the first |
Could the error message be adjusted to include a reference to the place where it was declared? Going from this message: As a bit of context, I stumbled upon this when refactoring some methods and I copied part of one method into another method. Since the destination already had the variable declared as a parameter and the pasted part contained the declaration it complained. I wondered why the variable was marked as undeclared, since the function parameter declared it, not realizing the problem was the redeclaration in the pasted part. I only found the cause because a vscode extension had a reference to it in the menu that appears when hovering over the error, but I believe that is not part of the linter. |
Considering this as a request for a more informative error message from the analyzer, it should be moved to the SDK repo. Doing that now. |
As implied above, the error message from the analyzer does not mention the location of the declaration of the local variable in the following example, and it might be helpful to include that information: void main(List<String> args) {
args.length;
var args;
} The analyzer does not mention the declaration of
|
Would it be possible to make a linting rule out of this? |
Actually, the analyzer already produces this information, but as a "context message". Unfortunately it isn't displayed unless you include the
Yes, but as I said above, "I suspect that there would be too many false positives for it to be useful to very many people." |
Does this still apply though? The only time this lint/error message should be displayed is when declaring a variable already used in this scope, which causes an error anyway. I don't see where there could be any false positives then. |
That's valid. I was thinking earlier about a less constrained lint. |
Sorry for the delay, what is the request for the lint rule? Is it that, given this code: void foo(int tmp) {
tmp++; // error here
var tmp = 5;
} we would continue to error on I think the solution to the UX issue here is just to add more text to the message like, "Local variable 'tmp' can't be referenced before it is declared on line X." or "note that declaration shadows another variable in this scope." |
Describe the issue
When a function parameter is redeclared, an error is displayed on every instance where the variable is used before the redeclaration instead of an error message on the redeclaration.
Relevant linting rules: referenced_before_declaration
To Reproduce
Expected behavior
I would expect the linter to also highlight the redeclaration stating that the variable is already declared.
Additional context
The referenced_before_declaration already gives a hint that this might be the problem ("Try [...] renaming the local variable so that it doesn't hide a name from an enclosing scope."), so why not also highlight the redeclaration as an error, which I would say it is in every case. That means I would also expect the following to throw errors, which at the moment it does not.
The text was updated successfully, but these errors were encountered: