linter: Move avoid_bool_literals_in_conditional_expressions tests
Change-Id: Ia5bf68415bf9842480adcba1da45c26e3ae6cc32
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/365547
Commit-Queue: Phil Quitslund <pquitslund@google.com>
Auto-Submit: Samuel Rawlins <srawlins@google.com>
Reviewed-by: Phil Quitslund <pquitslund@google.com>
diff --git a/pkg/linter/lib/src/rules/avoid_bool_literals_in_conditional_expressions.dart b/pkg/linter/lib/src/rules/avoid_bool_literals_in_conditional_expressions.dart
index 2769e24..05f93ff 100644
--- a/pkg/linter/lib/src/rules/avoid_bool_literals_in_conditional_expressions.dart
+++ b/pkg/linter/lib/src/rules/avoid_bool_literals_in_conditional_expressions.dart
@@ -65,8 +65,8 @@
@override
void visitConditionalExpression(ConditionalExpression node) {
var typeProvider = context.typeProvider;
- var thenExp = node.thenExpression;
- var elseExp = node.elseExpression;
+ var thenExp = node.thenExpression.unParenthesized;
+ var elseExp = node.elseExpression.unParenthesized;
if (thenExp.staticType == typeProvider.boolType &&
elseExp.staticType == typeProvider.boolType) {
diff --git a/pkg/linter/test/rules/all.dart b/pkg/linter/test/rules/all.dart
index fafcab5..f5722e8 100644
--- a/pkg/linter/test/rules/all.dart
+++ b/pkg/linter/test/rules/all.dart
@@ -11,6 +11,8 @@
import 'annotate_redeclares_test.dart' as annotate_redeclares;
import 'avoid_annotating_with_dynamic_test.dart'
as avoid_annotating_with_dynamic;
+import 'avoid_bool_literals_in_conditional_expressions_test.dart'
+ as avoid_bool_literals_in_conditional_expressions;
import 'avoid_catching_errors_test.dart' as avoid_catching_errors;
import 'avoid_classes_with_only_static_members_test.dart'
as avoid_classes_with_only_static_members;
@@ -274,6 +276,7 @@
annotate_overrides.main();
annotate_redeclares.main();
avoid_annotating_with_dynamic.main();
+ avoid_bool_literals_in_conditional_expressions.main();
avoid_catching_errors.main();
avoid_classes_with_only_static_members.main();
avoid_dynamic_calls.main();
diff --git a/pkg/linter/test/rules/avoid_bool_literals_in_conditional_expressions_test.dart b/pkg/linter/test/rules/avoid_bool_literals_in_conditional_expressions_test.dart
new file mode 100644
index 0000000..63dcd59
--- /dev/null
+++ b/pkg/linter/test/rules/avoid_bool_literals_in_conditional_expressions_test.dart
@@ -0,0 +1,71 @@
+// Copyright (c) 2024, 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:test_reflective_loader/test_reflective_loader.dart';
+
+import '../rule_test_support.dart';
+
+main() {
+ defineReflectiveSuite(() {
+ defineReflectiveTests(AvoidBoolLiteralsInConditionalExpressionsTest);
+ });
+}
+
+@reflectiveTest
+class AvoidBoolLiteralsInConditionalExpressionsTest extends LintRuleTest {
+ @override
+ String get lintRule => 'avoid_bool_literals_in_conditional_expressions';
+
+ test_elseFalse() async {
+ await assertDiagnostics(r'''
+var a = true;
+var b = a ? a : false;
+''', [
+ lint(22, 13),
+ ]);
+ }
+
+ test_elseTrue() async {
+ await assertDiagnostics(r'''
+var a = true;
+var b = a ? a : true;
+''', [
+ lint(22, 12),
+ ]);
+ }
+
+ test_noLiterals() async {
+ await assertNoDiagnostics(r'''
+var a = true;
+var b = a ? a : a;
+''');
+ }
+
+ test_thenFalse() async {
+ await assertDiagnostics(r'''
+var a = true;
+var b = a ? false : a;
+''', [
+ lint(22, 13),
+ ]);
+ }
+
+ test_thenTrue() async {
+ await assertDiagnostics(r'''
+var a = true;
+var b = a ? true : a;
+''', [
+ lint(22, 12),
+ ]);
+ }
+
+ test_thenTrue_parenthesized() async {
+ await assertDiagnostics(r'''
+var a = true;
+var b = a ? (true) : a;
+''', [
+ lint(22, 14),
+ ]);
+ }
+}
diff --git a/pkg/linter/test_data/rules/avoid_bool_literals_in_conditional_expressions.dart b/pkg/linter/test_data/rules/avoid_bool_literals_in_conditional_expressions.dart
deleted file mode 100644
index 26a1643..0000000
--- a/pkg/linter/test_data/rules/avoid_bool_literals_in_conditional_expressions.dart
+++ /dev/null
@@ -1,11 +0,0 @@
-// Copyright (c) 2018, 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.
-
-late bool a, b, c;
-
-var d = a ? true : b; // LINT
-var e = a ? false : b; // LINT
-var f = a ? b : true; // LINT
-var g = a ? b : false; // LINT
-var h = a ? b : c; // OK