diff --git a/test/rules/prefer_const_constructors_test.dart b/test/rules/prefer_const_constructors_test.dart index 7654a48eb..9a292f88e 100644 --- a/test/rules/prefer_const_constructors_test.dart +++ b/test/rules/prefer_const_constructors_test.dart @@ -20,6 +20,214 @@ class PreferConstConstructorsTest extends LintRuleTest { @override String get lintRule => 'prefer_const_constructors'; + test_canBeConst_argumentIsAdjacentStrings() async { + await assertDiagnostics(r''' +class A { + const A(String s); +} +var a = A('adjacent' 'string'); +''', [ + lint(41, 22), + ]); + } + + test_canBeConst_argumentIsListLiteral() async { + await assertDiagnostics(r''' +class A { + const A(List l); +} +var a = A([]); +''', [ + lint(44, 5), + ]); + } + + test_canBeConst_argumentIsMap_nonLiteral() async { + await assertNoDiagnostics(r''' +class A { + const A(Map m); +} +A f(Map m) => A(m); +'''); + } + + test_canBeConst_argumentIsMapLiteral_containsNonLiteral() async { + await assertNoDiagnostics(r''' +class A { + const A(Map m); +} +A f(int x) => A({x: x}); +'''); + } + + test_canBeConst_argumentIsMapLiteral_instantiated() async { + await assertDiagnostics(r''' +class A { + const A(Map m); +} +var a = A({}); +''', [ + lint(48, 5), + ]); + } + + test_canBeConst_explicitTypeArgument_dynamic() async { + await assertDiagnostics(r''' +class A { + const A(); +} +var a = A(); +''', [ + lint(36, 12), + ]); + } + + test_canBeConst_explicitTypeArgument_string() async { + await assertDiagnostics(r''' +class A { + const A(); +} +var a = A(); +''', [ + lint(36, 11), + ]); + } + + test_canBeConst_intLiteralArgument() async { + await assertDiagnostics(r''' +class A { + const A(int x); +} +var a = A(5); +''', [ + lint(38, 4), + ]); + } + + test_canBeConst_optionalNamedParameter() async { + await assertDiagnostics(r''' +class A { + const A({A? parent}); +} +var a = A(); +''', [ + lint(44, 3), + ]); + } + + test_canBeConst_optionalNamedParameter_nested() async { + await assertDiagnostics(r''' +class A { + const A({A? parent}); + const A.a(); +} +var a = A( + parent: A.a(), +); +''', [ + lint(59, 21), + lint(72, 5), + ]); + } + + test_canBeConst_optionalNamedParameter_newKeyword() async { + await assertDiagnostics(r''' +class A { + const A({A? parent}); +} +var a = new A(); +''', [ + lint(44, 7), + ]); + } + + test_cannotBeConst_argumentIsAdjacentStrings_withInterpolation() async { + await assertNoDiagnostics(r''' +class A { + const A(String s); +} +A f(int i) => A('adjacent' '$i'); +'''); + } + + test_cannotBeConst_argumentIsListLiteral_nonLiteralElement() async { + await assertNoDiagnostics(r''' +class A { + const A(List l); +} +A f(int i) => A([i]); +'''); + } + + test_cannotBeConst_argumentIsLocalVariable() async { + await assertNoDiagnostics(r''' +class A { + const A(String s); +} + +void f() { + final s = ''; + var a = A(s); +} +'''); + } + + test_cannotBeConst_argumentIsNonLiteral() async { + await assertNoDiagnostics(r''' +class A { + const A(String s); +} +A f(String s) => A(s); +'''); + } + + test_cannotBeConst_argumentIsNonLiteralList() async { + await assertNoDiagnostics(r''' +class A { + const A(List l); +} +A f(List l) => A(l); +'''); + } + + test_cannotBeConst_explicitTypeArgument_typeVariable() async { + await assertNoDiagnostics(r''' +class A { + const A(); +} +void f() => A(); +'''); + } + + test_cannotBeConst_nonConstArgument() async { + await assertNoDiagnostics(r''' +class A { + final int x; + + const A(this.x); +} +A f(int x) => A(x); +'''); + } + + test_cannotBeConst_notConstConstructor() async { + await assertNoDiagnostics(r''' +class A { + A(); +} +var a = A(); +'''); + } + + test_cannotBeConst_stringLiteralArgument_withInterpolation() async { + await assertNoDiagnostics(r''' +class A { + const A(String s); + static A m1(int i) => A('$i'); +} +'''); + } + test_deferred_arg() async { newFile2('$testPackageLibPath/a.dart', ''' class A { @@ -76,4 +284,30 @@ K k() { error(WarningCode.NON_CONST_CALL_TO_LITERAL_CONSTRUCTOR, 90, 3), ]); } + + test_isConst_intLiteralArgument() async { + await assertNoDiagnostics(r''' +class A { + final int x; + + const A(this.x); +} +A f() => const A(5); +'''); + } + + test_isConstCall_optionalNamedParameter() async { + await assertNoDiagnostics(r''' +class A { + const A({A? parent}); +} +var a = const A(); +'''); + } + + test_objectConstructorCall() async { + await assertNoDiagnostics(r''' +var x = Object(); +'''); + } } diff --git a/test_data/rules/prefer_const_constructors.dart b/test_data/rules/prefer_const_constructors.dart deleted file mode 100644 index 2c76ed70c..000000000 --- a/test_data/rules/prefer_const_constructors.dart +++ /dev/null @@ -1,100 +0,0 @@ -// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. - -// test w/ `dart test -N prefer_const_constructors` - -// ignore_for_file: unused_local_variable - -class A { - const A({A? parent}); - const A.a(); -} - -void accessA_0() { - A a = A(); // LINT - A a1 = A( // LINT - parent: A.a(), // LINT - ); -} - -void accessA_1() { - A a = new A(); // LINT -} - -void accessA_2() { - A a = const A(); // OK -} - -class B { - B(); -} - -void accessB() { - B b = new B(); // OK -} - -class C { - final int x; - - const C(this.x); -} - -C foo(int x) => new C(x); // OK -C bar() => const C(5); // OK -C baz() => new C(5); // LINT - -void objectId() { - Object id = new Object(); // OK -} - -class E { - final String s; - - const E(this.s); - - static E m1(int i) => new E('$i'); // OK - static E m2() => new E('adjacent' 'string'); // LINT - static E m3(int i) => new E('adjacent' '$i'); // OK - static E m4(String s) => new E(s); // OK - static void m5() { - final String s = ''; - E e = new E(s); // OK - } -} - -class F { - final List l; - - const F(this.l); - - static F m1() => new F([]); // LINT - static F m2(List l) => new F(l); // OK - static F m3(F f) => new F([f]); // OK -} - -class G { - final Map m; - - const G(this.m); - - static G m1() => new G({}); // LINT - static G m2(Map m) => new G(m); // OK - static G m3(G g) => new G({g: g}); // OK -} - -// optional new : https://github.com/dart-lang/linter/issues/995 -class H {} -class I { - final H foo; - const I({required this.foo}); - - I makeI() => I(foo: H()); // OK -} - -class J { - const J(); -} -void gimmeJ() => new J(); // OK -void gimmeJofString() => new J(); // LINT -void gimmeJofDynamic() => new J(); // LINT