Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Tweak unnecessary_statements lint to ignore getters; they may have side effects. #1019

Merged

Conversation

davidmorgan
Copy link
Contributor

@pq
Copy link
Member

pq commented Jun 11, 2018

/cc @bwilkerson

Copy link
Contributor

@MichaelRFairhurst MichaelRFairhurst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, I especially like your improved wording at the top.

Only two comments, one of which is more of a question about what we want to do.

@override
visitPrefixedIdentifier(PrefixedIdentifier node) {
// Allow getters; they may have a side effect.
if (node.identifier.bestElement is PropertyAccessorElement) return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I think this is potentially too broad...I believe this will flag the following case:

class A {
  int x;
}

use(A a) {
  a.x;
}

I believe we can distinguish this vs. int get x because I believe the latter is a PropertyInducingElement and the former is not.

OTOH, I'm curious what others think. Its possible to do

class B implements A {
  int get x => throw ...;
}

So, we'd still have false positives for getters inside this lint in some perhaps common perhaps uncommon case. I may still defend them, due to the fact that that class of false errors would be more confusing (indirect) than accessing a custom defined getter directly.

@bwilkerson @pq

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks. Added test cases for this. (Three, one for each added clause). Fix was to check isSynthetic.

I'm not too worried about the cases with overrides; I think they're very rare.

@@ -160,6 +159,14 @@ class _ReportNoClearEffectVisitor extends UnifyingAstVisitor {
// Has a clear effect
}

@override
visitPrefixedIdentifier(PrefixedIdentifier node) {
// Allow getters; they may have a side effect.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would clarify, we aren't allowing them because the may have a side effect. So do operators since those could be overloaded too.

I would maybe say something more like, allow getters since they commonly have side effects? Or, because in practice they make up most of the false positives?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, thanks.

Copy link
Contributor

@MichaelRFairhurst MichaelRFairhurst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice @davidmorgan. Definitely LGTM

@davidmorgan
Copy link
Contributor Author

Thanks!

@davidmorgan davidmorgan merged commit 41c5dd3 into dart-lang:master Jun 13, 2018
@davidmorgan davidmorgan deleted the tweak-unnecessary-statements branch June 13, 2018 08:49
@pq
Copy link
Member

pq commented Jun 13, 2018

Thank you both!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants