Skip to content
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

[Wildcard Variables] Update UNUSED_CATCH_CLAUSE reporting #55723

Open
Tracked by #55680
pq opened this issue May 14, 2024 · 3 comments
Open
Tracked by #55680

[Wildcard Variables] Update UNUSED_CATCH_CLAUSE reporting #55723

pq opened this issue May 14, 2024 · 3 comments
Assignees
Labels
analyzer-warning Issues with the analyzer's Warning codes area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. feature-wildcard-variables Implementation of the wildcard variables feature P2 A bug or feature request we're likely to work on

Comments

@pq
Copy link
Member

pq commented May 14, 2024

Like UNUSED_LOCAL_VARIABLEs (#55719), UNUSED_CATCH_CLAUSE should not special case __s.

This adjustment is straight-forward in the presence of an on clause where __s are special cased, however the case without an on is interesting... Currently we do not report unused catch clauses if there is no on clause regardless of underscores.

For example:

try {
} catch(e) { }

is treated the same as

try {
} catch(_) { }

(And neither produce a diagnostic.)

Question: with wildcard variables, should we start reporting UNUSED_CATCH_CLAUSE on all bound variables (and so on the catch(e) case above)?

(Note that this will be a bit disruptive -- luckily the quick fix is easy to implement.)

/cc @kallentu @bwilkerson @munificent

@pq pq self-assigned this May 14, 2024
@pq pq added area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. analyzer-warning Issues with the analyzer's Warning codes feature-wildcard-variables Implementation of the wildcard variables feature P2 A bug or feature request we're likely to work on labels May 14, 2024
@pq pq changed the title [Wildcard Variables] Update UNUSED_CATCH_CLAUSE reporting to flag multiple _s [Wildcard Variables] Update UNUSED_CATCH_CLAUSE May 14, 2024
@pq pq changed the title [Wildcard Variables] Update UNUSED_CATCH_CLAUSE [Wildcard Variables] Update UNUSED_CATCH_CLAUSE reporting May 14, 2024
@lrhn
Copy link
Member

lrhn commented May 15, 2024

Since you have to write something to catch an error, either } on Object { or } catch (_) {, we should probably not warn about the latter.

We could warn about one of on Object catch (_) being unnecessary. Probably the latter, we also have a lint against not having an on clause.
I wouldn't say anything about } catch (_) {, } catch (_, _) { or even } catch (e, _) {. They're fine as they are. Slightly longer than necessary for the last two, but not something worth bothering the author about unless they have a specific "As short as possible" lint.

@kallentu
Copy link
Member

I talked about this briefly with @pq today.

From what we discussed, there's some history with allowing exception variables (the e) to be unused. Even the lint examples for empty catch blocks show an un-opinionated example of catch clauses being ignored.

Maybe this is the right time, now that we have a mechanism for wildcards to push users to using _ if they're not actually using the exception variable?
Having the edge case where the exception variable can be unused in catches (with no on) seems weird, especially in a world where wildcards exist.

It'll be breaking, but the fix for it is simple (e -> _).

@pq
Copy link
Member Author

pq commented May 17, 2024

Maybe this is the right time, now that we have a mechanism for wildcards to push users to using _ if they're not actually using the exception variable?

💯

See #55719 for a proposal to start flagging all unused (non-wildcard) exception variables as UNUSED_LOCAL_VARIABLEs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-warning Issues with the analyzer's Warning codes area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. feature-wildcard-variables Implementation of the wildcard variables feature P2 A bug or feature request we're likely to work on
Projects
None yet
Development

No branches or pull requests

3 participants