-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Comments
Yes, 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. |
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
}
}
|
@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? |
Our dart version is: 46ab040 |
linter version is 0.1.49 I think? |
I can reproduce it on master of linter. It's related to ConstantVerifier that does not trigger error for |
I have identifier the cause of the problem. The expression cannot be Fixing this problem is a bit bigger than I have time for at the moment. |
@bwilkerson thanks for your explanation. |
As |
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 :) |
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]>
Has the analyzer fix unblocked this? |
The fix unblocked but some other changes need to be done (see dart-archive/linter#1035) |
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. |
|
Yes! |
Super. Can I close? |
I think so. |
Good deal. Thanks for your perseverance and @apwilson for your continued patience! |
The following code triggers the prefer_const_constructors linter rule on the initial assignment of foo:
As I thought new/const was optional and inferred, this appears to be a false positive.
The text was updated successfully, but these errors were encountered: