Skip to content

prefer_const_constructors false positive #57706

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

Closed
apwilson opened this issue May 17, 2018 · 18 comments
Closed

prefer_const_constructors false positive #57706

apwilson opened this issue May 17, 2018 · 18 comments
Labels
devexp-linter Issues with the analyzer's support for the linter package legacy-area-analyzer Use area-devexp instead.

Comments

@apwilson
Copy link
Contributor

apwilson commented May 17, 2018

The following code triggers the prefer_const_constructors linter rule on the initial assignment of foo:

///
class Foo {
  ///
  const Foo();
}

///
class Bar {
  ///
  Foo foo = Foo();    // Triggers prefer_const_constructors

  ///
  set changeFoo(Foo newFoo) {
    foo = newFoo;
  }
}

As I thought new/const was optional and inferred, this appears to be a false positive.

@matanlurey
Copy link
Contributor

Yes, new is optional, but const is only optional in a const context. Consider:

main() {
  var foo = /* new */ Foo();
  var foos = const [
    /* const */ Foo(),
  ];
}

Sorry about the confusion. I think @leafpetersen is working on an update to this feature for the language site/etc.

@apwilson
Copy link
Contributor Author

apwilson commented May 17, 2018

So I have a related false positive then:

///
class Foo {}

///
class Bar {
  ///
  final Foo foo;

  ///
  const Bar({this.foo});
}

///
class BarFactory {
  ///
  Bar makeBar() {
    return Bar(foo: Foo()); // false positive saying I should make Bar const.  But if I make Bar const, Foo will try to become const and it can't
  }
}

@matanlurey matanlurey reopened this May 17, 2018
@a14n
Copy link
Contributor

a14n commented May 17, 2018

@apwilson I don't see the lint with your last example. What's the version of your Dart SDK and the linter package if you have one specified?

@apwilson
Copy link
Contributor Author

Our dart version is: 46ab040

@apwilson
Copy link
Contributor Author

linter version is 0.1.49 I think?

@a14n
Copy link
Contributor

a14n commented May 18, 2018

I can reproduce it on master of linter. It's related to ConstantVerifier that does not trigger error for const Bar(foo: Foo()). @bwilkerson could you take a look?

@bwilkerson
Copy link
Member

I have identifier the cause of the problem. The expression cannot be const because Foo can't be const, but the error indicating the problem is not produced by the ConstantVerifier. Hence, it does not appear in the list of "constant related errors" and the lint rule cannot tell that the expression cannot be const.

Fixing this problem is a bit bigger than I have time for at the moment.

@a14n
Copy link
Contributor

a14n commented May 18, 2018

@bwilkerson thanks for your explanation.

@a14n
Copy link
Contributor

a14n commented May 28, 2018

As ConstantVerifier is also used by prefer_const_literals_to_create_immutables it may also explain prefer_const_literals_to_create_immutables false positives mentioned with optional new/const in #57712

@a14n
Copy link
Contributor

a14n commented Jun 19, 2018

I tried to fix it with this simple diff:

diff --git a/pkg/analyzer/lib/src/dart/constant/evaluation.dart b/pkg/analyzer/lib/src/dart/constant/evaluation.dart
index b65aa46..eb2e46b 100644
--- a/pkg/analyzer/lib/src/dart/constant/evaluation.dart
+++ b/pkg/analyzer/lib/src/dart/constant/evaluation.dart
@@ -417,6 +417,11 @@ class ConstantEvaluationEngine {
       ConstantVisitor constantVisitor,
       ErrorReporter errorReporter,
       {ConstructorInvocation invocation}) {
+    if (!constructor.isConst) {
+      errorReporter.reportErrorForNode(
+          CompileTimeErrorCode.CONST_WITH_NON_CONST, node);
+      return null;
+    }
     if (!getConstructorImpl(constructor).isCycleFree) {
       // It's not safe to evaluate this constructor, so bail out.
       // TODO(paulberry): ensure that a reasonable error message is produced

But this change triggers errors in several tests that need more knowledge and time than I have to fix them.

Any help would be very welcome :)

dart-bot referenced this issue Jun 20, 2018
To unblock dart-lang/linter#995

Closes #33515
#33515

GitOrigin-RevId: 9a6d3ca
Change-Id: Ie8d627c031489bbaa606063b0a43c6696e45c2f1
Reviewed-on: https://dart-review.googlesource.com/61220
Commit-Queue: Brian Wilkerson <[email protected]>
Reviewed-by: Brian Wilkerson <[email protected]>
@dgrove
Copy link
Contributor

dgrove commented Jun 22, 2018

Has the analyzer fix unblocked this?

@a14n
Copy link
Contributor

a14n commented Jun 24, 2018

The fix unblocked but some other changes need to be done (see dart-archive/linter#1035)

@pq
Copy link
Member

pq commented Jun 25, 2018

Thanks @a14n . Right, this is gated by an analyzer release; @bwilkerson and @devoncarew and I need to get together on how to make one happen.

@pq
Copy link
Member

pq commented Jun 26, 2018

analyzer published and our DEP bumped; are we good here now @a14n ?

@a14n
Copy link
Contributor

a14n commented Jun 26, 2018

Yes!

@pq
Copy link
Member

pq commented Jun 26, 2018

Super. Can I close?

@a14n
Copy link
Contributor

a14n commented Jun 26, 2018

I think so.

@pq
Copy link
Member

pq commented Jun 26, 2018

Good deal. Thanks for your perseverance and @apwilson for your continued patience!

@pq pq closed this as completed Jun 26, 2018
@devoncarew devoncarew added devexp-linter Issues with the analyzer's support for the linter package legacy-area-analyzer Use area-devexp instead. labels Nov 18, 2024
@devoncarew devoncarew transferred this issue from dart-archive/linter Nov 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
devexp-linter Issues with the analyzer's support for the linter package legacy-area-analyzer Use area-devexp instead.
Projects
None yet
Development

No branches or pull requests

7 participants