diff --git a/pkg/analyzer/lib/src/task/strong/checker.dart b/pkg/analyzer/lib/src/task/strong/checker.dart index 3583fb3671f..c3f3647612a 100644 --- a/pkg/analyzer/lib/src/task/strong/checker.dart +++ b/pkg/analyzer/lib/src/task/strong/checker.dart @@ -578,6 +578,30 @@ class CodeChecker extends RecursiveAstVisitor { node.visitChildren(this); } + @override + void visitSetLiteral(SetLiteral node) { + DartType type = DynamicTypeImpl.instance; + if (node.typeArguments != null) { + NodeList targs = node.typeArguments.arguments; + if (targs.length > 0) { + type = targs[0].type; + } + } else { + DartType staticType = node.staticType; + if (staticType is InterfaceType) { + List typeArguments = staticType.typeArguments; + if (typeArguments != null && typeArguments.length > 0) { + type = typeArguments[0]; + } + } + } + NodeList elements = node.elements; + for (int i = 0; i < elements.length; i++) { + checkArgument(elements[i], type); + } + super.visitSetLiteral(node); + } + @override void visitSuperConstructorInvocation(SuperConstructorInvocation node) { var element = node.staticElement; diff --git a/pkg/analyzer/test/src/diagnostics/invalid_cast_new_expr_test.dart b/pkg/analyzer/test/src/diagnostics/invalid_cast_new_expr_test.dart new file mode 100644 index 00000000000..125d43d91bb --- /dev/null +++ b/pkg/analyzer/test/src/diagnostics/invalid_cast_new_expr_test.dart @@ -0,0 +1,71 @@ +// Copyright (c) 2019, 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. + +import 'package:analyzer/src/error/codes.dart'; +import 'package:test_reflective_loader/test_reflective_loader.dart'; + +import '../../generated/resolver_test_case.dart'; + +main() { + defineReflectiveSuite(() { + defineReflectiveTests(InvalidCastNewExprTest); + }); +} + +@reflectiveTest +class InvalidCastNewExprTest extends ResolverTestCase { + @override + List get enabledExperiments => ['set-literals']; + + @override + bool get enableNewAnalysisDriver => true; + + test_listLiteral_const() async { + await assertErrorsInCode(r''' +const c = [A()]; +class A { + const A(); +} +class B extends A { + const B(); +} +''', [StrongModeCode.INVALID_CAST_NEW_EXPR]); + } + + test_listLiteral_nonConst() async { + await assertErrorsInCode(r''' +var c = [A()]; +class A { + const A(); +} +class B extends A { + const B(); +} +''', [StrongModeCode.INVALID_CAST_NEW_EXPR]); + } + + test_setLiteral_const() async { + await assertErrorsInCode(r''' +const c = {A()}; +class A { + const A(); +} +class B extends A { + const B(); +} +''', [StrongModeCode.INVALID_CAST_NEW_EXPR]); + } + + test_setLiteral_nonConst() async { + await assertErrorsInCode(r''' +var c = {A()}; +class A { + const A(); +} +class B extends A { + const B(); +} +''', [StrongModeCode.INVALID_CAST_NEW_EXPR]); + } +} diff --git a/pkg/analyzer/test/src/diagnostics/test_all.dart b/pkg/analyzer/test/src/diagnostics/test_all.dart index 1a892836df9..20b7c10544c 100644 --- a/pkg/analyzer/test/src/diagnostics/test_all.dart +++ b/pkg/analyzer/test/src/diagnostics/test_all.dart @@ -9,6 +9,7 @@ import 'can_be_null_after_null_aware_test.dart' as can_be_null_after_null_aware; import 'deprecated_member_use_test.dart' as deprecated_member_use; import 'division_optimization_test.dart' as division_optimization; import 'invalid_assignment_test.dart' as invalid_assignment; +import 'invalid_cast_new_expr_test.dart' as invalid_cast_new_expr; import 'invalid_required_param_test.dart' as invalid_required_param; import 'undefined_getter.dart' as undefined_getter; import 'unnecessary_cast_test.dart' as unnecessary_cast; @@ -25,6 +26,7 @@ main() { deprecated_member_use.main(); division_optimization.main(); invalid_assignment.main(); + invalid_cast_new_expr.main(); invalid_required_param.main(); undefined_getter.main(); unnecessary_cast.main();