Skip to content

Commit

Permalink
quick fix for NON_CONST_CALL_TO_LITERAL_CONSTRUCTOR_USING_NEW
Browse files Browse the repository at this point in the history
See: #55917

Change-Id: I9576105618d0201b4fdc339361b6c164870267b9
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/370140
Reviewed-by: Brian Wilkerson <[email protected]>
Commit-Queue: Phil Quitslund <[email protected]>
Auto-Submit: Phil Quitslund <[email protected]>
  • Loading branch information
pq authored and Commit Queue committed Jun 7, 2024
1 parent a1b42e1 commit 63d6e6f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3638,9 +3638,7 @@ WarningCode.NON_CONST_CALL_TO_LITERAL_CONSTRUCTOR:
notes: |-
The fix is to add `const` before the constructor invocation.
WarningCode.NON_CONST_CALL_TO_LITERAL_CONSTRUCTOR_USING_NEW:
status: needsFix
notes: |-
The fix is to replace `new` with `const`.
status: hasFix
WarningCode.NON_NULLABLE_EQUALS_PARAMETER:
status: needsFix
notes: |-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1582,6 +1582,9 @@ final _builtInNonLintProducers = <ErrorCode, List<ProducerGenerator>>{
WarningCode.MUST_CALL_SUPER: [
AddCallSuper.new,
],
WarningCode.NON_CONST_CALL_TO_LITERAL_CONSTRUCTOR_USING_NEW: [
ReplaceNewWithConst.new,
],
WarningCode.NULLABLE_TYPE_IN_CATCH_CLAUSE: [
RemoveQuestionMark.new,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ class ReplaceNewWithConstTest extends FixProcessorLintTest {
@override
String get lintCode => LintNames.prefer_const_constructors;

@override
void setUp() {
super.setUp();
writeTestPackageConfig(meta: true);
}

Future<void> test_new() async {
await resolveTestCode('''
class C {
Expand Down Expand Up @@ -137,4 +143,23 @@ void f() {
// handled by ADD_CONST
await assertNoFix();
}

Future<void> test_nonConstCallToLiteralConstructorUsingNew() async {
await resolveTestCode('''
import 'package:meta/meta.dart';
class A {
@literal
const A();
}
var a = new A();
''');
await assertHasFix('''
import 'package:meta/meta.dart';
class A {
@literal
const A();
}
var a = const A();
''');
}
}

0 comments on commit 63d6e6f

Please sign in to comment.