Generate diagnostic for non-bool conditions in if elements
Change-Id: I36bd37a3a3bdfea7ca0853d90299b5fa7d36a352
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/96520
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
diff --git a/pkg/analyzer/lib/src/generated/error_verifier.dart b/pkg/analyzer/lib/src/generated/error_verifier.dart
index a43094a..8dc85c4 100644
--- a/pkg/analyzer/lib/src/generated/error_verifier.dart
+++ b/pkg/analyzer/lib/src/generated/error_verifier.dart
@@ -894,6 +894,12 @@
}
@override
+ void visitIfElement(IfElement node) {
+ _checkForNonBoolCondition(node.condition);
+ super.visitIfElement(node);
+ }
+
+ @override
void visitIfStatement(IfStatement node) {
_checkForNonBoolCondition(node.condition);
super.visitIfStatement(node);
diff --git a/pkg/analyzer/test/src/diagnostics/non_bool_condition_test.dart b/pkg/analyzer/test/src/diagnostics/non_bool_condition_test.dart
new file mode 100644
index 0000000..4865ced
--- /dev/null
+++ b/pkg/analyzer/test/src/diagnostics/non_bool_condition_test.dart
@@ -0,0 +1,36 @@
+// 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/dart/analysis/experiments.dart';
+import 'package:analyzer/src/error/codes.dart';
+import 'package:analyzer/src/generated/engine.dart';
+import 'package:test_reflective_loader/test_reflective_loader.dart';
+
+import '../dart/resolution/driver_resolution.dart';
+
+main() {
+ defineReflectiveSuite(() {
+// defineReflectiveTests(NonBoolConditionTest);
+ defineReflectiveTests(NonBoolConditionWithUIAsCodeTest);
+ });
+}
+
+//@reflectiveTest
+class NonBoolConditionTest extends DriverResolutionTest {}
+
+@reflectiveTest
+class NonBoolConditionWithUIAsCodeTest extends NonBoolConditionTest {
+ @override
+ AnalysisOptionsImpl get analysisOptions => AnalysisOptionsImpl()
+ ..enabledExperiments = [
+ EnableString.control_flow_collections,
+ EnableString.spread_collections,
+ ];
+
+ test_ifElement() async {
+ assertErrorsInCode('''
+const c = [if (3) 1];
+''', [StaticTypeWarningCode.NON_BOOL_CONDITION]);
+ }
+}
diff --git a/pkg/analyzer/test/src/diagnostics/test_all.dart b/pkg/analyzer/test/src/diagnostics/test_all.dart
index edae1b5..7a4ae3d 100644
--- a/pkg/analyzer/test/src/diagnostics/test_all.dart
+++ b/pkg/analyzer/test/src/diagnostics/test_all.dart
@@ -31,6 +31,7 @@
import 'mixin_on_sealed_class_test.dart' as mixin_on_sealed_class;
import 'must_be_immutable_test.dart' as must_be_immutable;
import 'must_call_super_test.dart' as must_call_super;
+import 'non_bool_condition_test.dart' as non_bool_condition;
import 'non_constant_map_element_test.dart' as non_constant_map_element;
import 'set_element_type_not_assignable_test.dart'
as set_element_type_not_assignable;
@@ -85,6 +86,7 @@
mixin_on_sealed_class.main();
must_be_immutable.main();
must_call_super.main();
+ non_bool_condition.main();
non_constant_map_element.main();
set_element_type_not_assignable.main();
subtype_of_sealed_class.main();